cta-api 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,8 +10,8 @@ $ gem install cta-api
10
10
 
11
11
  This gem allows you to access the Chicago Transit Authority API via Ruby. You can track the real-time locations of all public transportation vehicles, including buses and trains. You can obtain API keys from the CTA website:
12
12
 
13
- http://www.transitchicago.com/developers/bustracker.aspx
14
- http://www.transitchicago.com/developers/traintracker.aspx
13
+ * http://www.transitchicago.com/developers/bustracker.aspx
14
+ * http://www.transitchicago.com/developers/traintracker.aspx
15
15
 
16
16
  ## Bus Tracker
17
17
 
@@ -21,30 +21,30 @@ http://www.transitchicago.com/developers/traintracker.aspx
21
21
  require 'cta-api'
22
22
 
23
23
  key = "XXXXXXXXXXXXXXXXXXXXXXXXX"
24
- @tracker = CTA::TrainTracker.new(key)
24
+ CTA::BusTracker.key = key
25
25
  ```
26
26
 
27
27
  ### Find Routes and Stops
28
28
 
29
29
  ``` ruby
30
30
  # list all available routes
31
- @tracker.routes
31
+ CTA::BusTracker.routes
32
32
 
33
33
  # gets the available directions for the specified route (north, south, etc.)
34
- @tracker.route_directions("50")
34
+ CTA::BusTracker.directions :rt => 50
35
35
 
36
36
  # list all stops that belong to a particular route
37
- @tracker.stops("50", :north)
37
+ CTA::BusTracker.stops :rt => 50, :north
38
38
  ```
39
39
 
40
40
  ### Find Vehicles
41
41
 
42
42
  ``` ruby
43
43
  # returns an array of vehicles that travel the given routes
44
- @tracker.vehicles_by_routes(["50", "52A"])
44
+ CTA::BusTracker.vehicles :rt => 50
45
45
 
46
46
  # returns an array of vehicles with the given vehicle ids
47
- @tracker.vehicles_by_ids(["1782", "1419", "1773"])
47
+ CTA::BusTracker.vehicles :vid => ["1782", "1419", "1773"]
48
48
  ```
49
49
 
50
50
  ### Get Predicted Arrival Times
@@ -52,15 +52,15 @@ key = "XXXXXXXXXXXXXXXXXXXXXXXXX"
52
52
  ``` ruby
53
53
  # get arrival times for a list of stop ids
54
54
  # note that the second argument is optional
55
- @tracker.predictions_by_stop_ids(["8751", "8752"], "50")
55
+ CTA::BusTracker.predictions :rt => 50, :stpid => 8923
56
56
 
57
57
  # get arrival times for a list of vehicle ids
58
- @tracker.predictions_by_vehicle_ids(["1782", "1419", "1773"])
58
+ CTA::BusTracker.predictions :vid => ["1782", "1419", "1773"]
59
59
  ```
60
60
 
61
61
  ### Get System Time
62
62
  ``` ruby
63
- @tracker.time
63
+ CTA::BusTracker.time
64
64
  ```
65
65
 
66
66
  ## Train Tracker
@@ -71,21 +71,21 @@ key = "XXXXXXXXXXXXXXXXXXXXXXXXX"
71
71
  require 'cta-api'
72
72
 
73
73
  key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
74
- tracker = CTA::TrainTracker.new(key)
74
+ CTA::TrainTracker.key = key
75
75
  ```
76
76
 
77
- ## Get a List of Stops and Stations
77
+ ### Get a List of Stops and Stations
78
78
 
79
79
  ``` ruby
80
80
  # stops
81
- @tracker.stops
81
+ CTA::TrainTracker.stops
82
82
 
83
83
  # stations
84
- @tracker.stations
84
+ CTA::TrainTracker.stations
85
85
  ```
86
86
 
87
- ## Get Predicted Arrival Times
87
+ ### Get Predicted Arrival Times
88
88
 
