forked from ardanlabs/gotraining
/
exercise1.go
108 lines (87 loc) · 2.68 KB
/
exercise1.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go build
// ./exercise1
// Sample program to show how to cache data from an API, and then
// use that data in analyzing a dataset.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
cache "github.com/patrickmn/go-cache"
)
const (
// statusURL provides an explanation of Citibike station statuses.
statusURL = "https://feeds.citibikenyc.com/stations/status.json"
// currentStatusURL provides the current Citibike station statuses.
currentStatusURL = "https://feeds.citibikenyc.com/stations/stations.json"
)
// stationData is used to unmarshal the JSON document returned form citiBikeURL.
type stationData struct {
ExecutionTime string `json:"executionTime"`
StationBeanList []station `json:"stationBeanList"`
}
// station is used to unmarshal each of the station documents in stationData.
type station struct {
ID int `json:"id"`
StationName string `json:"stationName"`
StatusKey int `json:"statusKey"`
}
func main() {
// Get the JSON status codes from the statusURL.
response, err := http.Get(statusURL)
if err != nil {
log.Fatal(err)
}
// Defer response body close.
defer response.Body.Close()
// Read the body of the response into []byte.
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
// Unmarshal the JSON data into a map. Here the keys are the
// status codes and the values are the meanings of the codes.
var statuses map[string]string
if err := json.Unmarshal(body, &statuses); err != nil {
log.Fatal(err)
return
}
// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 30 seconds
c := cache.New(5*time.Minute, 30*time.Second)
// Put the status codes and meanings into the cache.
for k, v := range statuses {
c.Set(k, v, cache.DefaultExpiration)
}
// Get the current CitiBike station statuses.
response, err = http.Get(currentStatusURL)
if err != nil {
log.Fatal(err)
}
// Read the body of the response into []byte.
body, err = ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
// Declare a variable of type stationData.
var sd stationData
// Unmarshal the JSON data into the variable.
if err := json.Unmarshal(body, &sd); err != nil {
log.Fatal(err)
return
}
// Determine which stations are not in service.
for _, station := range sd.StationBeanList {
// Get the respective code from the Cache.
v, found := c.Get(strconv.Itoa(station.StatusKey))
if found && v == "Not In Service" {
fmt.Printf("%s station not in service\n", station.StationName)
}
}
}