Skip to content

Commit f4ab491

Browse files
authored
moving vocation logic to separate function (#17)
to be able to sanitize the input
1 parent 9a7b146 commit f4ab491

File tree

3 files changed

+45
-45
lines changed

3 files changed

+45
-45
lines changed

src/TibiaHighscoresV3.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,8 @@ func TibiaHighscoresV3(c *gin.Context) {
106106
category = "experience"
107107
}
108108

109-
// Sanitize of vocation value and assigning correct vocationid for request
110-
var vocationid string
111-
vocationid = "0"
112-
if len(vocation) > 0 {
113-
if strings.EqualFold(vocation, "none") {
114-
vocationid = "1"
115-
vocation = "none"
116-
} else if strings.EqualFold(vocation, "knight") || strings.EqualFold(vocation, "knights") {
117-
vocationid = "2"
118-
vocation = "knights"
119-
} else if strings.EqualFold(vocation, "paladin") || strings.EqualFold(vocation, "paladins") {
120-
vocationid = "3"
121-
vocation = "paladins"
122-
} else if strings.EqualFold(vocation, "sorcerer") || strings.EqualFold(vocation, "sorcerers") {
123-
vocationid = "4"
124-
vocation = "sorcerers"
125-
} else if strings.EqualFold(vocation, "druid") || strings.EqualFold(vocation, "druids") {
126-
vocationid = "5"
127-
vocation = "druids"
128-
} else {
129-
vocation = "all"
130-
}
131-
} else {
132-
vocation = "all"
133-
}
109+
// Sanitize of vocation input
110+
vocationName, vocationid := TibiaDataVocationValidator(vocation)
134111

135112
// Getting data with TibiadataHTMLDataCollectorV3
136113
BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/community/?subtopic=highscores&world=" + TibiadataQueryEscapeStringV3(world) + "&category=" + TibiadataQueryEscapeStringV3(categoryid) + "&profession=" + TibiadataQueryEscapeStringV3(vocationid) + "&currentpage=400000000000000")
@@ -244,7 +221,7 @@ func TibiaHighscoresV3(c *gin.Context) {
244221
Highscores{
245222
World: strings.Title(strings.ToLower(world)),
246223
Category: category,
247-
Vocation: vocation,
224+
Vocation: vocationName,
248225
HighscoreAge: HighscoreAge,
249226
HighscoreList: HighscoreData,
250227
},

src/TibiaSpellsOverviewV3.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,19 @@ func TibiaSpellsOverviewV3(c *gin.Context) {
4747
Information Information `json:"information"`
4848
}
4949

50-
// Sanatize of vocation value
51-
vocation = strings.ToLower(vocation)
52-
if len(vocation) > 0 {
53-
if strings.EqualFold(vocation, "knight") || strings.EqualFold(vocation, "knights") {
54-
vocation = strings.Title("knight")
55-
} else if strings.EqualFold(vocation, "paladin") || strings.EqualFold(vocation, "paladins") {
56-
vocation = strings.Title("paladin")
57-
} else if strings.EqualFold(vocation, "sorcerer") || strings.EqualFold(vocation, "sorcerers") {
58-
vocation = strings.Title("sorcerer")
59-
} else if strings.EqualFold(vocation, "druid") || strings.EqualFold(vocation, "druids") {
60-
vocation = strings.Title("druid")
61-
} else {
62-
vocation = ""
63-
}
50+
// Sanitize of vocation input
51+
vocationName, _ := TibiaDataVocationValidator(vocation)
52+
if vocationName == "all" || vocationName == "none" {
53+
vocationName = ""
6454
} else {
65-
vocation = ""
55+
// removes the last letter (s) from the string (required for spells page)
56+
vocationName = strings.TrimSuffix(vocationName, "s")
57+
// setting string to first upper case
58+
vocationName = strings.Title(vocationName)
6659
}
6760

6861
// Getting data with TibiadataHTMLDataCollectorV3
69-
BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/library/?subtopic=spells&vocation=" + TibiadataQueryEscapeStringV3(vocation))
62+
BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/library/?subtopic=spells&vocation=" + TibiadataQueryEscapeStringV3(vocationName))
7063

7164
// Loading HTML data into ReaderHTML for goquery with NewReader
7265
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
@@ -143,15 +136,15 @@ func TibiaSpellsOverviewV3(c *gin.Context) {
143136
})
144137

145138
// adding readable SpellsVocationFilter field
146-
if vocation == "" {
147-
vocation = "none"
139+
if vocationName == "" {
140+
vocationName = "all"
148141
}
149142

150143
//
151144
// Build the data-blob
152145
jsonData := JSONData{
153146
Spells{
154-
SpellsVocationFilter: vocation,
147+
SpellsVocationFilter: vocationName,
155148
Spells: SpellsData,
156149
},
157150
Information{

src/webserver.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,33 @@ func getEnvAsInt(name string, defaultVal int) int {
486486
return defaultVal
487487
}
488488
*/
489+
490+
// TibiaDataVocationValidator func - return valid vocation string and vocation id
491+
func TibiaDataVocationValidator(vocation string) (string, string) {
492+
// defining return vars
493+
var vocationid string
494+
495+
switch strings.ToLower(vocation) {
496+
case "none":
497+
vocationid = "1"
498+
vocation = "none"
499+
case "knight", "knights":
500+
vocationid = "2"
501+
vocation = "knights"
502+
case "paladin", "paladins":
503+
vocationid = "3"
504+
vocation = "paladins"
505+
case "sorcerer", "sorcerers":
506+
vocationid = "4"
507+
vocation = "sorcerers"
508+
case "druid", "druids":
509+
vocationid = "5"
510+
vocation = "druids"
511+
default:
512+
vocationid = "0"
513+
vocation = "all"
514+
}
515+
516+
// returning vars
517+
return vocation, vocationid
518+
}

0 commit comments

Comments
 (0)