-
Notifications
You must be signed in to change notification settings - Fork 5
/
forecast.go
executable file
·143 lines (126 loc) · 3.67 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
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
///usr/bin/env go run "$0" "$@" ; exit "$?"
package main
import (
"errors"
"flag"
"fmt"
logger "log"
"net/http"
"os"
"time"
)
const (
//replace with your personal information as desired
key = "32772f4b37c5a08eb4488a2ce79155bd"
latitude = "42.2797" // Ann Arbor Latitude
longitude = "-83.7369" // Ann Arbor Longitude
layoutUS = "January 2, 2006"
)
func main() {
log := logger.New(os.Stderr, "", 0)
forecast := getFlag()
fc, _ := GetForecast(key, latitude, longitude)
output(fc, forecast, log)
}
func getFlag() bool {
flag.Usage = help
forecastPtr := flag.Bool("forecast", false, "Show 8 day forecast")
flag.Parse()
return *forecastPtr
}
func help() {
fmt.Printf("Usage: forecast [flags]\n")
flag.PrintDefaults()
}
// GetForecast - This is a "glue" function. It takes all of the more testable,
// behavioral functions and "glues" them together without any other inherent behavior
func GetForecast(key, latitude, longitude string) (Forecast, error) {
url := GenerateURL(key, latitude, longitude)
weatherClient := http.Client{}
req := BuildRequest(url)
response, _ := weatherClient.Do(req)
if response.StatusCode != http.StatusOK {
return Forecast{}, errors.New("forbidden - most likely due to invalid token")
}
body := GetBody(response)
return ParseWeatherResponse(body)
}
// GenerateURL will construct the DarkSky API url from components
func GenerateURL(key, latitude, longitude string) string {
_, _, _ = key, latitude, longitude // TODO: Use these parameters
return ""
}
// BuildRequest will build a new client and request with the proper headers
func BuildRequest(url string) *http.Request {
_ = url // TODO: Use this parameters
return nil
}
// GetBody will take an httpResponse and extract the body as a string
func GetBody(res *http.Response) string {
_ = res // TODO: Use this parameter
return ""
}
// ParseWeatherResponse will parse the DarkSky service response into a Forecast
func ParseWeatherResponse(jsonData string) (Forecast, error) {
_ = jsonData // TODO: Use this parameter
panic("Stub")
}
func output(fc Forecast, forecast bool, log *logger.Logger) {
cur := fc.Currently
daily := fc.Daily
if !forecast {
curTime := time.Unix(cur.Time, 0).Format(time.RFC822)
curWeatherFormat := `
Current Weather: %s
Summary %s
Temperature %f
Humidity %f
WindSpeed %f
WindBearing %f
`
log.Printf(curWeatherFormat, curTime, cur.Summary, cur.Temperature, cur.Humidity, cur.WindSpeed, cur.WindBearing)
} else {
var dailys string
for _, v := range daily.Data {
dTime := time.Unix(v.Time, 0).Format(layoutUS)
dailyForecastFormat := `
Weather for %s
Summary %s
Temperature Min %f
Temperature Max %f
Humidity %f
WindSpeed %f
WindBearing %f
`
dailys += fmt.Sprintf(dailyForecastFormat, dTime, v.Summary, v.TemperatureMin, v.TemperatureMax, v.Humidity, v.WindSpeed, v.WindBearing)
}
log.Println(dailys)
}
}
// Forecast is just the parts of the response we care about. Current and Daily
type Forecast struct {
Currently CurrentConditions
Daily WeatherDaily
}
// CurrentConditions represents the current weather observed weather conditions
type CurrentConditions struct {
Time int64
Summary string
Temperature float32
Humidity float32
WindSpeed float32
WindBearing float32
}
// WeatherDaily represents the daily forecast for the next several days
type WeatherDaily struct {
Summary string
Data []struct {
Time int64
Summary string
TemperatureMin float32
TemperatureMax float32
Humidity float32
WindSpeed float32
WindBearing float32
}
}