geokit 1.7.0.rc1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ module Geokit
2
+ module Geocoders
3
+ # Yandex geocoder implementation. Expects the Geokit::Geocoders::YANDEX variable to
4
+ # contain a Yandex API key (optional). Conforms to the interface set by the Geocoder class.
5
+ class YandexGeocoder < Geocoder
6
+ private
7
+
8
+ # Template method which does the geocode lookup.
9
+ def self.do_geocode(address, options = {})
10
+ address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
11
+ url = "http://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector::url_escape(address_str)}&format=json"
12
+ url += "&key=#{Geokit::Geocoders::yandex}" if Geokit::Geocoders::yandex != 'REPLACE_WITH_YOUR_YANDEX_KEY'
13
+ res = self.call_geocoder_service(url)
14
+ return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
15
+ json = res.body
16
+ # logger.debug "Yandex geocoding. Address: #{address}. Result: #{json}"
17
+ return self.json2GeoLoc(json, address)
18
+ end
19
+
20
+ def self.json2GeoLoc(json, address)
21
+ geoloc = GeoLoc.new
22
+
23
+ result = MultiJson.load(json)
24
+ if result["response"]["GeoObjectCollection"]["metaDataProperty"]["GeocoderResponseMetaData"]["found"].to_i > 0
25
+ loc = result["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]
26
+
27
+ geoloc.success = true
28
+ geoloc.provider = "yandex"
29
+ geoloc.lng = loc["Point"]["pos"].split(" ").first
30
+ geoloc.lat = loc["Point"]["pos"].split(" ").last
31
+ geoloc.country_code = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["CountryNameCode"]
32
+ geoloc.full_address = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AddressLine"]
33
+ geoloc.street_address = loc["name"]
34
+
35
+ locality = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["Locality"] || loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AdministrativeArea"]["Locality"] || loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"]["Locality"] rescue nil
36
+ geoloc.street_number = locality["Thoroughfare"]["Premise"]["PremiseNumber"] rescue nil
37
+ geoloc.street_name = locality["Thoroughfare"]["ThoroughfareName"] rescue nil
38
+ geoloc.city = locality["LocalityName"] rescue nil
39
+ geoloc.state = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AdministrativeArea"]["AdministrativeAreaName"] rescue nil
40
+ geoloc.state ||= loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["Locality"]["LocalityName"] rescue nil
41
+ geoloc.precision = loc["metaDataProperty"]["GeocoderMetaData"]["precision"].sub(/exact/, "building").sub(/number|near/, "address").sub(/other/, "city")
42
+ geoloc.precision = "country" unless locality
43
+ else
44
+ logger.info "Yandex was unable to geocode address: " + address
45
+ end
46
+
47
+ return geoloc
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module Geokit
2
- VERSION = '1.7.0.rc1'
2
+ VERSION = '1.7.0'
3
3
  end
data/test/helper.rb CHANGED
@@ -8,6 +8,8 @@ rescue LoadError => e
8
8
  puts "Error loading bundler (#{e.message}): \"gem install bundler\" for bundler support."
9
9
  end
10
10
 
11
+ require 'geoip'
12
+
11
13
  if ENV['COVERAGE']
12
14
  COVERAGE_THRESHOLD = 84
13
15
  require 'simplecov'
@@ -16,7 +18,6 @@ if ENV['COVERAGE']
16
18
  Coveralls.wear!
17
19
 
18
20
  SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
19
- SimpleCov.start
20
21
  SimpleCov.start do
21
22
  add_filter '/test/'
22
23
  add_group 'lib', 'lib'
@@ -49,6 +50,51 @@ class MockFailure < Net::HTTPServiceUnavailable #:nodoc: all
49
50
  end
50
51
  end
51
52
 
