onebusaway_ruby 0.1.0

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 +7 -0
  2. data/lib/onebusaway.rb +199 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 981ce4965e29312a6dd62995050412b8e4ee7b3b
4
+ data.tar.gz: c14e8f0395a090f90e3b8a0383bea5abde041521
5
+ SHA512:
6
+ metadata.gz: a28c07c051c9d844a16b9f3ec84e08d5ddf3ec2562f7cd80e38d097d9827ab463c26c6980312b7ad4c7cdb8d9d995cd562526191e1de7ae4ea3ac907af52c20a
7
+ data.tar.gz: 44712d5395dbbfab6054ec30ca8ff019e731f0f0cc11b7a62d74014d7fa41cc2a39088bad653c918940c75321c515389844663c019316feeb3a65d3d18e42217
@@ -0,0 +1,199 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'httparty'
4
+ require_relative 'onebusaway_data'
5
+
6
+ class OneBusAway
7
+ include HTTParty
8
+
9
+ def initialize(key, url)
10
+ raise "invalid url format" unless url =~ URI::regexp
11
+ self.class.base_uri(url)
12
+ @api_key = key
13
+ raise "cannot connect" unless valid_connection?
14
+ end
15
+
16
+ def valid_connection?
17
+ begin
18
+ response = get("/current-time.json?key=#{@api_key}")
19
+ true
20
+ rescue
21
+ return false
22
+ end
23
+ end
24
+
25
+ def get(url)
26
+ response = self.class.get(url)
27
+ if response.success?
28
+ raise response['text'] unless response['code'] == 200
29
+ response
30
+ else
31
+ raise response.response
32
+ end
33
+ end
34
+
35
+ # lists all supported agencies along with the center of their coverage area
36
+ # @return [Hash<String, Agency>] a hash of all agencies with their agency ids as keys
37
+ def agencies_with_coverage
38
+ response = get("/agencies-with-coverage.json?key=#{@api_key}")
39
+ response['data'].reduce({}) do |hash, agency|
40
+ a = Agency.new.extend(AgencyRepresenter).from_json(agency['agency'].to_json)
41
+ hash[a.agency_id] = a; hash
42
+ end
43
+ end
44
+
45
+ # gets details for a specific agency
46
+ # @return [Agency]
47
+ # @param [Hash] opts
48
+ # @option opts [String] :agency_id the id of the agency to be returned
49
+ def agency(opts)
50
+ raise if opts[:agency_id].nil?
51
+ response = get("/agency/#{opts[:agency_id]}.json?key=#{@api_key}")
52
+ Agency.new.extend(AgencyRepresenter).from_json(response['data']['entry'].to_json)
53
+ end
54
+
55
+ # arrival_and_departure_for_stop - details about a specific arrival/departure at a stop
56
+ def arrival_and_departure_for_stop(stop_id, trip_id, service_date)
57
+ response = get("/arrival-and-departure-for-stop/#{stop_id}.json?key=#{@api_key}&tripId=#{trip_id}&serviceDate=#{service_date}")
58
+ end
59
+
60
+ # arrivals_and_departures_for_stop - get current arrivals and departures for a stop
61
+ def arrivals_and_departures_for_stop(stop_id)
62
+ response = get("/arrivals-and-departures-for-stop/#{stop_id}.json?key=#{@api_key}")
63
+ response['data']['arrivalsAndDepartures'].reduce({}) do |hash, ad|
64
+ r = ArrivalAndDeparture.new.extend(ArrivalAndDepartureRepresenter).from_json(ad.to_json)
65
+ hash[r.route_id] = r
66
+ hash
67
+ end
68
+ end
69
+
70
+ # cancel_alarm - cancel a registered alarm
71
+ def cancel_alarm(alarm_id)
72
+ response = get("/cancel-alarm/#{alarm_id}.json?key=#{@api_key}")
73
+ end
74
+
75
+ # retrieves the current system time
76
+ #
77
+ # @return [DateTime]
78
+ def current_time
79
+ response = get("/current-time.json?key=#{@api_key}")
80
+ Time.at(response["data"]["time"] / 1000).to_datetime
81
+ end
82
+
83
+ # register_alarm_for_arrival_and_departure_at_stop - register an alarm for an arrival-departure event
84
+ def register_alarm_for_arrival_and_departure_at_stop(stop_id, trip_id, service_date, vehicle_id, stop_sequence, alarm_time_offset, callback_url)
85
+ response = get("/register-alarm-for-arrival-and-departure-at-stop/#{stop_id}.json?key=#{@api_key}&tripId=#{trip_id}&serviceDate=#{service_date}&vehicleId=#{vehicle_id}&stopSequence=#{stop_sequence}&alarmTimeOffset=#{alarm_time_offset}&url=#{callback_url}")
86
+ end
87
+
88
+ # route_ids_for_agency - get a list of all route ids for an agency
89
+ def route_ids_for_agency(agency)
90
+ response = get("/route-ids-for-agency/#{agency}.json?key=#{@api_key}")
91
+ response["data"]["list"]
92
+ end
93
+
94
+ # route - get details for a specific route
95
+ def route(route_id)
96
+ response = get("/route/#{route_id}.json?key=#{@api_key}")
97
+ Route.new.extend(RouteRepresenter).from_json(response['data'].to_json)
98
+ end
99
+
100
+ # routes_for_agency - get a list of all routes for an agency
101
+ def routes_for_agency(agency)
102
+ response = get("/routes-for-agency/#{agency}.json?key=#{@api_key}")
103
+ response['data']['list'].reduce({}) do |hash, route|
104
+ r = Route.new.extend(RouteRepresenter).from_json(route.to_json)
105
+ hash[r.route_id] = r
106
+ hash
107
+ end
108
+ end
109
+
110
+ # routes_for_location - search for routes near a location, optionally by route name
111
+ def routes_for_location(lat, lon)
112
+ response = get("/routes-for-location.json?key=#{@api_key}&lat=#{lat}&lon=#{lon}")
113
+ response['data']['routes'].reduce({}) do |hash, route|
114
+ r = Route.new.extend(RouteRepresenter).from_json(route.to_json)
115
+ hash[r.route_id] = r
116
+ hash
117
+ end
118
+ end
119
+
120
+ # schedule_for_stop - get the full schedule for a stop on a particular day
121
+ def schedule_for_stop(stop)
122
+ response = get("/schedule-for-stop/#{stop}.json?key=#{@api_key}")
123
+ StopSchedule.new.extend(StopScheduleRepresenter).from_json(response['data']['entry'].to_json)
124
+ end
125
+
126
+ # shape - get details for a specific shape (polyline drawn on a map)
127
+ def shape(shape_id)
128
+ response = get("/shape/#{shape_id}.json?key=#{@api_key}")
129
+ end
130
+
131
+ # stop_ids_for_agency - get a list of all stops for an agency
132
+ def stop_ids_for_agency(agency)
133
+ response = get("/stop-ids-for-agency/#{agency}.json?key=#{@api_key}")
134
+ response["data"]["list"]
135
+ end
136
+
137
+ # stop - get details for a specific stop
138
+ def stop(stop_id)
139
+ response = get("/stop/#{stop_id}.json?key=#{@api_key}")
140
+ Stop.new.extend(StopRepresenter).from_json(response['data'].to_json)
141
+ end
142
+
143
+ # stops_for_location - search for stops near a location, optionally by stop code
144
+ def stops_for_location(lat, lon)
145
+ response = get("/stops-for-location.json?key=#{@api_key}&lat=#{lat}&lon=#{lon}")
146
+ response['data']['stops'].reduce({}) do |hash, stop|
147
+ s = Stop.new.extend(StopRepresenter).from_json(stop.to_json)
148
+ hash[s.stop_id] = s
149
+ hash
150
+ end
151
+ end
152
+
153
+ # stops_for_route - get the set of stops and paths of travel for a particular route
154
+ def stops_for_route(route_id)
155
+ response = get("/stops-for-route/#{route_id}.json?key=#{@api_key}&version=2")
156
+ response['data']['references']['stops'].reduce({}) do |hash, stop|
157
+ s = Stop.new.extend(StopRepresenter).from_json(stop.to_json)
158
+ hash[s.stop_id] = s
159
+ hash
160
+ end
161
+ end
162
+
163
+ # trip_details - get extended details for a specific trip
164
+ def trip_details(trip_id)
165
+ response = get("/trip-details/#{trip_id}.json?key=#{@api_key}")
166
+ end
167
+
168
+ # trip_for_vehicle - get extended trip details for current trip of a specific transit vehicle
169
+ def trip_for_vehicle(vehicle_id)
170
+ response = get("/trip-for-vehicle/#{vehicle_id}.json?key=#{@api_key}")
171
+ end
172
+
173
+ # trip - get details for a specific trip
174
+ def trip(trip_id)
175
+ response = get("/trip/#{trip_id}.json?key=#{@api_key}")
176
+ end
177
+
178
+ # trips_for_location - get active trips near a location
179
+ def trips_for_location(lon, lat)
180
+ response = get("/trips-for-location.json?key=#{@api_key}&lat=#{lat}&lon=#{lon}")
181
+ end
182
+
183
+ # trips_for_route - get active trips for a route
184
+ def trips_for_route(route_id)
185
+ response = get("/trips-for-route/#{route_id}.json?key=#{@api_key}")
186
+ end
187
+
188
+ # vehicles_for_agency - get active vehicles for an agency
189
+ def vehicles_for_agency(agency_id)
190
+ response = get("/vehicles-for-agency/#{agency_id}.json?key=#{@api_key}")
191
+ response['data']['list'].reduce({}) do |hash, vehicle|
192
+ v = Vehicle.new.extend(VehicleRepresenter).from_json(vehicle.to_json)
193
+ hash[v.vehicle_id] = v
194
+ hash
195
+ end
196
+ end
197
+
198
+ private :get, :valid_connection?
199
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onebusaway_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Faulkenberry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A OneBusAway Ruby library built with HTTParty
14
+ email: jon.faulkenberry@outlook.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/onebusaway.rb
20
+ homepage: http://rubygems.org/gems/onebusaway_ruby
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.1.10
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A OneBusAway Ruby library
44
+ test_files: []
45
+ has_rdoc: