-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
171 lines (146 loc) · 3.54 KB
/
options.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ebird
import (
"net/url"
"strconv"
"strings"
)
type ClientOption func(*Client)
type RequestOption func(*RequestOptions)
type RequestOptions struct {
URLParams url.Values
}
func processOptions(options ...RequestOption) RequestOptions {
o := RequestOptions{
URLParams: url.Values{},
}
for _, opt := range options {
opt(&o)
}
return o
}
func Back(days int) RequestOption {
return func(o *RequestOptions) {
if days > 0 && days <= 30 {
o.URLParams.Set("back", strconv.Itoa(days))
}
}
}
func Cat(category string) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("cat", category)
}
}
func Delim(delimiter string) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("delim", delimiter)
}
}
func Dist(distance int) RequestOption {
return func(o *RequestOptions) {
if distance >= 0 && distance <= 500 {
o.URLParams.Set("dist", strconv.Itoa(distance))
}
}
}
func Fmt(format string) RequestOption {
return func(o *RequestOptions) {
if format == "csv" || format == "json" {
o.URLParams.Set("fmt", format)
}
}
}
func GroupNameLocale(locale string) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("groupNameLocale", locale)
}
}
func Hotspot(isHotspot bool) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("hotspot", strconv.FormatBool(isHotspot))
}
}
func IncludeProvisional(include bool) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("includeProvisional", strconv.FormatBool(include))
}
}
func Lat(latitude float64) RequestOption {
return func(o *RequestOptions) {
if latitude >= -90 && latitude <= 90 {
o.URLParams.Set("lat", strconv.FormatFloat(latitude, 'f', 2, 64))
}
}
}
func Lng(longitude float64) RequestOption {
return func(o *RequestOptions) {
if longitude >= -180 && longitude <= 180 {
o.URLParams.Set("lng", strconv.FormatFloat(longitude, 'f', 2, 64))
}
}
}
func Locale(locale string) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("locale", locale)
}
}
func MaxResults(max int) RequestOption {
return func(o *RequestOptions) {
if max > 0 && max <= 100 {
o.URLParams.Set("maxResults", strconv.Itoa(max))
}
}
}
func R(locations ...string) RequestOption {
return func(o *RequestOptions) {
if len(locations) > 0 && len(locations) <= 10 {
o.URLParams.Set("r", strings.Join(locations, ","))
}
}
}
func RankedBy(rankMethod string) RequestOption {
return func(o *RequestOptions) {
if rankMethod == "spp" || rankMethod == "cl" {
o.URLParams.Set("rankedBy", rankMethod)
}
}
}
func RegionNameFormat(format string) RequestOption {
return func(o *RequestOptions) {
validFormats := []string{"detailed", "detailednoqual", "full", "namequal", "nameonly", "revdetailed"}
if contains(validFormats, format) {
o.URLParams.Set("regionNameFormat", format)
}
}
}
func SortKey(key string) RequestOption {
return func(o *RequestOptions) {
if key == "obs_dt" || key == "creation_dt" {
o.URLParams.Set("sortKey", key)
}
}
}
func Species(speciesCodes ...string) RequestOption {
return func(o *RequestOptions) {
if len(speciesCodes) > 0 {
o.URLParams.Set("species", strings.Join(speciesCodes, ","))
}
}
}
func SppLocale(locale string) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("sppLocale", locale)
}
}
func Version(version string) RequestOption {
return func(o *RequestOptions) {
o.URLParams.Set("version", version)
}
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}