-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathforecast.go
75 lines (62 loc) · 1.28 KB
/
forecast.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
package surflinef
import (
"encoding/json"
"fmt"
"net/url"
)
// Forecast is the root JSON struct for forecast data.
type Forecast struct {
Analysis
Tide
Surf
Weather
Wind
}
type iForecast struct {
iAnalysis `json:"Analysis"`
Tide `json:"Tide,omitempty"`
Surf `json:"Surf,omitempty"`
Weather `json:"Weather,omitempty"`
Wind `json:"Wind,omitempty"`
}
// GetForecast grabs a Forecast from surfline with the provided Sub Region ID
// and Query string.
func (c *Client) GetForecast(srID string, query string) (Forecast, error) {
p := fmt.Sprintf("%s/%s?%s", c.BaseURL, srID, query)
u, err := url.Parse(p)
if err != nil {
return Forecast{}, err
}
resp, err := c.get(u)
if err != nil {
return Forecast{}, err
}
defer resp.Body.Close()
var forecast Forecast
err = json.NewDecoder(resp.Body).Decode(&forecast)
if err != nil {
return Forecast{}, err
}
return forecast, nil
}
// UnmarshalJSON is a custom unmarshaller for Forecast.
func (f *Forecast) UnmarshalJSON(data []byte) error {
var iF iForecast
err := json.Unmarshal(data, &iF)
if err != nil {
return err
}
*f = iF.forecast()
return nil
}
func (iF iForecast) forecast() Forecast {
iA := iF.iAnalysis
a := iA.analysis()
return Forecast{
a,
iF.Tide,
iF.Surf,
iF.Weather,
iF.Wind,
}
}