53
+ class TestHelper
54
+ def self.last_url(url)
55
+ @@url = url
56
+ end
57
+ def self.get_last_url
58
+ @@url
59
+ end
60
+ end
61
+
62
+ Geokit::Geocoders::Geocoder.class_eval do
63
+ class << self
64
+ def call_geocoder_service_for_test(url)
65
+ TestHelper.last_url(url)
66
+ call_geocoder_service_old(url)
67
+ end
68
+
69
+ alias call_geocoder_service_old call_geocoder_service
70
+ alias call_geocoder_service call_geocoder_service_for_test
71
+ end
72
+ end
73
+
74
+ def assert_array_in_delta(expected_array, actual_array, delta = 0.001, message='')
75
+ full_message = build_message(message, "<?> and\n<?> expected to be within\n<?> of each other.\n", expected_array, actual_array, delta)
76
+ assert_block(full_message) do
77
+ expected_array.zip(actual_array).all?{|expected_item, actual_item|
78
+ (expected_item.to_f - actual_item.to_f).abs <= delta.to_f
79
+ }
80
+ end
81
+ end
82
+
83
+ require 'vcr'
84
+
85
+ VCR.configure do |c|
86
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
87
+ c.hook_into :webmock # or :fakeweb
88
+ # Yahoo BOSS Ignore changing params
89
+ c.default_cassette_options = {
90
+ :match_requests_on => [:method,
91
+ VCR.request_matchers.uri_without_params(
92
+ :oauth_nonce, :oauth_timestamp, :oauth_signature
93
+ )
94
+ ]
95
+ }
96
+ end
97
+
52
98
  # Base class for testing geocoders.
53
99
  class BaseGeocoderTest < Test::Unit::TestCase #:nodoc: all
54
100
 
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.join(File.dirname(__FILE__), 'helper')
2
3
 
3
4
  Geokit::Geocoders::google = 'Google'
@@ -5,383 +6,8 @@ Geokit::Geocoders::google_client_id = nil
5
6
  Geokit::Geocoders::google_cryptographic_key = nil
6
7
  Geokit::Geocoders::google_channel = nil
7
8
 
8
-
9
9
  class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
10
10
 