89
89
  ``` ruby
90
- @tracker.arrivals stop_id: "30106"
90
+ CTA::TrainTracker.arrivals :stpid => "30106"
91
91
  ```
@@ -1,7 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'cta-api'
3
- s.version = '1.0.0'
3
+ s.version = '1.0.1'
4
4
  s.author = 'Frank Bonetti'
5
+ s.homepage = 'https://github.com/fbonetti/cta-api'
5
6
  s.date = '2013-02-02'
6
7
 
7
8
  s.license = 'MIT'
@@ -13,5 +14,6 @@ Gem::Specification.new do |s|
13
14
 
14
15
  s.require_paths = ['lib']
15
16
  s.files = Dir.glob("**/*").reject { |x| File.directory?(x) }
16
- s.add_dependency('nokogiri', '>= 1.5.6')
17
+ s.add_dependency('httparty', '>= 0.10.2')
18
+ s.add_dependency('hashie', '>= 2.0.0')
17
19
  end
@@ -1,23 +1,195 @@
1
- require 'nokogiri'
2
- require 'open-uri'
3
- require 'time'
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'hashie'
4
4
  require 'csv'
5
+ require 'time'
6
+
7
+ class Array
8
+ def self.wrap(object)
9
+ if object.nil?
10
+ []
11
+ elsif object.respond_to?(:to_ary)
12
+ object.to_ary || [object]
13
+ else
14
+ [object]
15
+ end
16
+ end
17
+ end
18
+
19
+ module CTA
20
+ class BusTracker
21
+ include HTTParty
22
+ base_uri 'http://www.ctabustracker.com/bustime/api/v1'
23
+ format :xml
24
+ @@key = nil
25
+
26
+ def self.time(options={})
27
+ options.merge!({
28
+ :key => @@key
29
+ })
30
+
31
+ response = get("/gettime", :query => options)['bustime_response']
32
+ check_for_errors response['error']
33
+
34
+ Time.parse response['tm']
35
+ end
36
+
37
+ def self.vehicles(options={})
38
+ options.merge!({
39
+ :key => @@key
40
+ })
41
+ options[:vid] = options[:vid].join(',') if options[:vid].kind_of?(Array)
42
+ options[:rt] = options[:rt].join(',') if options[:rt].kind_of?(Array)
43
+
44
+ response = get("/getvehicles", :query => options)['bustime_response']
45
+ check_for_errors response['error']
46
+
47
+ results = Array.wrap response['vehicle']
48
+ results.map { |result| Hashie::Mash.new result } unless results.nil?
49
+ end
50
+
51
+ def self.routes(options={})
52
+ options.merge!({
53
+ :key => @@key
54
+ })
55
+
56
+ response = get("/getroutes", :query => options)['bustime_response']
57
+ check_for_errors response['error']
58
+
59
+ results = Array.wrap response['route']
60
+ Hash[ results.map { |result| [result['rt'], result['rtnm']] } ] unless results.nil?
61
+ end
62
+
63
+ def self.directions(options={})
64
+ options.merge!({
65
+ :key => @@key
66
+ })
67
+
68
+ response = get("/getdirections", :query => options)['bustime_response']
69
+ check_for_errors response['error']
70
+
71
+ results = Array.wrap response['dir']
72
+ results.map { |direction| direction.split(/ /)[0].downcase.to_sym } unless results.nil?
73
+ end
74
+
75
+ def self.stops(options={})
76
+ options.merge!({
77
+ :key => @@key
78
+ })
79
+ options[:dir] = "#{options[:dir]} bound" if options[:dir].kind_of?(Symbol)
80
+
81
+ response = get("/getstops", :query => options)['bustime_response']
82
+ check_for_errors response['error']
83
+
84
+ results = Array.wrap response['stop']
85
+ results.map { |result| Hashie::Mash.new result } unless results.nil?
86
+ end
87
+
88
+ def self.patterns(options={})
89
+ options.merge!({
90
+ :key => @@key
91
+ })
92
+ options['pid'] = options['pid'].join(',') if options['pid'].kind_of?(Array)
93
+
94
+ response = get("/getpatterns", :query => options)['bustime_response']
95
+ check_for_errors response['error']
96
+
97
+ results = Array.wrap response['ptr']
98
+ results.map { |result| Hashie::Mash.new result } unless results.nil?
99
+ end
100
+
101
+ def self.predictions(options={})
102
+ options.merge!({
103
+ :key => @@key
104
+ })
105
+ options['stpid'] = options['stpid'].join(',') if options['stpid'].kind_of?(Array)
106
+ options['rt'] = options['rt'].join(',') if options['rt'].kind_of?(Array)
107
+ options['vid'] = options['vid'].join(',') if options['vid'].kind_of?(Array)
108
+
109
+ response = get("/getpredictions", :query => options)['bustime_response']
110
+ check_for_errors response['error']
111
+
112
+ results = Array.wrap response['prd']
113
+ results.map { |result| Hashie::Mash.new result } unless results.nil?
114
+ end
115
+
116
+ def self.bulletins(options={})
117
+ options.merge!({
118
+ :key => @@key
119
+ })
120
+ options['rt'] = options['rt'].join(',') if options['rt'].kind_of?(Array)
121
+ options['stpid'] = options['stpid'].join(',') if options['stpid'].kind_of?(Array)
122
+
123
+ response = get("/getservicebulletins", :query => options)['bustime_response']
124
+ check_for_errors response['error']
125
+
126
+ results = Array.wrap response['sb']
127
+ results.map { |result| Hashie::Mash.new result } unless results.nil?
128
+ end
129
+
130
+ def self.key
131
+ @@key
132
+ end
133
+
134
+ def self.key=(key)
135
+ @@key = key
136
+ end
137
+
138
+ private
139
+
140
+ def self.check_for_errors(error)
141
+ puts "API ERROR: #{error['msg']}" if error
142
+ end
143
+ end
144
+
145
+ class TrainTracker
146
+ include HTTParty
147
+ base_uri 'http://lapi.transitchicago.com/api/1.0'
148
+ format :xml
149
+ @@key = nil
150
+
151
+ def self.arrivals(options={})
152
+ options.merge!({
153
+ :key => @@key
154
+ })
155
+
156
+ response = get("/ttarrivals.aspx", :query => options)['ctatt']
157
+ check_for_errors response
158
+
159
+ results = Array.wrap response['eta']
160
+ results.map { |result| Hashie::Mash.new result } unless results.nil?
161
+ end
162
+
163
+ def self.stops
164
+ stops = stop_table.map { |stop| [stop['STOP_ID'], stop['STOP_NAME']] }.flatten
165
+ Hash[*stops]
166
+ end
167
+
168
+ def self.stations
169
+ stations = stop_table.map { |stop| [stop['PARENT_STOP_ID'], stop['STATION_DESCRIPTIVE_NAME']] }.flatten
170
+ Hash[*stations]
171
+ end
172
+
173
+ def self.stop_table
174
+ stop_data = []
175
+ CSV.read(File.dirname(__FILE__) + "/cta_L_stops.csv").each_with_index do |line, index|
176
+ index == 0 ? @headers = line : stop_data << Hash[ @headers.zip line ]
177
+ end
178
+ stop_data
179
+ end
180
+
181
+ def self.key
182
+ @@key
183
+ end
184
+
185
+ def self.key=(key)
186
+ @@key = key
187
+ end
188
+
189
+ private
5
190
 
