-
Notifications
You must be signed in to change notification settings - Fork 0
/
citibike.cr
59 lines (48 loc) · 1.78 KB
/
citibike.cr
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
require "http/client"
require "./citibike/*"
# A simple wrapper over the NYC Citi Bike [GBFS](https://github.com/NABSA/gbfs/blob/master/gbfs.md)
# feed. Uses the feed's TTL to cache requests.
module CitiBike
extend self
# The base URL for all NYC Citibike GBFS requests.
BASE_URL = "https://gbfs.citibikenyc.com/gbfs/en"
@@system_regions : Envelope(SystemRegions)?
@@system_information : Envelope(SystemInformation)?
@@station_information : Envelope(StationInformation)?
@@system_alerts : Envelope(SystemAlerts)?
@@station_status : Envelope(StationStatus)?
private def fetch(endpoint)
resp = HTTP::Client.get("#{BASE_URL}/#{endpoint}.json")
resp.body
end
private macro fetch_if_stale(response, klass)
if @@{{response.id}}.nil? || @@{{response.id}}.not_nil!.stale?
@@{{response.id}} = Envelope({{klass.id}}).from_json fetch("{{response.id}}")
end
end
# Returns an `Array` of all `SystemRegions::Region`s.
def system_regions
fetch_if_stale system_regions, SystemRegions
@@system_regions.not_nil!.data.regions
end
# Returns a `SystemInformation` containing metadata about the bikeshare.
def system_information
fetch_if_stale system_information, SystemInformation
@@system_information.not_nil!.data
end
# Returns an `Array` of `StationInformation::Station`s.
def station_information
fetch_if_stale station_information, StationInformation
@@station_information.not_nil!.data.stations
end
# Returns an `Array` of `SystemAlerts::Alert`s.
def system_alerts
fetch_if_stale system_alerts, SystemAlerts
@@system_alerts.not_nil!.data.alerts
end
# Returns an `Array` of `StationStatus::Status`es.
def station_status
fetch_if_stale station_status, StationStatus
@@station_status.not_nil!.data.stations
end
end