11
- GOOGLE3_FULL=%q/
12
- {
13
- "results" : [
14
- {
15
- "address_components" : [
16
- {
17
- "long_name" : "5",
18
- "short_name" : "5",
19
- "types" : [ "subpremise" ]
20
- },
21
- {
22
- "long_name" : "100",
23
- "short_name" : "100",
24
- "types" : [ "street_number" ]
25
- },
26
- {
27
- "long_name" : "Spear St",
28
- "short_name" : "Spear St",
29
- "types" : [ "route" ]
30
- },
31
- {
32
- "long_name" : "South Beach",
33
- "short_name" : "South Beach",
34
- "types" : [ "neighborhood", "political" ]
35
- },
36
- {
37
- "long_name" : "San Francisco",
38
- "short_name" : "SF",
39
- "types" : [ "locality", "political" ]
40
- },
41
- {
42
- "long_name" : "San Francisco",
43
- "short_name" : "San Francisco",
44
- "types" : [ "administrative_area_level_2", "political" ]
45
- },
46
- {
47
- "long_name" : "California",
48
- "short_name" : "CA",
49
- "types" : [ "administrative_area_level_1", "political" ]
50
- },
51
- {
52
- "long_name" : "United States",
53
- "short_name" : "US",
54
- "types" : [ "country", "political" ]
55
- },
56
- {
57
- "long_name" : "94105",
58
- "short_name" : "94105",
59
- "types" : [ "postal_code" ]
60
- }
61
- ],
62
- "formatted_address" : "100 Spear St #5, San Francisco, CA 94105, USA",
63
- "geometry" : {
64
- "location" : {
65
- "lat" : 37.79215090,
66
- "lng" : -122.3940
67
- },
68
- "location_type" : "APPROXIMATE",
69
- "viewport" : {
70
- "northeast" : {
71
- "lat" : 37.79349988029150,
72
- "lng" : -122.3926510197085
73
- },
74
- "southwest" : {
75
- "lat" : 37.79080191970850,
76
- "lng" : -122.3953489802915
77
- }
78
- }
79
- },
80
- "partial_match" : true,
81
- "types" : [ "subpremise" ]
82
- }
83
- ],
84
- "status" : "OK"
85
- }
86
- /.strip
87
-
88
- GOOGLE3_CITY=%q/
89
- {
90
- "status": "OK",
91
- "results": [ {
92
- "types": [ "locality", "political" ],
93
- "formatted_address": "San Francisco, CA, USA",
94
- "address_components": [ {
95
- "long_name": "San Francisco",
96
- "short_name": "San Francisco",
97
- "types": [ "locality", "political" ]
98
- }, {
99
- "long_name": "San Francisco",
100
- "short_name": "San Francisco",
101
- "types": [ "administrative_area_level_2", "political" ]
102
- }, {
103
- "long_name": "California",
104
- "short_name": "CA",
105
- "types": [ "administrative_area_level_1", "political" ]
106
- }, {
107
- "long_name": "United States",
108
- "short_name": "US",
109
- "types": [ "country", "political" ]
110
- } ],
111
- "geometry": {
112
- "location": {
113
- "lat": 37.7749295,
114
- "lng": -122.4194155
115
- },
116
- "location_type": "APPROXIMATE",
117
- "viewport": {
118
- "southwest": {
119
- "lat": 37.7043396,
120
- "lng": -122.5474749
121
- },
122
- "northeast": {
123
- "lat": 37.8454521,
124
- "lng": -122.2913561
125
- }
126
- },
127
- "bounds": {
128
- "southwest": {
129
- "lat": 37.7034000,
130
- "lng": -122.5270000
131
- },
132
- "northeast": {
133
- "lat": 37.8120000,
134
- "lng": -122.3482000
135
- }
136
- }
137
- }
138
- } ]
139
- }
140
- /.strip
141
- GOOGLE3_MULTI=%q/
142
- {
143
- "status": "OK",
144
- "results": [ {
145
- "types": [ "street_address" ],
146
- "formatted_address": "Via Sandro Pertini, 8, 20010 Mesero MI, Italy",
147
- "address_components": [ {
148
- "long_name": "8",
149
- "short_name": "8",
150
- "types": [ "street_number" ]
151
- }, {
152
- "long_name": "Via Sandro Pertini",
153
- "short_name": "Via Sandro Pertini",
154
- "types": [ "route" ]
155
- }, {
156
- "long_name": "Mesero",
157
- "short_name": "Mesero",
158
- "types": [ "locality", "political" ]
159
- }, {
160
- "long_name": "Milan",
161
- "short_name": "MI",
162
- "types": [ "administrative_area_level_2", "political" ]
163
- }, {
164
- "long_name": "Lombardy",
165
- "short_name": "Lombardy",
166
- "types": [ "administrative_area_level_1", "political" ]
167
- }, {
168
- "long_name": "Italy",
169
- "short_name": "IT",
170
- "types": [ "country", "political" ]
171
- }, {
172
- "long_name": "20010",
173
- "short_name": "20010",
174
- "types": [ "postal_code" ]
175
- } ],
176
- "geometry": {
177
- "location": {
178
- "lat": 45.4966218,
179
- "lng": 8.8526940
180
- },
181
- "location_type": "RANGE_INTERPOLATED",
182
- "viewport": {
183
- "southwest": {
184
- "lat": 45.4934754,
185
- "lng": 8.8495559
186
- },
187
- "northeast": {
188
- "lat": 45.4997707,
189
- "lng": 8.8558512
190
- }
191
- },
192
- "bounds": {
193
- "southwest": {
194
- "lat": 45.4966218,
195
- "lng": 8.8526940
196
- },
197
- "northeast": {
198
- "lat": 45.4966243,
199
- "lng": 8.8527131
200
- }
201
- }
202
- },
203
- "partial_match": true
204
- },
205
- {
206
- "types": [ "route" ],
207
- "formatted_address": "Via Sandro Pertini, 20010 Ossona MI, Italy",
208
- "address_components": [ {
209
- "long_name": "Via Sandro Pertini",
210
- "short_name": "Via Sandro Pertini",
211
- "types": [ "route" ]
212
- }, {
213
- "long_name": "Ossona",
214
- "short_name": "Ossona",
215
- "types": [ "locality", "political" ]
216
- }, {
217
- "long_name": "Milan",
218
- "short_name": "MI",
219
- "types": [ "administrative_area_level_2", "political" ]
220
- }, {
221
- "long_name": "Lombardy",
222
- "short_name": "Lombardy",
223
- "types": [ "administrative_area_level_1", "political" ]
224
- }, {
225
- "long_name": "Italy",
226
- "short_name": "IT",
227
- "types": [ "country", "political" ]
228
- }, {
229
- "long_name": "20010",
230
- "short_name": "20010",
231
- "types": [ "postal_code" ]
232
- } ],
233
- "geometry": {
234
- "location": {
235
- "lat": 45.5074444,
236
- "lng": 8.9023200
237
- },
238
- "location_type": "GEOMETRIC_CENTER",
239
- "viewport": {
240
- "southwest": {
241
- "lat": 45.5043320,
242
- "lng": 8.8990670
243
- },
244
- "northeast": {
245
- "lat": 45.5106273,
246
- "lng": 8.9053622
247
- }
248
- },
249
- "bounds": {
250
- "southwest": {
251
- "lat": 45.5064427,
252
- "lng": 8.9020024
253
- },
254
- "northeast": {
255
- "lat": 45.5085166,
256
- "lng": 8.9024268
257
- }
258
- }
259
- }
260
- }
261
- ]
262
- }
263
- /.strip
264
- GOOGLE3_REVERSE_MADRID=%q/
265
- {
266
- "status": "OK",
267
- "results": [ {
268
- "types": [ ],
269
- "formatted_address": "Calle de las Carretas, 28013 Madrid, Spain",
270
- "address_components": [ {
271
- "long_name": "Calle de las Carretas",
272
- "short_name": "Calle de las Carretas",
273
- "types": [ "route" ]
274
- }, {
275
- "long_name": "Madrid",
276
- "short_name": "Madrid",
277
- "types": [ "locality", "political" ]
278
- }, {
279
- "long_name": "Madrid",
280
- "short_name": "M",
281
- "types": [ "administrative_area_level_2", "political" ]
282
- }, {
283
- "long_name": "Madrid",
284
- "short_name": "Madrid",
285
- "types": [ "administrative_area_level_1", "political" ]
286
- }, {
287
- "long_name": "Spain",
288
- "short_name": "ES",
289
- "types": [ "country", "political" ]
290
- }, {
291
- "long_name": "28013",
292
- "short_name": "28013",
293
- "types": [ "postal_code" ]
294
- } ],
295
- "geometry": {
296
- "location": {
297
- "lat": 40.4166824,
298
- "lng": -3.7033411
299
- },
300
- "location_type": "APPROXIMATE",
301
- "viewport": {
302
- "southwest": {
303
- "lat": 40.4135351,
304
- "lng": -3.7064880
305
- },
306
- "northeast": {
307
- "lat": 40.4198303,
308
- "lng": -3.7001927
309
- }
310
- },
311
- "bounds": {
312
- "southwest": {
313
- "lat": 40.4166419,
314
- "lng": -3.7033685
315
- },
316
- "northeast": {
317
- "lat": 40.4167235,
318
- "lng": -3.7033122
319
- }
320
- }
321
- }
322
- } ]
323
- }
324
- /
325
- GOOGLE3_COUNTRY_CODE_BIASED_RESULT=%q/
326
- {
327
- "status": "OK",
328
- "results": [ {
329
- "types": [ "administrative_area_level_2", "political" ],
330
- "formatted_address": "Syracuse, Italy",
331
- "address_components": [ {
332
- "long_name": "Syracuse",
333
- "short_name": "SR",
334
- "types": [ "administrative_area_level_2", "political" ]
335
- }, {
336
- "long_name": "Sicily",
337
- "short_name": "Sicily",
338
- "types": [ "administrative_area_level_1", "political" ]
339
- }, {
340
- "long_name": "Italy",
341
- "short_name": "IT",
342
- "types": [ "country", "political" ]
343
- } ],
344
- "geometry": {
345
- "location": {
346
- "lat": 37.0630218,
347
- "lng": 14.9856176
348
- },
349
- "location_type": "APPROXIMATE",
350
- "viewport": {
351
- "southwest": {
352
- "lat": 36.7775664,
353
- "lng": 14.4733800
354
- },
355
- "northeast": {
356
- "lat": 37.3474070,
357
- "lng": 15.4978552
358
- }
359
- },
360
- "bounds": {
361
- "southwest": {
362
- "lat": 36.6441736,
363
- "lng": 14.7724913
364
- },
365
- "northeast": {
366
- "lat": 37.4125978,
367
- "lng": 15.3367367
368
- }
369
- }
370
- }
371
- } ]
372
- }
373
- /
374
- GOOGLE3_TOO_MANY=%q/
375
- {
376
- "status": "OVER_QUERY_LIMIT"
377
- }
378
- /
379
- GOOGLE3_INVALID_REQUEST=%q/
380
- {
381
- "results" : [],
382
- "status" : "INVALID_REQUEST"
383
- }
384
- /
385
11
  def setup