6
- require 'bus-tracker/bus-tracker'
7
- require 'bus-tracker/pattern'
8
- require 'bus-tracker/stop'
9
- require 'bus-tracker/vehicle'
10
- require 'bus-tracker/point'
11
- require 'bus-tracker/prediction'
12
- require 'bus-tracker/service-bulletin'
13
- require 'bus-tracker/service'
14
- require 'train-tracker/train-tracker'
15
- require 'train-tracker/estimate'
16
-
17
- class String
18
- def to_bool
19
- return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
20
- return false if self == false || self =~ (/(false|f|no|n|0)$/i)
21
- raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
191
+ def self.check_for_errors(error)
192
+ puts "API ERROR: #{error['errNm']}. Error code: #{error['errCd']}" if error['errCd'] != "0"
193
+ end
22
194
  end
23
195
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cta-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,13 +12,13 @@ cert_chain: []
12
12
  date: 2013-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: nokogiri
15
+ name: httparty
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.5.6
21
+ version: 0.10.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,23 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 1.5.6
29
+ version: 0.10.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: hashie
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.0.0
30
46
  description: An easy way to access the Chicago Transit Authority API via the Ruby
31
47
  programming language
32
48
  email: frank.r.bonetti@gmail.com
@@ -34,27 +50,16 @@ executables: []
34
50
  extensions: []
