next_train 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e71f149f31604f0703404e2c752639c909be585a
4
+ data.tar.gz: 19ed2d88a0ba7bcd6cf3ed5869b5b730dfabb4cf
5
+ SHA512:
6
+ metadata.gz: 945b48ec270b02d314450ad3955622e614e56b89ae92c9f1325bd8265946211853f0529d315cdd98f1112ab0ec9418183b714fec64add5013f48ecef2f1d7d78
7
+ data.tar.gz: 49edf79f746b583972565796e0f9cfa000bca4eac3e28d82633a56768e646ea66ae0081446018dbefff7db5e08d4e1cdaa821e17a01a82327a9417763a85f649
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ next_train (0.0.1)
5
+ cta-api
6
+ time_diff
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (4.2.0)
12
+ i18n (~> 0.7)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.3, >= 0.3.4)
16
+ tzinfo (~> 1.1)
17
+ addressable (2.3.6)
18
+ coderay (1.1.0)
19
+ crack (0.4.2)
20
+ safe_yaml (~> 1.0.0)
21
+ cta-api (1.0.1)
22
+ hashie (>= 2.0.0)
23
+ httparty (>= 0.10.2)
24
+ diff-lcs (1.2.5)
25
+ hashie (3.3.2)
26
+ httparty (0.13.3)
27
+ json (~> 1.8)
28
+ multi_xml (>= 0.5.2)
29
+ i18n (0.7.0)
30
+ json (1.8.2)
31
+ method_source (0.8.2)
32
+ minitest (5.5.1)
33
+ multi_xml (0.5.5)
34
+ pry (0.10.0)
35
+ coderay (~> 1.1.0)
36
+ method_source (~> 0.8.1)
37
+ slop (~> 3.4)
38
+ rake (10.4.2)
39
+ rspec (3.1.0)
40
+ rspec-core (~> 3.1.0)
41
+ rspec-expectations (~> 3.1.0)
42
+ rspec-mocks (~> 3.1.0)
43
+ rspec-core (3.1.7)
44
+ rspec-support (~> 3.1.0)
45
+ rspec-expectations (3.1.2)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.1.0)
48
+ rspec-mocks (3.1.3)
49
+ rspec-support (~> 3.1.0)
50
+ rspec-support (3.1.2)
51
+ safe_yaml (1.0.4)
52
+ slop (3.6.0)
53
+ thread_safe (0.3.4)
54
+ time_diff (0.3.0)
55
+ activesupport
56
+ i18n
57
+ timecop (0.7.1)
58
+ tzinfo (1.2.2)
59
+ thread_safe (~> 0.1)
60
+ webmock (1.20.4)
61
+ addressable (>= 2.3.6)
62
+ crack (>= 0.3.2)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ bundler
69
+ next_train!
70
+ pry
71
+ rake
72
+ rspec
73
+ timecop
74
+ webmock
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sandro Padin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,321 @@
1
+ # Next Train
2
+
3
+ Find out when the next CTA train arrives.
4
+
5
+ ### Example:
6
+
7
+ require "next_train"
8
+
9
+ api_key = "key-here" # Check resources below to sign up
10
+ stpid = 90032 # Racine Blue Line towards O'Hare
11
+
12
+ NextTrain.api_key = api_key
13
+ NextTrain[stpid] # => "2 minutes"
14
+
15
+ ### Stops:
16
+
17
+ STPID STOP_NAME
18
+ 30162 18th (54th/Cermak-bound)
19
+ 30161 18th (Loop-bound)
20
+ 30022 35th/Archer (Loop-bound)
21
+ 30023 35th/Archer (Midway-bound)
22
+ 30214 35-Bronzeville-IIT (63rd-bound)
23
+ 30213 35-Bronzeville-IIT (Harlem-bound)
24
+ 30246 43rd (63rd-bound)
25
+ 30245 43rd (Harlem-bound)
26
+ 30210 47th (63rd-bound) Elevated (63rd-bound)
27
+ 30209 47th (SB) Elevated (Harlem-bound)
28
+ 30238 47th-Dan Ryan (95th-bound)
29
+ 30237 47th-Dan Ryan (Howard-bound)
30
+ 30025 51st (63rd-bound)
31
+ 30024 51st (Harlem-bound)
32
+ 30113 54th/Cermak (Loop-bound)
33
+ 30114 54th/Cermak (Terminal arrival)
34
+ 30178 63rd-Dan Ryan (95th-bound)
35
+ 30177 63rd-Dan Ryan (Howard-bound)
36
+ 30192 69th (95th-bound)
37
+ 30191 69th (Howard-bound)
38
+ 30047 79th (95th-bound)
39
+ 30046 79th (Howard-bound)
40
+ 30276 87th (95th-bound)
41
+ 30275 87th (Howard-bound)
42
+ 30089 95th/Dan Ryan (95th-bound)
43
+ 30088 95th/Dan Ryan (Howard-bound)
44
+ 30132 Adams/Wabash (Inner Loop)
45
+ 30131 Adams/Wabash (Outer Loop)
46
+ 30240 Addison (O'Hare Branch) (Forest Pk-bound)
47
+ 30239 Addison (O'Hare Branch) (O'Hare-bound)
48
+ 30277 Addison (Kimball-bound)
49
+ 30278 Addison (Loop-bound)
50
+ 30274 Addison (95th-bound)
51
+ 30273 Addison (Howard-bound)
52
+ 30230 Argyle (95th-bound)
53
+ 30229 Argyle (Howard-bound)
54
+ 30127 Armitage (Kimball-Linden-bound)
55
+ 30128 Armitage (Loop-bound)
56
+ 30205 Ashland (Loop-bound)
57
+ 30206 Ashland (Midway-bound)
58
+ 30032 Ashland (Harlem-54th/Cermak-bound)
59
+ 30033 Ashland (Loop-63rd-bound)
60
+ 30056 Ashland/63rd (Harlem-bound)
61
+ 30057 Ashland/63rd (Terminal arrival)
62
+ 30002 Austin (Forest Pk-bound)
63
+ 30001 Austin (O'Hare-bound)
64
+ 30243 Austin (63rd-bound)
65
+ 30244 Austin (Harlem-bound)
66
+ 30013 Belmont (O'Hare Branch) (Forest Pk-bound)
67
+ 30012 Belmont (O'Hare Branch) (O'Hare-bound)
68
+ 30256 Belmont (95th-bound)
69
+ 30255 Belmont (Howard-bound)
70
+ 30257 Belmont (Kimball-Linden-bound)
71
+ 30258 Belmont (Loop-bound)
72
+ 30067 Berwyn (95th-bound)
73
+ 30066 Berwyn (Howard-bound)
74
+ 30268 Bryn Mawr (95th-bound)
75
+ 30267 Bryn Mawr (Howard-bound)
76
+ 30087 California (54th/Cermak-bound)
77
+ 30086 California (Loop-bound)
78
+ 30112 California/Milwaukee (Forest Pk-bound)
79
+ 30265 California (63rd-bound)
80
+ 30266 California (Harlem-bound)
81
+ 30111 California/Milwaukee (O'Hare-bound)
82
+ 30242 Central-Evanston (Howard-Loop-bound)
83
+ 30241 Central-Evanston (Linden-bound)
84
+ 30054 Central (63rd-bound)
85
+ 30055 Central (Harlem-bound)
86
+ 30152 Central Park (54th/Cermak-bound)
87
+ 30151 Central Park (Loop-bound)
88
+ 30194 Cermak-Chinatown (95th-bound)
89
+ 30193 Cermak-Chinatown (Howard-bound)
90
+ 30272 Chicago/Milwaukee (Forest Pk-bound)
91
+ 30271 Chicago/Milwaukee (O'Hare-bound)
92
+ 30137 Chicago/Franklin (Kimball-Linden-bound)
93
+ 30138 Chicago/Franklin (Loop-bound)
94
+ 30280 Chicago/State (95th-bound)
95
+ 30279 Chicago/State (Howard-bound)
96
+ 30083 Cicero (54th/Cermak-bound)
97
+ 30082 Cicero (Loop-bound)
98
+ 30188 Cicero (Forest Pk-bound)
99
+ 30187 Cicero (O'Hare-bound)
100
+ 30094 Cicero (63rd-bound)
101
+ 30009 Cicero (Harlem-bound)
102
+ 30122 Clark/Division (95th-bound)
103
+ 30121 Clark/Division (Howard-bound)
104
+ 30074 Clark/Lake (Inner Loop)
105
+ 30075 Clark/Lake (Outer Loop)
106
+ 30374 Clark/Lake (Forest Pk-bound)
107
+ 30375 Clark/Lake (O'Hare-bound)
108
+ 30085 Clinton (Forest Pk-bound)
109
+ 30084 Clinton (O'Hare-bound)
110
+ 30222 Clinton (Harlem-54th/Cermak-bound)
111
+ 30221 Clinton (Loop-63rd-bound)
112
+ 30291 Conservatory (63rd-bound)
113
+ 30292 Conservatory (Harlem-bound)
114
+ 30139 Cottage Grove (Terminal arrival)
115
+ 30140 East 63rd-Cottage Grove (Harlem-bound)
116
+ 30045 Cumberland (Forest Pk-bound)
117
+ 30044 Cumberland (O'Hare-bound)
118
+ 30041 Damen (54th/Cermak-bound)
119
+ 30040 Damen (Loop-bound)
120
+ 30116 Damen/Milwaukee (Forest Pk-bound)
121
+ 30115 Damen/Milwaukee (O'Hare-bound)
122
+ 30018 Damen (Kimball-bound)
123
+ 30019 Damen (Loop-bound)
124
+ 30011 Davis (Howard-Loop-bound)
125
+ 30010 Davis (Linden-bound)
126
+ 30134 Dempster (Howard-Loop-bound)
127
+ 30133 Dempster (Linden-bound)
128
+ 30103 Diversey (Kimball-Linden-bound)
129
+ 30104 Diversey (Loop-bound)
130
+ 30063 Division/Milwaukee (Forest Pk-bound)
131
+ 30062 Division/Milwaukee (O'Hare-bound)
132
+ 30076 Forest Park (O'Hare-bound)
133
+ 30077 Forest Park (Terminal Arrival)
134
+ 30102 Foster (Howard-Loop-bound)
135
+ 30101 Foster (Linden-bound)
136
+ 30167 Francisco (Kimball-bound)
137
+ 30168 Francisco (Loop-bound)
138
+ 30234 Fullerton (95th-bound)
139
+ 30233 Fullerton (Howard-bound)
140
+ 30235 Fullerton (Kimball-Linden-bound)
141
+ 30236 Fullerton (Loop-bound)
142
+ 30100 Garfield (63rd-bound)
143
+ 30099 Garfield (Harlem-bound)
144
+ 30224 Garfield-Dan Ryan (95th-bound)
145
+ 30223 Garfield-Dan Ryan (Howard-bound)
146
+ 30096 Grand/Milwaukee (Forest Pk-bound)
147
+ 30095 Grand/Milwaukee (O'Hare-bound)
148
+ 30065 Grand/State (95th-bound)
149
+ 30064 Grand/State (Howard-bound)
150
+ 30148 Granville (95th-bound)
151
+ 30147 Granville (Howard-bound)
152
+ 30215 Halsted (Loop-bound)
153
+ 30216 Halsted (Midway-bound)
154
+ 30184 Halsted/63rd (Ashland-bound)
155
+ 30183 Halsted/63rd (Harlem-bound)
156
+ 30190 Harlem (Forest Pk-bound)
157
+ 30189 Harlem (O'Hare-bound)
158
+ 30146 Harlem (O'Hare Branch) (Forest Pk-bound)
159
+ 30145 Harlem (O'Hare Branch) (O'Hare-bound)
160
+ 30004 Harlem (Terminal arrival)
161
+ 30003 Harlem (63rd-bound)
162
+ 30166 Library (Inner Loop)
163
+ 30165 Library (Outer Loop)
164
+ 30286 Harrison (95th-bound)
165
+ 30285 Harrison (Howard-bound)
166
+ 30175 Howard (NB) (Linden, Skokie-bound)
167
+ 30176 Howard (Terminal arrival)
168
+ 30173 Howard (Terminal arrival)
169
+ 30174 Howard (95th-Bound)
170
+ 30158 Illinois Medical District (Forest Pk-bound)
171
+ 30157 Illinois Medical District (O'Hare-bound)
172
+ 30059 Indiana (63rd-bound)
173
+ 30058 Indiana (Harlem-bound)
174
+ 30108 Irving Park (O'Hare Branch) (Forest Pk-bound)
175
+ 30107 Irving Park (O'Hare Branch) (O'Hare-bound)
176
+ 30281 Irving Park (Kimball-bound)
177
+ 30282 Irving Park (Loop-bound)
178
+ 30015 Jackson/Dearborn (Forest Pk-bound)
179
+ 30014 Jackson/Dearborn (O'Hare-bound)
180
+ 30110 Jackson/State (95th-bound)
181
+ 30109 Jackson/State (Howard-bound)
182
+ 30228 Jarvis (95th-bound)
183
+ 30227 Jarvis (Howard-bound)
184
+ 30248 Jefferson Park (Forest Pk-bound)
185
+ 30247 Jefferson Park (O'Hare-bound)
186
+ 30219 Kedzie (Loop-bound)
187
+ 30220 Kedzie (Midway-bound)
188
+ 30202 Kedzie (54th/Cermak-bound)
189
+ 30201 Kedzie (Loop-bound)
190
+ 30225 Kedzie (Kimball-bound)
191
+ 30226 Kedzie (Loop-bound)
192
+ 30207 Kedzie (63rd-bound)
193
+ 30208 Kedzie (Harlem-bound)
194
+ 30049 Kedzie-Homan (Forest Pk-bound)
195
+ 30048 Kedzie-Homan (O'Hare-bound)
196
+ 30250 Kimball (Loop-bound)
197
+ 30249 Kimball (Terminal arrival)
198
+ 30217 King Drive (Cottage Grove-bound)
199
+ 30218 King Drive (Harlem-bound)
200
+ 30118 Kostner (54th/Cermak-bound)
201
+ 30117 Kostner (Loop-bound)
202
+ 30290 Lake/State (95th-bound)
203
+ 30289 Lake/State (Howard-bound)
204
+ 30135 Laramie (63rd-bound)
205
+ 30136 Laramie (Harlem-bound)
206
+ 30262 LaSalle (Forest Pk-bound)
207
+ 30261 LaSalle (O'Hare-bound)
208
+ 30031 LaSalle/Van Buren (Inner Loop)
209
+ 30030 LaSalle/Van Buren (Outer Loop)
210
+ 30150 Lawrence (95th-bound)
211
+ 30149 Lawrence (Howard-bound)
212
+ 30204 Linden (Howard-Loop-bound)
213
+ 30203 Linden (Linden-bound)
214
+ 30198 Logan Square (Forest Pk-bound)
215
+ 30197 Logan Square (O'Hare-bound)
216
+ 30252 Loyola (95th-bound)
217
+ 30251 Loyola (Howard-bound)
218
+ 30124 Madison/Wabash (Inner Loop)
219
+ 30123 Madison/Wabash (Outer Loop)
220
+ 30053 Main (Howard-Loop-bound)
221
+ 30052 Main (Linden-bound)
222
+ 30090 Merchandise Mart (Kimball-Linden-bound)
223
+ 30091 Merchandise Mart (Loop-bound)
224
+ 30182 Midway (Arrival)
225
+ 30181 Midway (Loop-bound)
226
+ 30154 Monroe/Dearborn (Forest Pk-bound)
227
+ 30153 Monroe/Dearborn (O'Hare-bound)
228
+ 30212 Monroe/State (95th-bound)
229
+ 30211 Monroe/State (Howard-bound)
230
+ 30260 Montrose (Forest Pk-bound)
231
+ 30259 Montrose (O'Hare-bound)
232
+ 30287 Montrose (Kimball-bound)
233
+ 30288 Montrose (Loop-bound)
234
+ 30296 Morgan (Harlem-54th/Cermak-bound)
235
+ 30295 Morgan (Loop-63rd-bound)
236
+ 30021 Morse (95th-bound)
237
+ 30020 Morse (Howard-bound)
238
+ 30126 North/Clybourn (95th-bound)
239
+ 30125 North/Clybourn (Howard-bound)
240
+ 30079 Noyes (Howard-Loop-bound)
241
+ 30078 Noyes (Linden-bound)
242
+ 30035 Oak Park (Forest Pk-bound)
243
+ 30034 Oak Park (O'Hare-bound)
244
+ 30263 Oak Park (63rd-bound)
245
+ 30264 Oak Park (Harlem-bound)
246
+ 30297 Oakton (Dempster-Skokie-bound)
247
+ 30298 Oakton (Howard-bound)
248
+ 30172 O'Hare Airport (Forest Pk-bound)
249
+ 30171 O'Hare Airport (Terminal Arrival)
250
+ 30253 Paulina (Kimball-bound)
251
+ 30254 Paulina (Loop-bound)
252
+ 30200 Polk (54th/Cermak-bound)
253
+ 30199 Polk (Loop-bound)
254
+ 30185 Pulaski (Loop-bound)
255
+ 30186 Pulaski (Midway-bound)
256
+ 30029 Pulaski (54th/Cermak-bound)
257
+ 30028 Pulaski (Loop-bound)
258
+ 30180 Pulaski (Forest Pk-bound)
259
+ 30179 Pulaski (O'Hare-bound)
260
+ 30005 Pulaski (63rd-bound)
261
+ 30006 Pulaski (Harlem-bound)
262
+ 30007 Quincy/Wells (Inner Loop)
263
+ 30008 Quincy/Wells (Outer Loop)
264
+ 30093 Racine (Forest Pk-bound)
265
+ 30092 Racine (O'Hare-bound)
266
+ 30039 Randolph/Wabash (Inner Loop)
267
+ 30038 Randolph/Wabash (Outer Loop)
268
+ 30119 Ridgeland (63rd-bound)
269
+ 30120 Ridgeland (Harlem-bound)
270
+ 30195 Rockwell (Kimball-bound)
271
+ 30196 Rockwell (Loop-bound)
272
+ 30080 Roosevelt/Wabash (Loop-Harlem-bound)
273
+ 30081 Roosevelt/Wabash (Midway-63rd-bound)
274
+ 30269 Roosevelt/State (Howard-bound)
275
+ 30270 Roosevelt/State (Howard-bound)
276
+ 30160 Rosemont (Forest Pk-bound)
277
+ 30159 Rosemont (O'Hare-bound)
278
+ 30155 Sedgwick (Kimball-Linden-bound)
279
+ 30156 Sedgwick (Loop-bound)
280
+ 30017 Sheridan (95th-bound)
281
+ 30016 Sheridan (Howard-bound)
282
+ 30293 Sheridan (Howard-Linden-bound)
283
+ 30294 Sheridan (Loop-bound)
284
+ 30026 Skokie (Arrival)
285
+ 30027 Skokie (Howard-bound)
286
+ 30164 South Blvd (Howard-Loop-bound)
287
+ 30163 South Blvd (Linden-bound)
288
+ 30070 Southport (Kimball-bound)
289
+ 30071 Southport (Loop-bound)
290
+ 30037 Sox-35th (95th-bound)
291
+ 30036 Sox-35th (Howard-bound)
292
+ 30050 State/Lake (Inner Loop)
293
+ 30051 State/Lake (Outer Loop)
294
+ 30170 Thorndale (95th-bound)
295
+ 30169 Thorndale (Howard-bound)
296
+ 30069 UIC-Halsted (Forest Pk-bound)
297
+ 30068 UIC-Halsted (O'Hare-bound)
298
+ 30073 Washington/Dearborn (Forest Pk-bound)
299
+ 30072 Washington/Dearborn (O'Hare-bound)
300
+ 30141 Washington/Wells (Inner Loop)
301
+ 30142 Washington/Wells (Outer Loop)
302
+ 30231 Wellington (Kimball-Linden-bound)
303
+ 30232 Wellington (Loop-bound)
304
+ 30060 Western (Loop-bound)
305
+ 30061 Western (Midway-bound)
306
+ 30144 Western (54th/Cermak-bound)
307
+ 30143 Western (Loop-bound)
308
+ 30043 Western (Forest Pk-bound)
309
+ 30042 Western (O'Hare-bound)
310
+ 30130 Western/Milwaukee (Forest Pk-bound)
311
+ 30129 Western/Milwaukee (O'Hare-bound)
312
+ 30283 Western (Kimball-bound)
313
+ 30284 Western (Loop-bound)
314
+ 30106 Wilson (95th-bound)
315
+ 30105 Wilson (Howard-bound)
316
+
317
+ ### Resources:
318
+
319
+ * [CTA Train Tracker API Key Application](http://www.transitchicago.com/developers/traintrackerapply.aspx)
320
+
321
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/next_train.rb ADDED
@@ -0,0 +1,76 @@
1
+ require "cta-api"
2
+ require "time_diff"
3
+
4
+ class NextTrain
5
+ def self.[](stpid)
6
+ new(stpid).execute
7
+ end
8
+
9
+ def self.api_key= (api_key)
10
+ CTA::TrainTracker.key = api_key
11
+ end
12
+
13
+ def initialize(stpid)
14
+ @stpid = stpid
15
+ end
16
+
17
+ def execute
18
+ if minutes_until_next_arrival == 0
19
+ "arriving"
20
+ else
21
+ arrival_time_in_minutes
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :stpid
28
+
29
+ def arrival_time_in_minutes
30
+ time_until_next_arrival_diff[:diff]
31
+ end
32
+
33
+ def minutes_until_next_arrival
34
+ time_until_next_arrival_diff[:minute]
35
+ end
36
+
37
+ def time_until_next_arrival_diff
38
+ Time.diff(next_arrival_time, Time.now, "%N")
39
+ end
40
+
41
+ def next_arrival_time
42
+ Time.parse(next_arrival[arrival_time_key])
43
+ end
44
+
45
+ def arrival_time_key
46
+ "arrT"
47
+ end
48
+
49
+ def next_arrival
50
+ arrivals[0]
51
+ end
52
+
53
+ def arrivals
54
+ ensure_no_puts do
55
+ data = CTA::TrainTracker.arrivals(stpid: stpid)
56
+
57
+ if data.empty?
58
+ raise "No data returned, please check your API key"
59
+ else
60
+ data
61
+ end
62
+ end
63
+ end
64
+
65
+ def error?(data)
66
+ data["errCd"] && data["errCd"] != "0"
67
+ end
68
+
69
+ def ensure_no_puts(&block)
70
+ tmp = $stdout
71
+ $stdout = StringIO.new
72
+ block.call
73
+ ensure
74
+ $stdout = tmp
75
+ end
76
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "next_train"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Sandro Padin"]
9
+ spec.email = ["sandropadin@gmail.com"]
10
+ spec.summary = %q{Find out when the next CTA train arrives.}
11
+ spec.description = %q{Find out when the next CTA train arrives.}
12
+ spec.homepage = "https://github.com/spadin/next-train"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "cta-api"
21
+ spec.add_dependency "time_diff"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "timecop"
28
+ spec.add_development_dependency "webmock"
29
+ end
@@ -0,0 +1,66 @@
1
+ require "spec_helper"
2
+ require "next_train"
3
+
4
+ describe NextTrain do
5
+ let(:api_key) { 'test-api-key' }
6
+ let(:stpid) { 30092 }
7
+ let(:base_time) { "20150201 21:00:00" }
8
+ let(:two_minutes_from_base_time) { "20150201 21:02:00" }
9
+
10
+ it "returns 2 minutes when train is two minutes away" do
11
+ stub_cta_train_request(
12
+ api_key: api_key,
13
+ stpid: stpid,
14
+ response: arrival_response(two_minutes_from_base_time)
15
+ )
16
+
17
+ with_time(base_time) do
18
+ described_class.api_key = api_key
19
+ expect(described_class[stpid]).to eq("2 minutes")
20
+ end
21
+ end
22
+
23
+ it "returns arriving when train is 0 minutes away" do
24
+ stub_cta_train_request(
25
+ api_key: api_key,
26
+ stpid: stpid,
27
+ response: arrival_response(base_time)
28
+ )
29
+
30
+ with_time(base_time) do
31
+ described_class.api_key = api_key
32
+ expect(described_class[stpid]).to eq("arriving")
33
+ end
34
+ end
35
+
36
+ it "returns invalid api_key message when api_key is nil" do
37
+ stub_cta_train_request(
38
+ api_key: nil,
39
+ stpid: stpid,
40
+ response: error_response
41
+ )
42
+
43
+ with_time(base_time) do
44
+ described_class.api_key = nil
45
+ expect { described_class[stpid]}.to raise_error("No data returned, please check your API key")
46
+ end
47
+ end
48
+
49
+ it "supresses output from CTA::TrainTracker gem" do
50
+ stub_cta_train_request(
51
+ api_key: nil,
52
+ stpid: stpid,
53
+ response: error_response
54
+ )
55
+
56
+ with_time(base_time) do
57
+ described_class.api_key = nil
58
+
59
+ out = capture_output do
60
+ expect { described_class[stpid]}.to raise_error
61
+ end
62
+
63
+ expect(out.string).to be_blank
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,43 @@
1
+ require "timecop"
2
+ require "rspec"
3
+ require 'webmock/rspec'
4
+
5
+ def with_time(time , &block)
6
+ Timecop.freeze(time) do
7
+ block.call
8
+ end
9
+ end
10
+
11
+ def arrival_response(arrival_time)
12
+ <<-RESPONSE
13
+ <ctatt>
14
+ <errCd>0</errCd>
15
+ <eta>
16
+ <arrT>#{arrival_time}</arrT>
17
+ </eta>
18
+ </ctatt>
19
+ RESPONSE
20
+ end
21
+
22
+ def error_response
23
+ <<-RESPONSE
24
+ <ctatt>
25
+ <errCd>101</errCd>
26
+ <errNm>Invalid API key</errNm>
27
+ </ctatt>
28
+ RESPONSE
29
+ end
30
+
31
+ def stub_cta_train_request(api_key:, stpid:, response:)
32
+ request_url = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=#{api_key}&stpid=#{stpid}"
33
+ stub_request(:get, request_url).to_return(body: response)
34
+ end
35
+
36
+ def capture_output
37
+ out = StringIO.new
38
+ $stdout = out
39
+ yield
40
+ return out
41
+ ensure
42
+ $stdout = STDOUT
43
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: next_train
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sandro Padin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cta-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: time_diff
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Find out when the next CTA train arrives.
126
+ email:
127
+ - sandropadin@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - Gemfile
133
+ - Gemfile.lock
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - lib/next_train.rb
138
+ - next_train.gemspec
139
+ - spec/next_train_spec.rb
140
+ - spec/spec_helper.rb
141
+ homepage: https://github.com/spadin/next-train
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.4.5
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Find out when the next CTA train arrives.
165
+ test_files:
166
+ - spec/next_train_spec.rb
167
+ - spec/spec_helper.rb