386
12
  super
387
13
  @full_address = '100 Spear St Apt. 5, San Francisco, CA, 94105-1522, US'
@@ -415,50 +41,48 @@ class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
415
41
  assert_equal 'http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=', url
416
42
  end
417
43
 
418
-
419
44
  def test_google3_full_address
420
- response = MockSuccess.new
421
- response.expects(:body).returns(GOOGLE3_FULL)
45
+ VCR.use_cassette('google3_full_short') do
422
46
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
423
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
47
+ TestHelper.expects(:last_url).with(url)
424
48
  res=Geokit::Geocoders::GoogleGeocoder3.geocode(@address)
425
49
  assert_equal "CA", res.state
426
50
  assert_equal "San Francisco", res.city
427
- assert_equal "37.7921509,-122.394", res.ll # slightly dif from yahoo
51
+ assert_array_in_delta [37.7749295, -122.4194155], res.to_a # slightly dif from yahoo
428
52
  assert res.is_us?
429
- assert_equal "100 Spear St #5, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
53
+ assert_equal "San Francisco, CA, USA", res.full_address #slightly different from yahoo
430
54
  assert_equal "google3", res.provider
55
+ end
431
56
  end
432
57
 
433
58
  def test_google3_full_address_with_geo_loc
434
- response = MockSuccess.new
435
- response.expects(:body).returns(GOOGLE3_FULL)
59
+ VCR.use_cassette('google3_full') do
436
60
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
437
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
61
+ TestHelper.expects(:last_url).with(url)
438
62
  res=Geokit::Geocoders::GoogleGeocoder3.geocode(@google_full_loc)