35
51
  extra_rdoc_files: []
36
52
  files:
37
- - cta-api-1.0.0.gem
53
+ - cta-api-1.0.1.gem
38
54
  - cta-api.gemspec
39
55
  - Gemfile
40
- - lib/bus-tracker/bus-tracker.rb
41
- - lib/bus-tracker/pattern.rb
42
- - lib/bus-tracker/point.rb
43
- - lib/bus-tracker/prediction.rb
44
- - lib/bus-tracker/service-bulletin.rb
45
- - lib/bus-tracker/service.rb
46
- - lib/bus-tracker/stop.rb
47
- - lib/bus-tracker/vehicle.rb
48
- - lib/cta-api/version.rb
49
56
  - lib/cta-api.rb
50
57
  - lib/cta_L_stops.csv
51
- - lib/train-tracker/estimate.rb
52
- - lib/train-tracker/train-tracker.rb
53
58
  - LICENSE
54
59
  - Rakefile
55
60
  - README.md
56
61
  - spec/spec_helper.rb
57
- homepage:
62
+ homepage: https://github.com/fbonetti/cta-api
58
63
  licenses:
59
64
  - MIT
60
65
  post_install_message:
@@ -1,208 +0,0 @@
1
- module CTA
2
- class BusTracker
3
-
4
- def initialize(key)
5
- @key = key
6
- end
7
-
8
- def time
9
- base_url = "http://www.ctabustracker.com/bustime/api/v1/gettime?"
10
- url = "#{ base_url}key=#{ @key }"
11
- Time.parse(Nokogiri::XML(open(url)).at("//tm").text)
12
- end
13
-
14
- def vehicles_by_routes(routes)
15
- if routes.kind_of?(Array)
16
- full_array = []
17
- until routes.size == 0
18
- full_array << vehicles(route: routes.shift(10))
19
- end
20
- full_array.flatten!
21
- else
22
- raise ArgumentError.new "Argument must be an Array."
23
- end
24
- end
25
-
26
- def vehicles_by_ids(vids)
27
- if vids.kind_of?(Array) && routes.size.between(1,10)
28
- vehicles(vid: vids)
29
- else
30
- raise ArgumentError.new "must be an Array with 1 to 10 elements."
31
- end
32
- end
33
-
34
- def patterns_by_route(route)
35
- if route.kind_of?(String) && route.length > 0
36
- patterns(route: route)
37
- else
38
- raise ArgumentError.new "must be a String with a length greater than zero."
39
- end
40
- end
41
-
42
- def patterns_by_ids(pid)
43
- if pid.kind_of?(Array) && pid.size.between?(1,10)
44
- patterns(pid: pid)
45
- else
46
- raise ArgumentError.new "must be an Array with 1 to 10 elements."
47
- end
48
- end
49
-
50
- def predictions_by_stop_ids(stop_id, route="")
51
- if stop_id.kind_of?(Array) && stop_id.size.between?(1,10)
52
- predictions(stop_id: stop_id, route: route)
53
- else
54
- raise ArgumentError.new "must be an Array with 1 to 10 elements."
55
- end
56
- end
57
-
58
- def predictions_by_vehicle_ids(vid)
59
- if vid.kind_of?(Array) && vid.size.between?(1,10)
60
- predictions(vid: vid)
61
- else
62
- raise ArgumentError.new "must be an Array with 1 to 10 elements."
63
- end
64
- end
65
-
66
- def routes
67
- base_url = "http://www.ctabustracker.com/bustime/api/v1/getroutes?"
68
- url = "#{ base_url }key=#{ @key }"
69
- route_hash = {}
70
- Nokogiri::XML(open(url)).xpath("//route").each do |route|
71
- route_hash[route.at("rt").text] = route.at("rtnm").text
72
- end
73
- route_hash
74
- end
75
-
76
- def route_directions(route)
77
- if route.kind_of?(String)
78
- base_url = "http://www.ctabustracker.com/bustime/api/v1/getdirections?rt=#{ route }"
79
- url = "#{ base_url }&key=#{ @key }"
80
- Nokogiri::XML(open(url)).xpath("//dir").map { |x| x.text.split(/ /)[0].to_sym }
81
- end
82
- end
83
-
84
- def stops(route, direction)
85
- if (route.kind_of?(String) || route.kind_of?(Integer)) && direction.kind_of?(Symbol)
86
- direction = direction.to_s + "+bound"
87
- base_url = "http://www.ctabustracker.com/bustime/api/v1/getstops?"
88
- url = "#{ base_url }key=#{ @key }&rt=#{ route }&dir=#{ direction }"
89
-
90
- stop_array = Nokogiri::XML(open(url)).xpath("//stop").map do |item|
91
- Stop.new({
92
- :id => item.at("stpid").text,
93
- :name => item.at("stpnm").text,
94
- :latitude => item.at("lat").text,
95
- :longitude => item.at("lon").text
96
- })
97
- end
98
- stop_array
99
- end
100
- end
101
-
102
- def service_bulletins(params)
103
- route = params[:route] || []
104
- route_direction = params[:route_direction]
105
- stop_id = params[:stop_id] || []
106
-
107
- base_url = "http://www.ctabustracker.com/bustime/api/v1/getservicebulletins?"
108
- url = "#{ base_url }key=#{ @key }&rt=#{ route.join(',') }&rtdir=#{ route_direction }&stpid=#{ stop_id.join(',') }"
109
- puts url
110
-
111
- bulletin_array = Nokogiri::XML(open(url)).xpath("//sb").map do |item|
112
- ServiceBulletin.new({
113
- :name => item.at("nm") && item.at("nm").text,
114
- :subject => item.at("sbj") && item.at("sbj").text,
115
- :detail => item.at("dtl") && item.at("dtl").text.gsub("<br/>", "\n").strip,
116
- :brief => item.at("brf") && item.at("brf").text,
117
- :priority => item.at("prty") && item.at("prty").text,
118
- :services => item.xpath("srvc") && item.xpath("srvc").map do |subitem|
119
- ServiceBulletin::Service.new({
120
- :route => subitem.at("rt") && subitem.at("rt").text,
121
- :route_direction => subitem.at("rtdir") && subitem.at("rtdir").text,
122
- :stop_id => subitem.at("stpid") && subitem.at("stpid").text,
123
- :stop_name => subitem.at("stpnm") && subitem.at("stpnm").text
124
- })
125
- end
126
- })
127
- end
128
- bulletin_array
129
- end
130
-
131
- private
132
-
133
- def vehicles(params)
134
- route = params[:route] || []
135
- vid = params[:vid] || []
136
-
137
- base_url = "http://www.ctabustracker.com/bustime/api/v1/getvehicles?"
138
- url = "#{ base_url }key=#{ @key }&rt=#{ route.join(",") }&vid=#{ vid.join(",") }"
139
-
140
- vehicle_array = Nokogiri::XML(open(url)).xpath("//vehicle").map do |item|
141
- Vehicle.new({
142
- :id => item.at("vid").text,
143
- :timestamp => Time.parse(item.at("tmstmp").text),
144
- :latitude => item.at("lat").text,
145
- :longitude => item.at("lon").text,
146
- :heading => item.at("hdg").text,
147
- :pattern_id => item.at("pid").text,
148
- :pattern_distance => item.at("pdist").text,
149
- :destination => item.at("des").text,
150
- :delay => item.at("dly") && item.at("dly").text.to_bool
151
- })
152
- end
153
- vehicle_array
154
- end
155
-
156
- def patterns(params)
157
- route = params[:route]
158
- pid = params[:pid] || []
159
-
160
- base_url = "http://www.ctabustracker.com/bustime/api/v1/getpatterns?"
161
- url = "#{ base_url }key=#{ @key }&rt=#{ route }&pid=#{ pid.join(",") }"
162
-
163
- pattern_array = Nokogiri::XML(open(url)).xpath("//ptr").map do |item|
164
- Pattern.new({
165
- :id => item.at("pid").text,
166
- :length => item.at("ln").text.to_f,
167
- :direction => item.at("rtdir").text,
168
- :points => item.xpath("pt").map do |subitem|
169
- Pattern::Point.new({
170
- :sequence => subitem.at("seq").text,
171
- :latitude => subitem.at("lat").text,
172
- :longitude => subitem.at("lon").text,
173
- :type => subitem.at("typ").text == "S" ? "stop" : "waypoint",
174
- :id => subitem.at("stpid") && subitem.at("stpid").text,
175
- :name => subitem.at("stpnm") && subitem.at("stpnm").text,
176
- :distance => subitem.at("pdist") && subitem.at("pdist").text
177
- })
178
- end
179
- })
180
- end
181
- pattern_array
182
- end
183
-
184
- def predictions(params)
185
- stop_id = params[:stop_id] || []
186
- route = params[:route] || []
187
- vid = params[:vid] || []
188
-
189
- base_url = "http://www.ctabustracker.com/bustime/api/v1/getpredictions?"
190
- url = "#{ base_url }key=#{ @key }&stpid=#{ stop_id.join(',') }&rt=#{ route.join(',') }&vid=#{ vid.join(',') }"
191
-
192
- prediction_array = Nokogiri::XML(open(url)).xpath("//prd").map do |item|
193
- Prediction.new({
194
- :timestamp => Time.parse(item.at("tmstmp").text),
195
- :type => item.at("typ").text == "A" ? "arrival" : "departure",
196
- :stop_name => item.at("stpnm").text,
197
- :vehicle_id => item.at("vid").text,
198
- :distance_from_stop => item.at("dstp").text.to_f,
199
- :route => item.at("rt").text,
200
- :route_direction => item.at("rtdir").text,
201
- :destination => item.at("des").text,
202
- :predicted_time => Time.parse(item.at("prdtm").text)
203
- })
204
- end
205
- prediction_array
206
- end
207
- end
208
- end
@@ -1,13 +0,0 @@
1
- module CTA
2
- class BusTracker
3
- class Pattern
4
- attr_reader :id, :length, :direction, :points
5
-
6
- def initialize(params)
7
- params.each do |key, value|
8
- self.instance_variable_set("@#{ key }".to_sym, value)
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,15 +0,0 @@
1
- module CTA
2
- class BusTracker
3
- class Pattern
4
- class Point
5
- attr_reader :sequence, :latitude, :longitude, :type, :id, :name, :distance
6
-
7
- def initialize(params)
8
- params.each do |key, value|
9
- self.instance_variable_set("@#{ key }".to_sym, value)
10
- end
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,13 +0,0 @@
1
- module CTA
2
- class BusTracker
3
- class Prediction
4
- attr_reader :timestamp, :type, :stop_name, :stop_id, :vehicle_id, :distance_from_stop, :route, :route_direction, :destination, :predicted_time
5
-
6
- def initialize(params)
7
- params.each do |key, value|
8
- self.instance_variable_set("@#{ key }".to_sym, value)
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- module CTA
2
- class BusTracker
3
- class ServiceBulletin
4
- attr_reader :name, :subject, :detail, :brief, :priority, :services
5
-
6
- def initialize(params)
7
- params.each do |key, value|
8
- self.instance_variable_set("@#{ key }".to_sym, value)
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,15 +0,0 @@
1
- module CTA
2
- class BusTracker
3
- class ServiceBulletin
4
- class Service
5
- attr_reader :route, :route_direction, :stop_id, :stop_name
6
-
7
- def initialize(params)
8
- params.each do |key, value|
9
- self.instance_variable_set("@#{ key }".to_sym, value)
10
- end
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,13 +0,0 @@
1
- module CTA
2
- class BusTracker
3
- class Stop
4
- attr_reader :id, :name, :latitude, :longitude
5
-
6
- def initialize(params)
7
- params.each do |key, value|
8
- self.instance_variable_set("@#{ key }".to_sym, value)
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- module CTA
2
- class BusTracker
3
- class Vehicle
4
- attr_reader :id, :timestamp, :latitude, :longitude, :heading, :pattern_id, :pattern_distance, :destination, :delay
5
-
6
- def initialize(params)
7
- params.each do |key, value|
8
- self.instance_variable_set("@#{ key }".to_sym, value)
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,3 +0,0 @@
1
- module CTA
2
- VERSION = "1.0.1"
3
- end
@@ -1,13 +0,0 @@
1
- module CTA
2
- class TrainTracker
3
- class Estimate
4
- attr_reader :station_id, :stop_id, :station_name, :stop_description, :run_number, :route, :destination_stop_id, :destination_name, :direction, :timestamp, :arrival_time, :approaching, :schedule, :fit, :delay
5
-
6
- def initialize(params)
7
- params.each do |key, value|
8
- self.instance_variable_set("@#{ key }".to_sym, value)
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,56 +0,0 @@
1
- module CTA
2
- class TrainTracker
3
-
4
- def initialize(key)
5
- @key = key
6
- @line_table = {
7
- :red => "Red",
8
- :blue => "Blue",
9
- :brown => "Brn",
10
- :orange => "Org",
11
- :purple => "P",
12
- :pink => "Pink",
13
- :yellow => "Y"
14
- }
15
- end
16
-
17
- def arrivals(params)
18
- station_id = params[:station_id]
19
- stop_id = params[:stop_id]
20
- max = params[:max]
21
- route = @line_table[params[:route]]
22
-
23
- base_url = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?"
24
- url = "#{ base_url }key=#{ @key }&mapid=#{ station_id }&stpid=#{ stop_id }&max=#{ max }&rt=#{ route }"
25
-
26
- estimate_array = Nokogiri::XML(open(url)).xpath("//eta").map do |item|
27
- Estimate.new({
28
- :station_id => item.at("staId").text,
29
- :stop_id => item.at("stpId").text,
30
- :station_name => item.at("staNm").text,
31
- :stop_description => item.at("stpDe").text,
32
- :run_number => item.at("rn").text,
33
- :route => @line_table.key(item.at("rt").text),
34
- :destination_stop_id => item.at("destSt").text,
35
- :destination_name => item.at("destNm").text,
36
- :direction => item.at("trDr").text,
37
- :timestamp => Time.parse(item.at("prdt").text),
38
- :arrival_time => Time.parse(item.at("arrT").text),
39
- :approaching => item.at("isApp").text.to_bool,
40
- :schedule => item.at("isSch").text.to_bool,
41
- :fit => item.at("isFlt").text.to_bool,
42
- :delay => item.at("isDly").text.to_bool
43
- })
44
- end
45
- estimate_array
46
- end
47
-
48
- def stops
49
- CSV.read("lib/cta_L_stops.csv").map { |x| {x[0] => x[2]} }
50
- end
51
-
52
- def stations
53
- CSV.read("lib/cta_L_stops.csv").map { |x| {x[7] => x[5]} }
54
- end
55
- end
56
- end