octranspo_fetch 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/octranspo_fetch.rb +37 -14
  3. metadata +6 -13
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjgxNzZjYzdkZTVkNTViMmZhYmMyOTExNmIyOGM1ZThmN2Q0OTg4Nw==
5
+ data.tar.gz: !binary |-
6
+ MzVmZTQxYTJjY2FkZjI1NWQ5YThhYzI4ZTA4MzE5ODhkYmRlNzhmYg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjA3Y2YyZjRjNGVlMzI1MDAyYjU1MGYzNDA1ODZmYzU3NTc3NGU4ZTFkNGNh
10
+ YWJjOGNlYjAxMzcxZDk0YzlmNGQ0MzE2NzYzZDkyZjY2NDFkODBkOWE2ZTY1
11
+ MzJjM2Y1ZGY5ZDY3OWJlYzY0MDM0Y2Y2ZGY2ZjkxMGFlY2YwNWU=
12
+ data.tar.gz: !binary |-
13
+ MTdhOWFjNzdiZjAyNDE4OTg0ZDllMmU1ZDlhYjNkZjc1ZTUxMGI3YjQxNzY0
14
+ OWFjNTdkOGExMWJkMGU5YzBkODYzNzRjMmExNDlmMDQ5MGU1NTI3ZDYzZjUx
15
+ NzE4MTZlYWU5YzA1MWQzMTZiMDllMzRlYTBiY2RiOTg1MDQ3YTE=
@@ -90,43 +90,64 @@ class OCTranspo
90
90
  #
91
91
  # Arguments:
92
92
  # stop: (String) The stop number.
93
- # route_no: (String) The route number.
93
+ # route_no: (String) The route number, optional. If nil (default), get all routes.
94
94
  # options[:max_cache_time]: (Integer) Maximum cache age, in seconds. If cached data is
95
95
  # available and is newer than this, then the cached value will be returned. Defaults
96
96
  # to five minutes.
97
97
  #
98
- def get_next_trips_for_stop(stop, route_no, options={})
98
+ def get_next_trips_for_stop(stop, route_no=nil, options={})
99
99
  max_cache_time = (options[:max_cache_time] or 60*5)
100
100
 
101
101
  # Return result from cache, if available
102
- cache_key = "#{stop}-#{route_no}"
102
+ cache_key = "#{stop}-#{route_no.to_s}"
103
103
  cached_result = @next_trips_cache[cache_key]
104
104
  if !cached_result.nil? and ((cached_result[:time] + max_cache_time) > Time.now.to_i)
105
105
  @cache_hits += 1
106
106
  return adjust_cached_trip_times(cached_result[:next_trips])
107
107
  end
108
108
  @cache_misses += 1
109
+ if route_no.nil?
110
+ parts = {
111
+ :method => "GetNextTripsForStopAllRoutes",
112
+ :args => "stopNo=#{stop}",
113
+ :alt_resource => "GetRouteSummaryForStop",
114
+ :stop_description => "t:StopDescription",
115
+ :route_xpath => 't:Routes/t:Route',
116
+ :route_error => "Error for route",
117
+ :route_label => "t:RouteHeading"
118
+ }
119
+ else
120
+ parts = {
121
+ :method => "GetNextTripsForStop",
122
+ :args => "stopNo=#{stop}&routeNo=#{route_no}",
123
+ :alt_resource => nil,
124
+ :stop_description => "t:StopLabel",
125
+ :route_xpath => 't:Route/t:RouteDirection',
126
+ :route_error => "Error for route: #{route_no}",
127
+ :route_label => "t:RouteLabel"
128
+ }
129
+ end
109
130
 
110
- xresult = fetch "GetNextTripsForStop", "stopNo=#{stop}&routeNo=#{route_no}"
131
+ xresult = fetch parts[:method], parts[:args], parts[:alt_resource]
111
132
 
112
133
  result = {
113
134
  stop: get_value(xresult, "t:StopNo"),
114
- stop_description: get_value(xresult, "t:StopLabel"),
135
+ stop_description: get_value(xresult, parts[:stop_description]),
115
136
  time: Time.now,
116
137
  routes: []
117
138
  }
118
139
 
119
140
  found_data = false
120
141
 
121
- xresult.xpath('t:Route/t:RouteDirection', OCT_NS).each do |route|
122
- get_error(route, "Error for route: #{route_no}")
142
+ xresult.xpath(parts[:route_xpath], OCT_NS).each do |route|
143
+ get_error(route, parts[:route_error])
123
144
 