439
63
  assert_equal "CA", res.state
440
64
  assert_equal "San Francisco", res.city
441
- assert_equal "37.7921509,-122.394", res.ll # slightly dif from yahoo
65
+ assert_array_in_delta [37.7921509, -122.394], res.to_a # slightly dif from yahoo
442
66
  assert res.is_us?
443
- assert_equal "100 Spear St #5, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
67
+ assert_equal "100 Spear Street #5, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
444
68
  assert_equal "google3", res.provider
69
+ end
445
70
  end
446
71
 
447
72
  def test_google3_full_address_accuracy
448
- response = MockSuccess.new
449
- response.expects(:body).returns(GOOGLE3_FULL)
73
+ VCR.use_cassette('google3_full') do
450
74
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
451
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
75
+ TestHelper.expects(:last_url).with(url)
452
76
  res=Geokit::Geocoders::GoogleGeocoder3.geocode(@google_full_loc)
453
77
 
454
78
  assert_equal 9, res.accuracy
79
+ end
455
80
  end
456
81
 
457
82
  def test_google3_city
458
- response = MockSuccess.new
459
- response.expects(:body).returns(GOOGLE3_CITY)
83
+ VCR.use_cassette('google3_city') do
460
84
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
461
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
85
+ TestHelper.expects(:last_url).with(url)
462
86
  res=Geokit::Geocoders::GoogleGeocoder3.do_geocode(@address)
463
87
  assert_nil res.street_address
464
88
  assert_equal "CA", res.state
@@ -467,22 +91,22 @@ class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
467
91
  assert res.is_us?
468
92
  assert_equal "San Francisco, CA, USA", res.full_address
469
93
  assert_equal "google3", res.provider
94
+ end
470
95
  end
471
96
 
472
97
  def test_google3_city_accuracy
473
- response = MockSuccess.new
474
- response.expects(:body).returns(GOOGLE3_CITY)
98
+ VCR.use_cassette('google3_city') do
475
99
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
476
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
100
+ TestHelper.expects(:last_url).with(url)
477
101
  res=Geokit::Geocoders::GoogleGeocoder3.geocode(@address)
