-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguild.go
108 lines (92 loc) · 2.9 KB
/
guild.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package raiderio
import (
"encoding/json"
"errors"
)
// GuildQuery is a struct that represents the query parameters
// sent for a guild profile request
// Supports optional request fields: members, raid_progression, raid_rankings
type GuildQuery struct {
Region *Region
Realm string
Name string
Members bool
RaidProgression bool
RaidRankings bool
fields []string
}
// Guild is a struct that represents the response from
// a guild profile request
type Guild struct {
Name string `json:"name"`
Faction string `json:"faction"`
Region string `json:"region"`
Realm string `json:"realm"`
LastCrawledAt string `json:"last_crawled_at"`
ProfileUrl string `json:"profile_url"`
Members []Member `json:"members"`
RaidProgression GuildRaidProgression `json:"raid_progression"`
RaidRankings map[string]GuildRaidRanking `json:"raid_rankings"`
}
// Member is a struct that represents a member of a guild
// in a guild profile response
type Member struct {
Rank int `json:"rank"`
Character Character `json:"character"`
}
// RaidProgression is a struct that contains the raid progression of a guild
// in a guild profile response
// Currently supports Dragonflight raids
type GuildRaidProgression struct {
Amirdrassil RaidProgression `json:"amirdrassil-amirdrassil-the-dreams-hope"`
Aberrus RaidProgression `json:"aberrus-the-shadowed-crucible"`
VaultOfTheIncarnates RaidProgression `json:"vault-of-the-incarnates"`
}
// createGuildQuery creates and validates a GuildQuery struct
// It returns an error if any of the required parameters are empty
// or if the fields are invalid
func createGuildQuery(gq *GuildQuery) error {
if gq.Region == nil {
return ErrInvalidRegion
}
if gq.Realm == "" {
return ErrInvalidRealm
}
if gq.Name == "" {
return ErrInvalidGuildName
}
if gq.Members {
gq.fields = append(gq.fields, "members")
}
if gq.RaidProgression {
gq.fields = append(gq.fields, "raid_progression")
}
if gq.RaidRankings {
gq.fields = append(gq.fields, "raid_rankings")
}
return nil
}
func (g *Guild) GetGuildRaidRankBySlug(slug string) (*GuildRaidRanking, error) {
if g.RaidRankings == nil {
return nil, errors.New("guild raid rankings " + ErrFieldMissing.Error())
}
gr, ok := g.RaidRankings[slug]
if !ok {
return nil, ErrInvalidRaid
}
return &gr, nil
}
func unmarshalGuild(body []byte) (*Guild, error) {
var profile Guild
err := json.Unmarshal(body, &profile)
if err != nil {
return nil, errors.New("error unmarshalling guild profile")
}
for k := range profile.RaidRankings {
if entry, ok := profile.RaidRankings[k]; ok {
entry.RaidSlug = k
profile.RaidRankings[k] = entry
}
}
return &profile, nil
}