drifter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Widget
4
+ include Drifter::Location::Locatable
5
+ attr_accessor :lat, :lng
6
+ end
7
+
8
+
9
+ class LocatableTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @widget = Widget.new
13
+ end
14
+
15
+
16
+ # nodoc
17
+ def floats_equal?(f1, f2)
18
+ f1.to_s == f2.to_s
19
+ end
20
+
21
+
22
+ # nodoc
23
+ def test_location
24
+ @widget.lat = 10
25
+ @widget.lng = 20
26
+ loc = @widget.location
27
+ assert_equal 10, loc.lat
28
+ assert_equal 20, loc.lng
29
+
30
+ @widget.lat = nil
31
+ @widget.lng = nil
32
+ assert_equal nil, @widget.location.lat
33
+ assert_equal nil, @widget.location.lng
34
+ end
35
+
36
+
37
+ # nodoc
38
+ def test_location_setter
39
+ loc = Drifter::Location.new
40
+ loc.lat = 10
41
+ loc.lng = 20
42
+ @widget.location = loc
43
+ assert_equal 10, @widget.lat
44
+ assert_equal 20, @widget.lng
45
+
46
+ @widget.location = nil
47
+ assert_nil @widget.lat
48
+ assert_nil @widget.lng
49
+
50
+ @widget.location = [3,4]
51
+ assert_equal 3, @widget.lat
52
+ assert_equal 4, @widget.lng
53
+ end
54
+
55
+
56
+ def test_distance_to
57
+ assert_raise(ArgumentError) { @widget.distance_to nil }
58
+ assert_raise(ArgumentError) { @widget.distance_to "" }
59
+ assert_raise(ArgumentError) { @widget.distance_to 1 }
60
+
61
+ # london
62
+ @widget.lat = 51.5001524
63
+ @widget.lng = -0.1262362
64
+
65
+ # manchester
66
+ lat = 53.4807125
67
+ lng = -2.2343765
68
+
69
+ manchester = Widget.new
70
+ manchester.lat = lat
71
+ manchester.lng = lng
72
+
73
+ distance_in_miles = 162.941138493485
74
+ distance_in_km = 262.411019550554
75
+
76
+ # pass an array, default units
77
+ distance = @widget.distance_to [lat, lng]
78
+ assert floats_equal?(distance_in_miles, distance)
79
+
80
+ # location as array, units in miles
81
+ distance = @widget.distance_to [lat, lng], :units => :miles
82
+ assert floats_equal?(distance_in_miles, distance)
83
+
84
+ # location as array, units in km
85
+ distance = @widget.distance_to [lat, lng], :units => :km
86
+ assert floats_equal?(distance_in_km, distance)
87
+
88
+ # location as Locatable object, default units
89
+ distance = @widget.distance_to manchester
90
+ assert floats_equal?(distance_in_miles, distance)
91
+
92
+ # location as Locatable object, units in miles
93
+ distance = @widget.distance_to manchester, :units => :miles
94
+ assert floats_equal?(distance_in_miles, distance)
95
+
96
+ # location as Locatable object, units in km
97
+ distance = @widget.distance_to manchester, :units => :km
98
+ assert floats_equal?(distance_in_km, distance)
99
+ end
100
+
101
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class LocationTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @location = Drifter::Location.new
7
+ end
8
+
9
+
10
+ def test_data_method
11
+ # should return nil if raw_data is nil
12
+ @location.raw_data = nil
13
+ assert_nil @location.data
14
+
15
+ # should return a Hash if raw_data format is :hash
16
+ expected = { "foo" => "bar" }
17
+ @location.raw_data_format = :hash
18
+ @location.raw_data = expected
19
+ assert_equal expected, @location.data
20
+
21
+ # should return a Hash if raw_data_format is :json
22
+ @location.raw_data = expected.to_json
23
+ @location.raw_data_format = :json
24
+ assert_equal expected, @location.data
25
+ end
26
+
27
+ end
@@ -0,0 +1,4 @@
1
+ {
2
+ "status": "REQUEST_DENIED",
3
+ "results": [ ]
4
+ }
@@ -0,0 +1,478 @@
1
+ {
2
+ "status": "OK",
3
+ "results": [ {
4
+ "types": [ "locality", "political" ],
5
+ "formatted_address": "Springfield, MO, USA",
6
+ "address_components": [ {
7
+ "long_name": "Springfield",
8
+ "short_name": "Springfield",
9
+ "types": [ "locality", "political" ]
10
+ }, {
11
+ "long_name": "Greene",
12
+ "short_name": "Greene",
13
+ "types": [ "administrative_area_level_2", "political" ]
14
+ }, {
15
+ "long_name": "Missouri",
16
+ "short_name": "MO",
17
+ "types": [ "administrative_area_level_1", "political" ]
18
+ }, {
19
+ "long_name": "United States",
20
+ "short_name": "US",
21
+ "types": [ "country", "political" ]
22
+ } ],
23
+ "geometry": {
24
+ "location": {
25
+ "lat": 37.2153260,
26
+ "lng": -93.2982436
27
+ },
28
+ "location_type": "APPROXIMATE",
29
+ "viewport": {
30
+ "southwest": {
31
+ "lat": 37.1442056,
32
+ "lng": -93.4263030
33
+ },
34
+ "northeast": {
35
+ "lat": 37.2863794,
36
+ "lng": -93.1701842
37
+ }
38
+ },
39
+ "bounds": {
40
+ "southwest": {
41
+ "lat": 37.0874020,
42
+ "lng": -93.4140060
43
+ },
44
+ "northeast": {
45
+ "lat": 37.2708071,
46
+ "lng": -93.1798800
47
+ }
48
+ }
49
+ }
50
+ }, {
51
+ "types": [ "locality", "political" ],
52
+ "formatted_address": "Springfield, IL, USA",
53
+ "address_components": [ {
54
+ "long_name": "Springfield",
55
+ "short_name": "Springfield",
56
+ "types": [ "locality", "political" ]
57
+ }, {
58
+ "long_name": "Capital",
59
+ "short_name": "Capital",
60
+ "types": [ "administrative_area_level_3", "political" ]
61
+ }, {
62
+ "long_name": "Sangamon",
63
+ "short_name": "Sangamon",
64
+ "types": [ "administrative_area_level_2", "political" ]
65
+ }, {
66
+ "long_name": "Illinois",
67
+ "short_name": "IL",
68
+ "types": [ "administrative_area_level_1", "political" ]
69
+ }, {
70
+ "long_name": "United States",
71
+ "short_name": "US",
72
+ "types": [ "country", "political" ]
73
+ } ],
74
+ "geometry": {
75
+ "location": {
76
+ "lat": 39.7817213,
77
+ "lng": -89.6501481
78
+ },
79
+ "location_type": "APPROXIMATE",
80
+ "viewport": {
81
+ "southwest": {
82
+ "lat": 39.6536259,
83
+ "lng": -89.7731769
84
+ },
85
+ "northeast": {
86
+ "lat": 39.8740490,
87
+ "lng": -89.5684859
88
+ }
89
+ },
90
+ "bounds": {
91
+ "southwest": {
92
+ "lat": 39.6536259,
93
+ "lng": -89.7731769
94
+ },
95
+ "northeast": {
96
+ "lat": 39.8740490,
97
+ "lng": -89.5684859
98
+ }
99
+ }
100
+ }
101
+ }, {
102
+ "types": [ "locality", "political" ],
103
+ "formatted_address": "Springfield, MA, USA",
104
+ "address_components": [ {
105
+ "long_name": "Springfield",
106
+ "short_name": "Springfield",
107
+ "types": [ "locality", "political" ]
108
+ }, {
109
+ "long_name": "Hampden",
110
+ "short_name": "Hampden",
111
+ "types": [ "administrative_area_level_2", "political" ]
112
+ }, {
113
+ "long_name": "Massachusetts",
114
+ "short_name": "MA",
115
+ "types": [ "administrative_area_level_1", "political" ]
116
+ }, {
117
+ "long_name": "United States",
118
+ "short_name": "US",
119
+ "types": [ "country", "political" ]
120
+ } ],
121
+ "geometry": {
122
+ "location": {
123
+ "lat": 42.1014831,
124
+ "lng": -72.5898110
125
+ },
126
+ "location_type": "APPROXIMATE",
127
+ "viewport": {
128
+ "southwest": {
129
+ "lat": 42.0635950,
130
+ "lng": -72.6215340
131
+ },
132
+ "northeast": {
133
+ "lat": 42.1620859,
134
+ "lng": -72.4711490
135
+ }
136
+ },
137
+ "bounds": {
138
+ "southwest": {
139
+ "lat": 42.0635950,
140
+ "lng": -72.6215340
141
+ },
142
+ "northeast": {
143
+ "lat": 42.1620859,
144
+ "lng": -72.4711490
145
+ }
146
+ }
147
+ }
148
+ }, {
149
+ "types": [ "locality", "political" ],
150
+ "formatted_address": "Springfield, OH, USA",
151
+ "address_components": [ {
152
+ "long_name": "Springfield",
153
+ "short_name": "Springfield",
154
+ "types": [ "locality", "political" ]
155
+ }, {
156
+ "long_name": "Clark",
157
+ "short_name": "Clark",
158
+ "types": [ "administrative_area_level_2", "political" ]
159
+ }, {
160
+ "long_name": "Ohio",
161
+ "short_name": "OH",
162
+ "types": [ "administrative_area_level_1", "political" ]
163
+ }, {
164
+ "long_name": "United States",
165
+ "short_name": "US",
166
+ "types": [ "country", "political" ]
167
+ } ],
168
+ "geometry": {
169
+ "location": {
170
+ "lat": 39.9242266,
171
+ "lng": -83.8088171
172
+ },
173
+ "location_type": "APPROXIMATE",
174
+ "viewport": {
175
+ "southwest": {
176
+ "lat": 39.8878549,
177
+ "lng": -83.8696619
178
+ },
179
+ "northeast": {
180
+ "lat": 39.9828569,
181
+ "lng": -83.7034240
182
+ }
183
+ },
184
+ "bounds": {
185
+ "southwest": {
186
+ "lat": 39.8878549,
187
+ "lng": -83.8696619
188
+ },
189
+ "northeast": {
190
+ "lat": 39.9828569,
191
+ "lng": -83.7034240
192
+ }
193
+ }
194
+ }
195
+ }, {
196
+ "types": [ "locality", "political" ],
197
+ "formatted_address": "Springfield, OR, USA",
198
+ "address_components": [ {
199
+ "long_name": "Springfield",
200
+ "short_name": "Springfield",
201
+ "types": [ "locality", "political" ]
202
+ }, {
203
+ "long_name": "Eugene-Springfield",
204
+ "short_name": "Eugene-Springfield",
205
+ "types": [ "administrative_area_level_3", "political" ]
206
+ }, {
207
+ "long_name": "Lane",
208
+ "short_name": "Lane",
209
+ "types": [ "administrative_area_level_2", "political" ]
210
+ }, {
211
+ "long_name": "Oregon",
212
+ "short_name": "OR",
213
+ "types": [ "administrative_area_level_1", "political" ]
214
+ }, {
215
+ "long_name": "United States",
216
+ "short_name": "US",
217
+ "types": [ "country", "political" ]
218
+ } ],
219
+ "geometry": {
220
+ "location": {
221
+ "lat": 44.0462362,
222
+ "lng": -123.0220289
223
+ },
224
+ "location_type": "APPROXIMATE",
225
+ "viewport": {
226
+ "southwest": {
227
+ "lat": 44.0272439,
228
+ "lng": -123.0508960
229
+ },
230
+ "northeast": {
231
+ "lat": 44.0954590,
232
+ "lng": -122.8797300
233
+ }
234
+ },
235
+ "bounds": {
236
+ "southwest": {
237
+ "lat": 44.0272439,
238
+ "lng": -123.0508960
239
+ },
240
+ "northeast": {
241
+ "lat": 44.0954590,
242
+ "lng": -122.8797300
243
+ }
244
+ }
245
+ }
246
+ }, {
247
+ "types": [ "locality", "political" ],
248
+ "formatted_address": "Springfield, VA, USA",
249
+ "address_components": [ {
250
+ "long_name": "Springfield",
251
+ "short_name": "Springfield",
252
+ "types": [ "locality", "political" ]
253
+ }, {
254
+ "long_name": "Lee",
255
+ "short_name": "Lee",
256
+ "types": [ "administrative_area_level_3", "political" ]
257
+ }, {
258
+ "long_name": "Fairfax",
259
+ "short_name": "Fairfax",
260
+ "types": [ "administrative_area_level_2", "political" ]
261
+ }, {
262
+ "long_name": "Virginia",
263
+ "short_name": "VA",
264
+ "types": [ "administrative_area_level_1", "political" ]
265
+ }, {
266
+ "long_name": "United States",
267
+ "short_name": "US",
268
+ "types": [ "country", "political" ]
269
+ } ],
270
+ "geometry": {
271
+ "location": {
272
+ "lat": 38.7892801,
273
+ "lng": -77.1872036
274
+ },
275
+ "location_type": "APPROXIMATE",
276
+ "viewport": {
277
+ "southwest": {
278
+ "lat": 38.7380689,
279
+ "lng": -77.2189470
280
+ },
281
+ "northeast": {
282
+ "lat": 38.8153930,
283
+ "lng": -77.1344910
284
+ }
285
+ },
286
+ "bounds": {
287
+ "southwest": {
288
+ "lat": 38.7380689,
289
+ "lng": -77.2189470
290
+ },
291
+ "northeast": {
292
+ "lat": 38.8153930,
293
+ "lng": -77.1344910
294
+ }
295
+ }
296
+ }
297
+ }, {
298
+ "types": [ "locality", "political" ],
299
+ "formatted_address": "Springfield, AR, USA",
300
+ "address_components": [ {
301
+ "long_name": "Springfield",
302
+ "short_name": "Springfield",
303
+ "types": [ "locality", "political" ]
304
+ }, {
305
+ "long_name": "Union",
306
+ "short_name": "Union",
307
+ "types": [ "administrative_area_level_3", "political" ]
308
+ }, {
309
+ "long_name": "Conway",
310
+ "short_name": "Conway",
311
+ "types": [ "administrative_area_level_2", "political" ]
312
+ }, {
313
+ "long_name": "Arkansas",
314
+ "short_name": "AR",
315
+ "types": [ "administrative_area_level_1", "political" ]
316
+ }, {
317
+ "long_name": "United States",
318
+ "short_name": "US",
319
+ "types": [ "country", "political" ]
320
+ } ],
321
+ "geometry": {
322
+ "location": {
323
+ "lat": 35.2675809,
324
+ "lng": -92.5576595
325
+ },
326
+ "location_type": "APPROXIMATE",
327
+ "viewport": {
328
+ "southwest": {
329
+ "lat": 35.2644333,
330
+ "lng": -92.5608071
331
+ },
332
+ "northeast": {
333
+ "lat": 35.2707285,
334
+ "lng": -92.5545119
335
+ }
336
+ }
337
+ }
338
+ }, {
339
+ "types": [ "locality", "political" ],
340
+ "formatted_address": "Springfield, TN, USA",
341
+ "address_components": [ {
342
+ "long_name": "Springfield",
343
+ "short_name": "Springfield",
344
+ "types": [ "locality", "political" ]
345
+ }, {
346
+ "long_name": "Springfield-Greenbrier",
347
+ "short_name": "Springfield-Greenbrier",
348
+ "types": [ "administrative_area_level_3", "political" ]
349
+ }, {
350
+ "long_name": "Robertson",
351
+ "short_name": "Robertson",
352
+ "types": [ "administrative_area_level_2", "political" ]
353
+ }, {
354
+ "long_name": "Tennessee",
355
+ "short_name": "TN",
356
+ "types": [ "administrative_area_level_1", "political" ]
357
+ }, {
358
+ "long_name": "United States",
359
+ "short_name": "US",
360
+ "types": [ "country", "political" ]
361
+ } ],
362
+ "geometry": {
363
+ "location": {
364
+ "lat": 36.5092118,
365
+ "lng": -86.8849984
366
+ },
367
+ "location_type": "APPROXIMATE",
368
+ "viewport": {
369
+ "southwest": {
370
+ "lat": 36.4448900,
371
+ "lng": -86.9446749
372
+ },
373
+ "northeast": {
374
+ "lat": 36.5500560,
375
+ "lng": -86.8142039
376
+ }
377
+ },
378
+ "bounds": {
379
+ "southwest": {
380
+ "lat": 36.4448900,
381
+ "lng": -86.9446749
382
+ },
383
+ "northeast": {
384
+ "lat": 36.5500560,
385
+ "lng": -86.8142039
386
+ }
387
+ }
388
+ }
389
+ }, {
390
+ "types": [ "locality", "political" ],
391
+ "formatted_address": "Springfield, WV 26763, USA",
392
+ "address_components": [ {
393
+ "long_name": "Springfield",
394
+ "short_name": "Springfield",
395
+ "types": [ "locality", "political" ]
396
+ }, {
397
+ "long_name": "Hampshire",
398
+ "short_name": "Hampshire",
399
+ "types": [ "administrative_area_level_2", "political" ]
400
+ }, {
401
+ "long_name": "West Virginia",
402
+ "short_name": "WV",
403
+ "types": [ "administrative_area_level_1", "political" ]
404
+ }, {
405
+ "long_name": "United States",
406
+ "short_name": "US",
407
+ "types": [ "country", "political" ]
408
+ }, {
409
+ "long_name": "26763",
410
+ "short_name": "26763",
411
+ "types": [ "postal_code" ]
412
+ } ],
413
+ "geometry": {
414
+ "location": {
415
+ "lat": 39.4500000,
416
+ "lng": -78.6933330
417
+ },
418
+ "location_type": "APPROXIMATE",
419
+ "viewport": {
420
+ "southwest": {
421
+ "lat": 39.4468524,
422
+ "lng": -78.6964806
423
+ },
424
+ "northeast": {
425
+ "lat": 39.4531476,
426
+ "lng": -78.6901854
427
+ }
428
+ }
429
+ }
430
+ }, {
431
+ "types": [ "locality", "political" ],
432
+ "formatted_address": "Springfield, PA, USA",
433
+ "address_components": [ {
434
+ "long_name": "Springfield",
435
+ "short_name": "Springfield",
436
+ "types": [ "locality", "political" ]
437
+ }, {
438
+ "long_name": "Delaware",
439
+ "short_name": "Delaware",
440
+ "types": [ "administrative_area_level_2", "political" ]
441
+ }, {
442
+ "long_name": "Pennsylvania",
443
+ "short_name": "PA",
444
+ "types": [ "administrative_area_level_1", "political" ]
445
+ }, {
446
+ "long_name": "United States",
447
+ "short_name": "US",
448
+ "types": [ "country", "political" ]
449
+ } ],
450
+ "geometry": {
451
+ "location": {
452
+ "lat": 39.9306677,
453
+ "lng": -75.3201878
454
+ },
455
+ "location_type": "APPROXIMATE",
456
+ "viewport": {
457
+ "southwest": {
458
+ "lat": 39.8865119,
459
+ "lng": -75.3668850
460
+ },
461
+ "northeast": {
462
+ "lat": 39.9551109,
463
+ "lng": -75.3076310
464
+ }
465
+ },
466
+ "bounds": {
467
+ "southwest": {
468
+ "lat": 39.8865119,
469
+ "lng": -75.3668850
470
+ },
471
+ "northeast": {
472
+ "lat": 39.9551109,
473
+ "lng": -75.3076310
474
+ }
475
+ }
476
+ }
477
+ } ]
478
+ }