478
102
  assert_equal 4, res.accuracy
103
+ end
479
104
  end
480
105
 
481
106
  def test_google3_city_with_geo_loc
482
- response = MockSuccess.new
483
- response.expects(:body).returns(GOOGLE3_CITY)
107
+ VCR.use_cassette('google3_city') do
484
108
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
485
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
109
+ TestHelper.expects(:last_url).with(url)
486
110
  res=Geokit::Geocoders::GoogleGeocoder3.geocode(@google_city_loc)
487
111
  assert_equal "CA", res.state
488
112
  assert_equal "San Francisco", res.city
@@ -491,19 +115,30 @@ class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
491
115
  assert_equal "San Francisco, CA, USA", res.full_address
492
116
  assert_nil res.street_address
493
117
  assert_equal "google3", res.provider
118
+ end
494
119
  end
495
120
 
496
121
  def test_google3_suggested_bounds
497
- response = MockSuccess.new
498
- response.expects(:body).returns(GOOGLE3_FULL)
122
+ VCR.use_cassette('google3_full') do
499
123
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
500
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
124
+ TestHelper.expects(:last_url).with(url)
501
125
  res = Geokit::Geocoders::GoogleGeocoder3.geocode(@google_full_loc)
502
-
503
126
  assert_instance_of Geokit::Bounds, res.suggested_bounds
504
- assert_equal Geokit::Bounds.new(Geokit::LatLng.new(37.7908019197085, -122.3953489802915), Geokit::LatLng.new(37.7934998802915, -122.3926510197085)), res.suggested_bounds
127
+ assert_array_in_delta [37.7908019197085, -122.3953489802915], res.suggested_bounds.sw.to_a
128
+ assert_array_in_delta [37.7934998802915, -122.3926510197085], res.suggested_bounds.ne.to_a
129
+ end
505
130
  end
506
131
 
132
+ def test_google3_suggested_bounds_url
133
+ bounds = Geokit::Bounds.new(
134
+ Geokit::LatLng.new(33.7036917, -118.6681759),
135
+ Geokit::LatLng.new(34.3373061, -118.1552891)
136
+ )
137
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Winnetka&bounds=33.7036917%2C-118.6681759%7C34.3373061%2C-118.1552891"
138
+ Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url)
139
+ Geokit::Geocoders::GoogleGeocoder3.geocode('Winnetka', :bias => bounds)
140
+ end
141
+
507
142
  def test_service_unavailable
508
143
  response = MockFailure.new
509
144
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
@@ -512,40 +147,37 @@ class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
512
147
  end
513
148
 
514
149
  def test_multiple_results
515
- #Geokit::Geocoders::GoogleGeocoder3.do_geocode('via Sandro Pertini 8, Ossona, MI')
516
- response = MockSuccess.new
517
- response.expects(:body).returns(GOOGLE3_MULTI)
150
+ VCR.use_cassette('google3_multi') do
518
151
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape('via Sandro Pertini 8, Ossona, MI')}"
519
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
152
+ TestHelper.expects(:last_url).with(url)
520
153
  res=Geokit::Geocoders::GoogleGeocoder3.geocode('via Sandro Pertini 8, Ossona, MI')
154
+ assert_equal 5, res.all.size
155
+ res = res.all[0]
521
156
  assert_equal "Lombardy", res.state
522
157
  assert_equal "Mesero", res.city
523
- assert_equal "45.4966218,8.852694", res.ll
158
+ assert_array_in_delta [45.4966218, 8.852694], res.to_a
524
159
  assert !res.is_us?
525
- assert_equal "Via Sandro Pertini, 8, 20010 Mesero MI, Italy", res.full_address
160
+ assert_equal "Via Sandro Pertini, 8, 20010 Mesero Milan, Italy", res.full_address
526
161
  assert_equal "8 Via Sandro Pertini", res.street_address
527
162
  assert_equal "google3", res.provider
528
163
 
529
- assert_equal 2, res.all.size
530
- res = res.all[1]
164
+ res = res.all[4]
531
165
  assert_equal "Lombardy", res.state
532
166
  assert_equal "Ossona", res.city