124
145
  route_obj = {
125
146
  cached: false,
126
147
  route: get_value(route, "t:RouteNo"),
127
- route_label: get_value(route, "t:RouteLabel"),
148
+ route_label: get_value(route, parts[:route_label]),
128
149
  direction: get_value(route, "t:Direction"),
129
- request_processing_time: Time.parse(get_value(route, "t:RequestProcessingTime")),
150
+ request_processing_time: route_no.nil? ? nil : Time.parse(get_value(route, "t:RequestProcessingTime")),
130
151
  trips: []
131
152
  }
132
153
  route.xpath('t:Trips/t:Trip', OCT_NS).each do |trip|
@@ -217,20 +238,22 @@ class OCTranspo
217
238
 
218
239
  private
219
240
 
220
- BASE_URL = "https://api.octranspo1.com/v1.1"
241
+ BASE_URL = "https://api.octranspo1.com/v1.2"
221
242
  OCT_NS = {'oct' => 'http://octranspo.com', 't' => 'http://tempuri.org/'}
222
243
  NEXT_TRIPS_CACHE_SIZE = 100
223
244
  ROUTE_CACHE_SIZE = 100
224
245
 
225
246
  # Fetch and parse some data from the OC-Transpo API. Returns a nokogiri object for
226
247
  # the Result within the XML document.
227
- def fetch(resource, params)
248
+ def fetch(resource, params, alt_resource = nil)
228
249
  @api_calls = (@api_calls + 1)
229
250
  response = RestClient.post("#{BASE_URL}/#{resource}",
230
251
  "appID=#{@app_id}&apiKey=#{@app_key}&#{params}")
231
-
252
+ if alt_resource.nil?
253
+ alt_resource = resource
254
+ end
232
255
  doc = Nokogiri::XML(response.body)
233
- xresult = doc.xpath("//oct:#{resource}Result", OCT_NS)
256
+ xresult = doc.xpath("//oct:#{alt_resource}Result", OCT_NS)
234
257
  if xresult.length == 0
235
258
  raise "Error: No reply for #{resource}"
236
259
  end
@@ -316,4 +339,4 @@ class OCTranspo
316
339
  return cached_routes
317
340
  end
318
341
 
319
- end
342
+ end
metadata CHANGED
@@ -1,20 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octranspo_fetch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jason Walton
8
+ - Kevin Ross
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-24 00:00:00.000000000 Z
12
+ date: 2016-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
18
  - - ! '>='
20
19
  - !ruby/object:Gem::Version
@@ -22,7 +21,6 @@ dependencies:
22
21
  type: :runtime
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
25
  - - ! '>='
28
26
  - !ruby/object:Gem::Version
@@ -30,7 +28,6 @@ dependencies:
30
28
  - !ruby/object:Gem::Dependency
31
29
  name: rest-client
32
30
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
31
  requirements:
35
32
  - - ! '>='
36
33
  - !ruby/object:Gem::Version
@@ -38,7 +35,6 @@ dependencies:
38
35
  type: :runtime
39
36
  prerelease: false
40
37
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
38
  requirements:
43
39
  - - ! '>='
44
40
  - !ruby/object:Gem::Version
@@ -46,7 +42,6 @@ dependencies:
46
42
  - !ruby/object:Gem::Dependency
47
43
  name: lru_redux
48
44
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
45
  requirements:
51
46
  - - ! '>='
52
47
  - !ruby/object:Gem::Version
@@ -54,7 +49,6 @@ dependencies:
54
49
  type: :runtime
55
50
  prerelease: false
56
51
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
52
  requirements:
59
53
  - - ! '>='
60
54
  - !ruby/object:Gem::Version
@@ -69,26 +63,25 @@ files:
69
63
  homepage: http://rubygems.org/gems/octranspo_fetch
70
64
  licenses:
71
65
  - MIT
66
+ metadata: {}
72
67
  post_install_message:
73
68
  rdoc_options: []
74
69
  require_paths:
75
70
  - lib
76
71
  required_ruby_version: !ruby/object:Gem::Requirement
77
- none: false
78
72
  requirements:
79
73
  - - ! '>='
80
74
  - !ruby/object:Gem::Version
81
75
  version: '0'
82
76
  required_rubygems_version: !ruby/object:Gem::Requirement
83
- none: false
84
77
  requirements:
85
78
  - - ! '>='
86
79
  - !ruby/object:Gem::Version
87
80
  version: '0'
88
81
  requirements: []
89
82
  rubyforge_project:
90
- rubygems_version: 1.8.23
83
+ rubygems_version: 2.4.8
91
84
  signing_key:
92
- specification_version: 3
85
+ specification_version: 4
93
86
  summary: Fetch data from OC Tranpo API
94
87
  test_files: []