File tree 2 files changed +12
-25
lines changed
2 files changed +12
-25
lines changed Original file line number Diff line number Diff line change 1
1
package discourse
2
2
3
3
import (
4
- "errors"
5
- "net/url"
6
- "strconv"
7
- "strings"
4
+ "fmt"
5
+ "regexp"
8
6
)
9
7
10
- func effectiveUrl (rawUrl string ) (string , error ) {
11
- url , err := url .Parse (rawUrl )
12
- if err != nil {
13
- return "" , err
14
- }
15
-
16
- paths := strings .Split (url .Path , "/" )
17
- if len (paths ) != 4 {
18
- return "" , errors .New ("invalid url, does not enough paths" )
19
- }
8
+ var urlRegex = regexp .MustCompile (`http[s]?://[^/]+/t/[^/]+/[0-9]+` )
20
9
21
- if paths [1 ] != "t" {
22
- return "" , errors .New ("invalid url, does not have a `/t/` path" )
23
- }
24
-
25
- rawId := paths [3 ]
26
- if _ , err = strconv .Atoi (rawId ); err != nil {
27
- return "" , errors .New ("invalid url, does not end with a number" )
10
+ func effectiveUrl (rawUrl string ) (string , error ) {
11
+ url := urlRegex .FindString (rawUrl )
12
+ if url == "" {
13
+ return "" , fmt .Errorf ("invalid url: %s" , rawUrl )
28
14
}
29
-
30
- return url .String () + ".json" , nil
15
+ return url + ".json" , nil
31
16
}
Original file line number Diff line number Diff line change @@ -4,8 +4,10 @@ import "testing"
4
4
5
5
func TestEffectiveUrl (t * testing.T ) {
6
6
testUrls := map [string ]string {
7
- "https://llllllll.co/t/disquiet-junto-project-0590-concrete-roots/62027" : "https://llllllll.co/t/disquiet-junto-project-0590-concrete-roots/62027.json" ,
8
- "https://0x00sec.org/t/question-on-your-need-to-be-known/34563" : "https://0x00sec.org/t/question-on-your-need-to-be-known/34563.json" ,
7
+ "https://llllllll.co/t/disquiet-junto-project-0590-concrete-roots/62027" : "https://llllllll.co/t/disquiet-junto-project-0590-concrete-roots/62027.json" ,
8
+ "https://0x00sec.org/t/question-on-your-need-to-be-known/34563" : "https://0x00sec.org/t/question-on-your-need-to-be-known/34563.json" ,
9
+ "https://discourse.haskell.org/t/an-opportunity-that-i-couldnt-pass-up/7485/" : "https://discourse.haskell.org/t/an-opportunity-that-i-couldnt-pass-up/7485.json" ,
10
+ "https://discourse.haskell.org/t/an-opportunity-that-i-couldnt-pass-up/7485/3" : "https://discourse.haskell.org/t/an-opportunity-that-i-couldnt-pass-up/7485.json" ,
9
11
}
10
12
for rawUrl , expected := range testUrls {
11
13
got , _ := effectiveUrl (rawUrl )
You can’t perform that action at this time.
0 commit comments