533
- assert_equal "45.5074444,8.90232", res.ll
167
+ assert_array_in_delta [45.5074444, 8.90232], res.to_a
534
168
  assert !res.is_us?
535
- assert_equal "Via Sandro Pertini, 20010 Ossona MI, Italy", res.full_address
536
- assert_equal "Via Sandro Pertini", res.street_address
169
+ assert_equal "Via S. Pertini, 20010 Ossona Milan, Italy", res.full_address
170
+ assert_equal "Via S. Pertini", res.street_address
537
171
  assert_equal "google3", res.provider
172
+ end
538
173
  end
539
174
  #
540
175
  def test_reverse_geocode
541
- #Geokit::Geocoders::GoogleGeocoder3.do_reverse_geocode("40.4167413, -3.7032498")
176
+ VCR.use_cassette('google3_reverse_madrid') do
542
177
  madrid = Geokit::GeoLoc.new
543
178
  madrid.lat, madrid.lng = "40.4167413", "-3.7032498"
544
- response = MockSuccess.new
545
- response.expects(:body).returns(GOOGLE3_REVERSE_MADRID)
546
179
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=#{Geokit::Inflector::url_escape(madrid.ll)}"
547
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).
548
- returns(response)
180
+ TestHelper.expects(:last_url).with(url)
549
181
  res=Geokit::Geocoders::GoogleGeocoder3.do_reverse_geocode(madrid.ll)
550
182
 
551
183
  assert_equal madrid.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5)
@@ -554,32 +186,39 @@ class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
554
186
  assert_equal "google3", res.provider
555
187
 
556
188
  assert_equal "Madrid", res.city
557
- assert_equal "Madrid", res.state
189
+ assert_equal "Community of Madrid", res.state
558
190
 
559
191
  assert_equal "Spain", res.country
560
- assert_equal "street", res.precision
561
- assert_equal true, res.success
562
-
563
- assert_equal "Calle de las Carretas, 28013 Madrid, Spain", res.full_address
564
192
  assert_equal "28013", res.zip
565
- assert_equal "Calle de las Carretas", res.street_address
193
+ assert_equal true, res.success
194
+ end
566
195
  end
567
196
 
568
197
  def test_country_code_biasing
569
- response = MockSuccess.new
570
- response.expects(:body).returns(GOOGLE3_COUNTRY_CODE_BIASED_RESULT)
571
-
198
+ VCR.use_cassette('google3_country_code_biased_result') do
572
199
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Syracuse&region=it"
573
- Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
200
+ TestHelper.expects(:last_url).with(url)
574
201
  biased_result = Geokit::Geocoders::GoogleGeocoder3.geocode('Syracuse', :bias => 'it')
575
202
 
576
203
  assert_equal 'IT', biased_result.country_code
577
- assert_equal 'Sicily', biased_result.state
204
+ assert_equal 'Sicilia', biased_result.state
205
+ end
206
+ end
207
+
208
+ def test_language_response
209
+ VCR.use_cassette('google3_language_response_fr') do
210
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Hanoi&language=FR"
211
+ TestHelper.expects(:last_url).with(url)
212
+ language_result = Geokit::Geocoders::GoogleGeocoder3.geocode('Hanoi', :language => 'FR')
213
+
214
+ assert_equal 'VN', language_result.country_code
215
+ assert_equal 'Hanoï', language_result.city
216
+ end
578
217
  end
579
218
 
580
219
  def test_too_many_queries
581
220
  response = MockSuccess.new
582
- response.expects(:body).returns(GOOGLE3_TOO_MANY)
221
+ response.expects(:body).returns %q/{"status": "OVER_QUERY_LIMIT"}/
583
222
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
584
223
  Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
585
224
  assert_raise Geokit::TooManyQueriesError do
@@ -589,7 +228,7 @@ class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
589
228
 
590
229
  def test_invalid_request
591
230
  response = MockSuccess.new
592
- response.expects(:body).returns(GOOGLE3_INVALID_REQUEST)
231
+ response.expects(:body).returns %q/{"results" : [], "status" : "INVALID_REQUEST"}/
593
232
  url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape("3961 V\u00EDa Marisol")}"
594
233
  Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
595
234
  assert_raise Geokit::Geocoders::GeocodeError do