sfba_transit_api 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/sfba_transit_api.rb +0 -4
- data/lib/sfba_transit_api/client.rb +105 -25
- metadata +1 -5
- data/lib/sfba_transit_api/agency.rb +0 -30
- data/lib/sfba_transit_api/direction.rb +0 -27
- data/lib/sfba_transit_api/route.rb +0 -29
- data/lib/sfba_transit_api/stop.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35b19935386f244406d8ed85553ed9d4912936c5
|
4
|
+
data.tar.gz: 4170489d7650a5fb9cea855de0eebcec093bf554
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 628669216b20fe7ce44d0aeff9bd76b9a3d6b252570a0f14cbefbe046c5ade3ebcffbc2a479043f5af2b262a0cb5552ab7b4c79c9fb6c0f7c84c6edf2768f640
|
7
|
+
data.tar.gz: e3385d76eb53d1c2db0958c5a44d6a3bad6671978d7fbadb5514eb3abc0ffe6e1e6ceded45f4a2b978d6e40e1b3be1331d035907891108fcc8e936a38552fd51
|
data/lib/sfba_transit_api.rb
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
require_relative "./sfba_transit_api/stop"
|
2
|
-
require_relative "./sfba_transit_api/direction"
|
3
|
-
require_relative "./sfba_transit_api/route"
|
4
|
-
require_relative "./sfba_transit_api/agency"
|
5
1
|
require_relative "./sfba_transit_api/connection"
|
6
2
|
require_relative "./sfba_transit_api/client"
|
7
3
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/string'
|
2
|
+
|
1
3
|
module SFBATransitAPI
|
2
4
|
class Client
|
3
5
|
|
@@ -14,19 +16,19 @@ module SFBATransitAPI
|
|
14
16
|
def get_agencies
|
15
17
|
response = get(:get_agencies)
|
16
18
|
|
17
|
-
|
19
|
+
parse(response)
|
18
20
|
end
|
19
21
|
|
20
22
|
def get_routes_for_agency(agency_name)
|
21
23
|
response = get(:get_routes_for_agency, {agency_name: agency_name})
|
22
24
|
|
23
|
-
|
25
|
+
parse(response)
|
24
26
|
end
|
25
27
|
|
26
28
|
def get_routes_for_agencies(agency_names)
|
27
29
|
response = get(:get_routes_for_agencies, {agency_names: agency_names.join("|")})
|
28
30
|
|
29
|
-
|
31
|
+
parse(response)
|
30
32
|
end
|
31
33
|
|
32
34
|
def get_stops_for_route(route_info)
|
@@ -34,33 +36,13 @@ module SFBATransitAPI
|
|
34
36
|
|
35
37
|
response = get(:get_stops_for_route, {route_idf: route_idf})
|
36
38
|
|
37
|
-
|
38
|
-
agency.routes.map do |route|
|
39
|
-
route.stops
|
40
|
-
end.flatten
|
41
|
-
end.flatten
|
42
|
-
end
|
43
|
-
|
44
|
-
def get_stops_for_routes(route_infos)
|
45
|
-
route_idf = route_infos.map { |route_info| makeRouteIDF(route_info) }.join("|")
|
46
|
-
|
47
|
-
response = get(:get_stops_for_routes, {route_idf: route_idf})
|
48
|
-
|
49
|
-
Agency.parse(response).map do |agency|
|
50
|
-
agency.routes.map do |route|
|
51
|
-
route.stops
|
52
|
-
end.flatten
|
53
|
-
end.flatten
|
39
|
+
parse(response)
|
54
40
|
end
|
55
41
|
|
56
42
|
def get_next_departures_by_stop_code(stopcode)
|
57
43
|
response = get(:get_next_departures_by_stop_code, stopcode: stopcode)
|
58
44
|
|
59
|
-
|
60
|
-
agency.routes.map do |route|
|
61
|
-
route.stops
|
62
|
-
end.flatten
|
63
|
-
end.flatten
|
45
|
+
parse(response)
|
64
46
|
end
|
65
47
|
|
66
48
|
def makeRouteIDF(route_info)
|
@@ -72,5 +54,103 @@ module SFBATransitAPI
|
|
72
54
|
def get(method, options={})
|
73
55
|
self.connection.get(method, options)
|
74
56
|
end
|
57
|
+
|
58
|
+
def parse(doc)
|
59
|
+
agency_list_node = doc.at_xpath("//AgencyList")
|
60
|
+
if agency_list_node
|
61
|
+
parse_agencies(agency_list_node)
|
62
|
+
else
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse_agencies(agency_list_node)
|
68
|
+
agency_list_node.xpath("./Agency").map do |agency_node|
|
69
|
+
result = { "type" => "agency" }
|
70
|
+
|
71
|
+
agency_node.attributes.each do |key, val|
|
72
|
+
result[key.underscore] = to_boolean_if_possible(val.value)
|
73
|
+
end
|
74
|
+
|
75
|
+
route_list_node = agency_node.at_xpath("./RouteList")
|
76
|
+
if route_list_node
|
77
|
+
result["routes"] = parse_routes(route_list_node)
|
78
|
+
end
|
79
|
+
|
80
|
+
result
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def parse_routes(route_list_node)
|
85
|
+
route_list_node.xpath("./Route").map do |route_node|
|
86
|
+
result = { "type" => "route" }
|
87
|
+
|
88
|
+
route_node.attributes.each do |key, val|
|
89
|
+
result[key.underscore] = val.value
|
90
|
+
end
|
91
|
+
|
92
|
+
route_direction_list_node = route_node.at_xpath("./RouteDirectionList")
|
93
|
+
if route_direction_list_node
|
94
|
+
result["route_directions"] = parse_route_directions(route_direction_list_node)
|
95
|
+
else
|
96
|
+
stop_list_node = route_node.at_xpath("./StopList")
|
97
|
+
if stop_list_node
|
98
|
+
result["stops"] = parse_stops(stop_list_node)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
result
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def parse_route_directions(route_direction_list_node)
|
107
|
+
route_direction_list_node.xpath("./RouteDirection").map do |route_direction_node|
|
108
|
+
result = { "type" => "route_direction" }
|
109
|
+
|
110
|
+
route_direction_node.attributes.each do |key, val|
|
111
|
+
result[key.underscore] = val.value
|
112
|
+
end
|
113
|
+
|
114
|
+
stop_list_node = route_direction_node.at_xpath("./StopList")
|
115
|
+
if stop_list_node
|
116
|
+
result["stops"] = parse_stops(stop_list_node)
|
117
|
+
end
|
118
|
+
|
119
|
+
result
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def parse_stops(stop_list_node)
|
124
|
+
stop_list_node.xpath("./Stop").map do |stop_node|
|
125
|
+
result = { "type" => "stop" }
|
126
|
+
|
127
|
+
stop_node.attributes.each do |key, val|
|
128
|
+
result[key.underscore] = val.value
|
129
|
+
end
|
130
|
+
|
131
|
+
departure_time_list_node = stop_node.at_xpath("./DepartureTimeList")
|
132
|
+
if departure_time_list_node
|
133
|
+
result["departure_times"] = parse_departure_times(departure_time_list_node)
|
134
|
+
end
|
135
|
+
|
136
|
+
result
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def parse_departure_times(departure_time_list_node)
|
141
|
+
departure_time_list_node.xpath("./DepartureTime").map do |departure_time_node|
|
142
|
+
departure_time_node.text ? departure_time_node.text.to_i : nil
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def to_boolean_if_possible(value)
|
147
|
+
if value.downcase == "true"
|
148
|
+
true
|
149
|
+
elsif value.downcase == "false"
|
150
|
+
false
|
151
|
+
else
|
152
|
+
value
|
153
|
+
end
|
154
|
+
end
|
75
155
|
end
|
76
156
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sfba_transit_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank Liu
|
@@ -115,12 +115,8 @@ extensions: []
|
|
115
115
|
extra_rdoc_files: []
|
116
116
|
files:
|
117
117
|
- lib/sfba_transit_api.rb
|
118
|
-
- lib/sfba_transit_api/agency.rb
|
119
118
|
- lib/sfba_transit_api/client.rb
|
120
119
|
- lib/sfba_transit_api/connection.rb
|
121
|
-
- lib/sfba_transit_api/direction.rb
|
122
|
-
- lib/sfba_transit_api/route.rb
|
123
|
-
- lib/sfba_transit_api/stop.rb
|
124
120
|
homepage: https://github.com/gniquil/sfba_transit_api
|
125
121
|
licenses:
|
126
122
|
- MIT
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module SFBATransitAPI
|
2
|
-
class Agency
|
3
|
-
attr_accessor :name, :has_direction, :mode, :routes
|
4
|
-
|
5
|
-
def to_s
|
6
|
-
"#<SFBATransitAPI::Agency:#{object_id} @name=\"#{name}\", @has_direction=#{has_direction}, @mode=\"#{mode}\", routes.count=#{routes.count}>"
|
7
|
-
end
|
8
|
-
|
9
|
-
def has_direction=(has_direction)
|
10
|
-
if has_direction.is_a? String
|
11
|
-
@has_direction = has_direction.downcase == "true" ? true : false
|
12
|
-
else
|
13
|
-
@has_direction = has_direction ? true : false
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.parse(node)
|
18
|
-
node.xpath("//Agency").map do |agency_node|
|
19
|
-
agency = new
|
20
|
-
|
21
|
-
agency.name = agency_node["Name"]
|
22
|
-
agency.has_direction = agency_node["HasDirection"]
|
23
|
-
agency.mode = agency_node["Mode"]
|
24
|
-
agency.routes = Route.parse(agency_node, agency)
|
25
|
-
|
26
|
-
agency
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module SFBATransitAPI
|
2
|
-
class Direction
|
3
|
-
attr_accessor :name, :code, :route
|
4
|
-
|
5
|
-
def to_s
|
6
|
-
"#<SFBATransitAPI::Direction:#{object_id} @name=\"#{name}\", @code=\"#{code}\", @route=<SFBATransitAPI::Route:#{route.object_id}>>"
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.parse(route_node, route)
|
10
|
-
route_node.xpath(".//RouteDirection").map do |direction_node|
|
11
|
-
parse_direction_node(direction_node, route)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.parse_direction_node(direction_node, route)
|
16
|
-
return nil if direction_node.nil? or direction_node.name != 'RouteDirection'
|
17
|
-
|
18
|
-
direction = new
|
19
|
-
|
20
|
-
direction.route = route
|
21
|
-
direction.name = direction_node["Name"]
|
22
|
-
direction.code = direction_node["Code"]
|
23
|
-
|
24
|
-
direction
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module SFBATransitAPI
|
2
|
-
class Route
|
3
|
-
attr_accessor :name, :code, :directions, :stops, :agency
|
4
|
-
|
5
|
-
def has_direction
|
6
|
-
directions.count > 0
|
7
|
-
end
|
8
|
-
|
9
|
-
def to_s
|
10
|
-
"#<SFBATransitAPI::Route:#{object_id} @name=\"#{name}\", @code=\"#{code}\", @direction_codes=\"#{direction_codes}\", @agency=<SFBATransitAPI::Agency:#{agency.object_id}>, directions.count=#{directions.count}, stops.count=#{stops.count}>"
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.parse(agency_node, agency)
|
14
|
-
agency_node.xpath(".//Route").map do |route_node|
|
15
|
-
route = new
|
16
|
-
|
17
|
-
|
18
|
-
route.agency = agency
|
19
|
-
route.name = route_node["Name"]
|
20
|
-
route.code = route_node["Code"]
|
21
|
-
|
22
|
-
route.directions = Direction.parse(route_node, route)
|
23
|
-
route.stops = Stop.parse(route_node, route)
|
24
|
-
|
25
|
-
route
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module SFBATransitAPI
|
2
|
-
class Stop
|
3
|
-
attr_accessor :name, :code, :departure_times, :route, :direction
|
4
|
-
|
5
|
-
def to_s
|
6
|
-
"#<SFBATransitAPI::Stop:#{object_id} @name=\"#{name}\", @code=\"#{code}\", @direction=<SFBATransitAPI::Direction:#{direction.object_id}>, @route=<SFBATransitAPI::Route:#{route.object_id}>, departure_times=#{departure_times}>"
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.parse_departure_times(stop_node)
|
10
|
-
stop_node.xpath(".//DepartureTime").map do |departure_time_node|
|
11
|
-
departure_time_node.text ? departure_time_node.text.to_i : nil
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.parse(route_node, route)
|
16
|
-
route_node.xpath(".//Stop").map do |stop_node|
|
17
|
-
stop = new
|
18
|
-
|
19
|
-
stop.route = route
|
20
|
-
stop.direction = Direction.parse_direction_node(stop_node.parent.parent, route)
|
21
|
-
|
22
|
-
stop.name = stop_node["name"]
|
23
|
-
stop.code = stop_node["StopCode"]
|
24
|
-
stop.departure_times = parse_departure_times(stop_node)
|
25
|
-
|
26
|
-
stop
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|