geokit 1.10.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.hound.yml +2 -0
  4. data/.rubocop.yml +358 -0
  5. data/.travis.yml +4 -4
  6. data/CHANGELOG.md +16 -0
  7. data/Gemfile +3 -3
  8. data/README.markdown +19 -4
  9. data/Rakefile +11 -16
  10. data/fixtures/keys.yml +9 -0
  11. data/fixtures/vcr_cassettes/google_postal_town.yml +117 -0
  12. data/fixtures/vcr_cassettes/mapbox_forward_geocode.yml +30 -30
  13. data/fixtures/vcr_cassettes/mapbox_forward_geocode_city_only.yml +25 -25
  14. data/fixtures/vcr_cassettes/mapbox_forward_geocode_state_only.yml +71 -0
  15. data/fixtures/vcr_cassettes/mapbox_reverse_geocode.yml +25 -25
  16. data/fixtures/vcr_cassettes/test_component_filtering_off.yml +390 -0
  17. data/fixtures/vcr_cassettes/test_component_filtering_on.yml +164 -0
  18. data/fixtures/vcr_cassettes/test_component_filtering_on_without_filter.yml +404 -0
  19. data/geokit.gemspec +24 -23
  20. data/lib/geokit.rb +7 -7
  21. data/lib/geokit/core_ext.rb +1 -1
  22. data/lib/geokit/geo_loc.rb +25 -19
  23. data/lib/geokit/geocoders.rb +21 -30
  24. data/lib/geokit/geocoders/bing.rb +5 -4
  25. data/lib/geokit/geocoders/ca_geocoder.rb +10 -11
  26. data/lib/geokit/geocoders/fcc.rb +9 -9
  27. data/lib/geokit/geocoders/free_geo_ip.rb +8 -8
  28. data/lib/geokit/geocoders/geo_plugin.rb +7 -7
  29. data/lib/geokit/geocoders/geobytes.rb +10 -10
  30. data/lib/geokit/geocoders/geocodio.rb +14 -14
  31. data/lib/geokit/geocoders/geonames.rb +12 -12
  32. data/lib/geokit/geocoders/google.rb +89 -61
  33. data/lib/geokit/geocoders/ip.rb +9 -14
  34. data/lib/geokit/geocoders/mapbox.rb +30 -30
  35. data/lib/geokit/geocoders/mapquest.rb +12 -12
  36. data/lib/geokit/geocoders/maxmind.rb +1 -1
  37. data/lib/geokit/geocoders/opencage.rb +19 -19
  38. data/lib/geokit/geocoders/openstreetmap.rb +21 -19
  39. data/lib/geokit/geocoders/ripe.rb +7 -7
  40. data/lib/geokit/geocoders/us_geocoder.rb +5 -5
  41. data/lib/geokit/geocoders/yahoo.rb +46 -46
  42. data/lib/geokit/geocoders/yandex.rb +18 -17
  43. data/lib/geokit/inflectors.rb +5 -5
  44. data/lib/geokit/lat_lng.rb +5 -4
  45. data/lib/geokit/multi_geocoder.rb +4 -2
  46. data/lib/geokit/net_adapter/net_http.rb +3 -2
  47. data/lib/geokit/net_adapter/typhoeus.rb +2 -1
  48. data/lib/geokit/version.rb +1 -1
  49. data/test/coverage_loader.rb +25 -0
  50. data/test/helper.rb +18 -87
  51. data/test/test_base_geocoder.rb +44 -11
  52. data/test/test_bing_geocoder.rb +40 -48
  53. data/test/test_bounds.rb +1 -1
  54. data/test/test_ca_geocoder.rb +15 -15
  55. data/test/test_fcc_geocoder.rb +8 -9
  56. data/test/test_free_geo_ip_geocoder.rb +8 -10
  57. data/test/test_geo_plugin_geocoder.rb +21 -22
  58. data/test/test_geobytes_geocoder.rb +9 -11
  59. data/test/test_geocodio_geocoder.rb +12 -14
  60. data/test/test_geoloc.rb +48 -49
  61. data/test/test_geonames_geocoder.rb +19 -23
  62. data/test/test_google_geocoder.rb +197 -189
  63. data/test/test_inflector.rb +7 -7
  64. data/test/test_ipgeocoder.rb +32 -31
  65. data/test/test_latlng.rb +28 -28
  66. data/test/test_map_quest.rb +23 -27
  67. data/test/test_mapbox_geocoder.rb +38 -28
  68. data/test/test_mappable.rb +2 -2
  69. data/test/test_maxmind_geocoder.rb +16 -16
  70. data/test/test_multi_geocoder.rb +5 -5
  71. data/test/test_multi_ip_geocoder.rb +3 -3
  72. data/test/test_net_adapter.rb +4 -4
  73. data/test/test_opencage_geocoder.rb +58 -67
  74. data/test/test_openstreetmap_geocoder.rb +67 -65
  75. data/test/test_polygon.rb +4 -22
  76. data/test/test_ripe_geocoder.rb +21 -26
  77. data/test/test_us_geocoder.rb +21 -21
  78. data/test/test_useragent.rb +46 -0
  79. data/test/test_yahoo_geocoder.rb +35 -47
  80. data/test/test_yandex_geocoder.rb +29 -27
  81. data/test/vcr_loader.rb +18 -0
  82. metadata +20 -6
@@ -1,183 +1,183 @@
1
1
  # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "helper")
2
+ require File.join(File.dirname(__FILE__), 'helper')
3
3
 
4
4
  class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
5
5
  def setup
6
6
  super
7
- @full_address = "100 Spear St Apt. 5, San Francisco, CA, 94105-1522, US"
8
- @full_address_short_zip = "100 Spear St Apt. 5, San Francisco, CA, 94105, US"
9
- @google_full_hash = {street_address: "100 Spear St Apt. 5", city: "San Francisco", state: "CA", zip: "94105", country_code: "US"}
10
- @google_city_hash = {city: "San Francisco", state: "CA"}
7
+ @full_address = '100 Spear St Apt. 5, San Francisco, CA, 94105-1522, US'
8
+ @full_address_short_zip = '100 Spear St Apt. 5, San Francisco, CA, 94105, US'
9
+ @google_full_hash = {street_address: '100 Spear St Apt. 5', city: 'San Francisco', state: 'CA', zip: '94105', country_code: 'US'}
10
+ @google_city_hash = {city: 'San Francisco', state: 'CA'}
11
11
 
12
12
  @google_full_loc = Geokit::GeoLoc.new(@google_full_hash)
13
13
  @google_city_loc = Geokit::GeoLoc.new(@google_city_hash)
14
+
15
+ @key_url = 'https://maps.googleapis.com/maps/api/geocode/json'
16
+ @base_url = 'https://maps.google.com/maps/api/geocode/json'
14
17
  end
15
18
 
16
19
  # Example from:
17
20
  # https://developers.google.com/maps/documentation/business/webservices#signature_examples
18
21
  def test_google_signature
19
- cryptographic_key = "vNIXE0xscrmjlyV-12Nj_BvUPaw="
20
- query_string = "/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"
21
- signature = Geokit::Geocoders::GoogleGeocoder.send(:sign_gmap_bus_api_url, query_string, cryptographic_key)
22
- assert_equal "KrU1TzVQM7Ur0i8i7K3huiw3MsA=", signature
22
+ cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
23
+ query_string = '/maps/api/geocode/json?address=New+York&sensor=false&client=clientID'
24
+ signature = geocoder_class.send(:sign_gmap_bus_api_url, query_string, cryptographic_key)
25
+ assert_equal 'KrU1TzVQM7Ur0i8i7K3huiw3MsA=', signature
23
26
  end
24
27
 
25
28
  # Example from:
26
29
  # https://developers.google.com/maps/documentation/business/webservices#signature_examples
27
30
  def test_google_signature_and_url
28
- Geokit::Geocoders::GoogleGeocoder.client_id = "clientID"
29
- Geokit::Geocoders::GoogleGeocoder.cryptographic_key = "vNIXE0xscrmjlyV-12Nj_BvUPaw="
30
- url = Geokit::Geocoders::GoogleGeocoder.send(:submit_url, "address=New+York")
31
- Geokit::Geocoders::GoogleGeocoder.client_id = nil
32
- Geokit::Geocoders::GoogleGeocoder.cryptographic_key = nil
33
- assert_equal "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=New+York&client=clientID&signature=9mevp7SoVsSKzF9nj-vApMYbatg=", url
31
+ geocoder_class.client_id = 'clientID'
32
+ geocoder_class.cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
33
+ url = geocoder_class.send(:submit_url, 'address=New+York')
34
+ geocoder_class.client_id = nil
35
+ geocoder_class.cryptographic_key = nil
36
+ assert_equal "#{@key_url}?sensor=false&address=New+York&client=clientID&signature=9mevp7SoVsSKzF9nj-vApMYbatg=", url
34
37
  end
35
38
 
36
39
  def test_google_api_key
37
- Geokit::Geocoders::GoogleGeocoder.api_key = "someKey"
38
- url = Geokit::Geocoders::GoogleGeocoder.send(:submit_url, "address=New+York")
39
- Geokit::Geocoders::GoogleGeocoder.api_key = nil
40
- assert_equal "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=New+York&key=someKey", url
40
+ geocoder_class.api_key = 'someKey'
41
+ url = geocoder_class.send(:submit_url, 'address=New+York')
42
+ geocoder_class.api_key = nil
43
+ assert_equal "#{@key_url}?sensor=false&address=New+York&key=someKey", url
41
44
  end
42
45
 
43
46
  def test_google_insecure_url
44
47
  Geokit::Geocoders.secure = false
45
- url = Geokit::Geocoders::GoogleGeocoder.send(:submit_url, "address=New+York")
48
+ url = geocoder_class.send(:submit_url, 'address=New+York')
46
49
  Geokit::Geocoders.secure = true
47
- assert_equal "http://maps.google.com/maps/api/geocode/json?sensor=false&address=New+York", url
50
+ assert_equal 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=New+York', url
48
51
  end
49
52
 
50
53
  def test_google_full_address
51
- VCR.use_cassette("google_full_short") do
52
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
54
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
53
55
  TestHelper.expects(:last_url).with(url)
54
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@address)
55
- assert_equal "CA", res.state
56
- assert_equal "San Francisco", res.city
56
+ res = geocode(@address, :google_full_short)
57
+ assert_equal 'CA', res.state
58
+ assert_equal 'San Francisco', res.city
57
59
  assert_array_in_delta [37.7749295, -122.4194155], res.to_a # slightly dif from yahoo
58
60
  assert res.is_us?
59
- assert_equal "San Francisco, CA, USA", res.full_address # slightly different from yahoo
60
- assert_equal "google", res.provider
61
- end
61
+ assert_equal 'San Francisco, CA, USA', res.full_address # slightly different from yahoo
62
+ assert_equal 'google', res.provider
62
63
  end
63
64
 
64
65
  def test_google_full_address_with_geo_loc
65
- VCR.use_cassette("google_full") do
66
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
66
+ url = "#{@base_url}?sensor=false&address=#{escape(@full_address_short_zip)}"
67
67
  TestHelper.expects(:last_url).with(url)
68
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
69
- assert_equal "CA", res.state
70
- assert_equal "San Francisco", res.city
68
+ res = geocode(@google_full_loc, :google_full)
69
+ assert_equal 'CA', res.state
70
+ assert_equal 'San Francisco', res.city
71
71
  assert_array_in_delta [37.7921509, -122.394], res.to_a # slightly dif from yahoo
72
72
  assert res.is_us?
73
- assert_equal "100 Spear Street #5, San Francisco, CA 94105, USA", res.full_address # slightly different from yahoo
74
- assert_equal "google", res.provider
75
- end
73
+ assert_equal '100 Spear Street #5, San Francisco, CA 94105, USA', res.full_address # slightly different from yahoo
74
+ assert_equal 'google', res.provider
76
75
  end
77
76
 
78
77
  def test_google_full_address_accuracy
79
- VCR.use_cassette("google_full") do
80
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
78
+ url = "#{@base_url}?sensor=false&address=#{escape(@full_address_short_zip)}"
81
79
  TestHelper.expects(:last_url).with(url)
82
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
80
+ res = geocode(@google_full_loc, :google_full)
83
81
 
84
82
  assert_equal 9, res.accuracy
85
- end
83
+ end
84
+
85
+ def test_google_postal_town
86
+ address = 'London SE3 0JB, UK'
87
+ url = "#{@base_url}?sensor=false&address=#{escape(address)}"
88
+ TestHelper.expects(:last_url).with(url)
89
+ res = geocode(address, :google_postal_town)
90
+ assert_equal 'Kidbrooke Way', res.street_address
91
+ assert_equal 'England', res.state
92
+ assert_equal 'London', res.city
93
+ assert_equal 'Greater London', res.district
94
+ assert_equal 'SE3 0JB', res.zip
95
+ assert_equal '51.465923,0.0290915', res.ll
96
+ assert !res.is_us?
97
+ assert_equal 'Kidbrooke Way, London SE3 0JB, UK', res.full_address
98
+ assert_equal 'zip+4', res.precision
99
+ assert_equal 'google', res.provider
86
100
  end
87
101
 
88
102
  def test_google_city
89
- VCR.use_cassette("google_city") do
90
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
103
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
91
104
  TestHelper.expects(:last_url).with(url)
92
- res = Geokit::Geocoders::GoogleGeocoder.do_geocode(@address)
105
+ res = geocode(@address, :google_city)
93
106
  assert_nil res.street_address
94
- assert_equal "CA", res.state
95
- assert_equal "San Francisco", res.city
96
- assert_equal "37.7749295,-122.4194155", res.ll
107
+ assert_equal 'CA', res.state
108
+ assert_equal 'San Francisco', res.city
109
+ assert_equal '37.7749295,-122.4194155', res.ll
97
110
  assert res.is_us?
98
- assert_equal "San Francisco, CA, USA", res.full_address
99
- assert_equal "city", res.precision
100
- assert_equal "google", res.provider
101
- end
111
+ assert_equal 'San Francisco, CA, USA', res.full_address
112
+ assert_equal 'city', res.precision
113
+ assert_equal 'google', res.provider
102
114
  end
103
115
 
104
116
  def test_google_sublocality
105
- @address = "682 prospect place Brooklyn ny 11216"
106
- VCR.use_cassette("google_sublocality") do
107
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
117
+ @address = '682 prospect place Brooklyn ny 11216'
118
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
108
119
  TestHelper.expects(:last_url).with(url)
109
- res = Geokit::Geocoders::GoogleGeocoder.do_geocode(@address)
110
- assert_equal "682 Prospect Place", res.street_address
111
- assert_equal "NY", res.state
112
- assert_equal "Brooklyn", res.city
113
- assert_equal "40.6745812,-73.9541582", res.ll
120
+ res = geocode(@address, :google_sublocality)
121
+ assert_equal '682 Prospect Place', res.street_address
122
+ assert_equal 'NY', res.state
123
+ assert_equal 'Brooklyn', res.city
124
+ assert_equal '40.6745812,-73.9541582', res.ll
114
125
  assert res.is_us?
115
- assert_equal "682 Prospect Place, Brooklyn, NY 11216, USA", res.full_address
116
- assert_equal "address", res.precision
117
- assert_equal "google", res.provider
118
- end
126
+ assert_equal '682 Prospect Place, Brooklyn, NY 11216, USA', res.full_address
127
+ assert_equal 'address', res.precision
128
+ assert_equal 'google', res.provider
119
129
  end
120
130
 
121
131
  def test_google_administrative_area_level_3
122
- @address = "8 Barkwood Lane, Clifton Park, NY 12065"
123
- VCR.use_cassette("google_administrative_area_level_3") do
124
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
125
- TestHelper.expects(:last_url).with(url)
126
- res = Geokit::Geocoders::GoogleGeocoder.do_geocode(@address)
127
- assert_equal "8 Barkwood Lane", res.street_address
128
- assert_equal "NY", res.state
129
- assert_equal "Clifton Park", res.city
130
- assert_equal "42.829583,-73.788174", res.ll
131
- assert res.is_us?
132
- assert_equal "8 Barkwood Lane, Clifton Park, NY 12065, USA", res.full_address
133
- assert_equal "building", res.precision
134
- assert_equal "google", res.provider
135
- end
132
+ @address = '8 Barkwood Lane, Clifton Park, NY 12065'
133
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
134
+ TestHelper.expects(:last_url).with(url)
135
+ res = geocode(@address, :google_administrative_area_level_3)
136
+ assert_equal '8 Barkwood Lane', res.street_address
137
+ assert_equal 'NY', res.state
138
+ assert_equal 'Clifton Park', res.city
139
+ assert_equal '42.829583,-73.788174', res.ll
140
+ assert res.is_us?
141
+ assert_equal '8 Barkwood Lane, Clifton Park, NY 12065, USA', res.full_address
142
+ assert_equal 'building', res.precision
143
+ assert_equal 'google', res.provider
136
144
  end
137
145
 
138
146
  def test_google_city_improved_ordering
139
- VCR.use_cassette("google_city_ordering") do
140
- res = Geokit::Geocoders::GoogleGeocoder.geocode("62510, fr", bias: "fr")
141
- assert_equal "zip+4", res.precision
142
- assert_equal "62510 Arques, France", res.full_address
143
- end
147
+ res = geocode('62510, fr', :google_city_ordering, bias: 'fr')
148
+ assert_equal 'zip+4', res.precision
149
+ assert_equal '62510 Arques, France', res.full_address
144
150
  end
145
151
 
146
152
  def test_google_city_accuracy
147
- VCR.use_cassette("google_city") do
148
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
153
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
149
154
  TestHelper.expects(:last_url).with(url)
150
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@address)
151
- assert_equal "city", res.precision
155
+ res = geocode(@address, :google_city)
156
+ assert_equal 'city', res.precision
152
157
  assert_equal 4, res.accuracy
153
- end
154
158
  end
155
159
 
156
160
  def test_google_city_with_geo_loc
157
- VCR.use_cassette("google_city") do
158
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
161
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
159
162
  TestHelper.expects(:last_url).with(url)
160
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc)
161
- assert_equal "CA", res.state
162
- assert_equal "San Francisco", res.city
163
- assert_equal "37.7749295,-122.4194155", res.ll
163
+ res = geocode(@google_city_loc, :google_city)
164
+ assert_equal 'CA', res.state
165
+ assert_equal 'San Francisco', res.city
166
+ assert_equal '37.7749295,-122.4194155', res.ll
164
167
  assert res.is_us?
165
- assert_equal "San Francisco, CA, USA", res.full_address
168
+ assert_equal 'San Francisco, CA, USA', res.full_address
166
169
  assert_nil res.street_address
167
- assert_equal "city", res.precision
168
- assert_equal "google", res.provider
169
- end
170
+ assert_equal 'city', res.precision
171
+ assert_equal 'google', res.provider
170
172
  end
171
173
 
172
174
  def test_google_suggested_bounds
173
- VCR.use_cassette("google_full") do
174
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
175
+ url = "#{@base_url}?sensor=false&address=#{escape(@full_address_short_zip)}"
175
176
  TestHelper.expects(:last_url).with(url)
176
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
177
+ res = geocode(@google_full_loc, :google_full)
177
178
  assert_instance_of Geokit::Bounds, res.suggested_bounds
178
179
  assert_array_in_delta [37.7908019197085, -122.3953489802915], res.suggested_bounds.sw.to_a
179
180
  assert_array_in_delta [37.7934998802915, -122.3926510197085], res.suggested_bounds.ne.to_a
180
- end
181
181
  end
182
182
 
183
183
  def test_google_suggested_bounds_url
@@ -185,167 +185,175 @@ class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
185
185
  Geokit::LatLng.new(33.7036917, -118.6681759),
186
186
  Geokit::LatLng.new(34.3373061, -118.1552891),
187
187
  )
188
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=Winnetka&bounds=33.7036917%2C-118.6681759%7C34.3373061%2C-118.1552891"
189
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url)
190
- Geokit::Geocoders::GoogleGeocoder.geocode("Winnetka", bias: bounds)
188
+ url = "#{@base_url}?sensor=false&address=Winnetka&bounds=33.7036917%2C-118.6681759%7C34.3373061%2C-118.1552891"
189
+ geocoder_class.expects(:call_geocoder_service).with(url)
190
+ geocode('Winnetka', bias: bounds)
191
191
  end
192
192
 
193
193
  def test_google_place_id
194
- VCR.use_cassette("google_full_v3_20") do
195
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
196
- TestHelper.expects(:last_url).with(url)
197
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@full_address_short_zip)
198
- assert_equal 'EjExMDAgU3BlYXIgU3RyZWV0ICM1LCBTYW4gRnJhbmNpc2NvLCBDQSA5NDEwNSwgVVNB', res.place_id
199
- end
194
+ url = "#{@base_url}?sensor=false&address=#{escape(@full_address_short_zip)}"
195
+ TestHelper.expects(:last_url).with(url)
196
+ res = geocode(@full_address_short_zip, :google_full_v3_20)
197
+ assert_equal 'EjExMDAgU3BlYXIgU3RyZWV0ICM1LCBTYW4gRnJhbmNpc2NvLCBDQSA5NDEwNSwgVVNB', res.place_id
200
198
  end
201
199
 
202
200
  def test_google_formatted_address
203
- VCR.use_cassette("google_full_v3_20") do
204
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
205
- TestHelper.expects(:last_url).with(url)
206
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@full_address_short_zip)
207
- assert_equal '100 Spear Street #5, San Francisco, CA 94105, USA', res.formatted_address
208
- end
201
+ url = "#{@base_url}?sensor=false&address=#{escape(@full_address_short_zip)}"
202
+ TestHelper.expects(:last_url).with(url)
203
+ res = geocode(@full_address_short_zip, :google_full_v3_20)
204
+ assert_equal '100 Spear Street #5, San Francisco, CA 94105, USA', res.formatted_address
209
205
  end
210
206
 
211
207
  def test_service_unavailable
212
208
  response = MockFailure.new
213
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
214
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
215
- assert !Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc).success
209
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
210
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
211
+ assert !geocode(@google_city_loc).success
216
212
  end
217
213
 
218
214
  def test_multiple_results
219
- VCR.use_cassette("google_multi") do
220
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape('via Sandro Pertini 8, Ossona, MI')}"
215
+ url = "#{@base_url}?sensor=false&address=#{escape('via Sandro Pertini 8, Ossona, MI')}"
221
216
  TestHelper.expects(:last_url).with(url)
222
- res = Geokit::Geocoders::GoogleGeocoder.geocode("via Sandro Pertini 8, Ossona, MI")
217
+ res = geocode('via Sandro Pertini 8, Ossona, MI', :google_multi)
223
218
  assert_equal 5, res.all.size
224
219
  res = res.all[0]
225
- assert_equal "Lombardy", res.state
226
- assert_equal "Mesero", res.city
220
+ assert_equal 'Lombardy', res.state
221
+ assert_equal 'Mesero', res.city
227
222
  assert_array_in_delta [45.4966218, 8.852694], res.to_a
228
223
  assert !res.is_us?
229
- assert_equal "Via Sandro Pertini, 8, 20010 Mesero Milan, Italy", res.full_address
230
- assert_equal "8 Via Sandro Pertini", res.street_address
231
- assert_equal "google", res.provider
224
+ assert_equal 'Via Sandro Pertini, 8, 20010 Mesero Milan, Italy', res.full_address
225
+ assert_equal '8 Via Sandro Pertini', res.street_address
226
+ assert_equal 'google', res.provider
232
227
 
233
228
  res = res.all[4]
234
- assert_equal "Lombardy", res.state
235
- assert_equal "Ossona", res.city
229
+ assert_equal 'Lombardy', res.state
230
+ assert_equal 'Ossona', res.city
236
231
  assert_array_in_delta [45.5074444, 8.90232], res.to_a
237
232
  assert !res.is_us?
238
- assert_equal "Via S. Pertini, 20010 Ossona Milan, Italy", res.full_address
239
- assert_equal "Via S. Pertini", res.street_address
240
- assert_equal "google", res.provider
241
- end
233
+ assert_equal 'Via S. Pertini, 20010 Ossona Milan, Italy', res.full_address
234
+ assert_equal 'Via S. Pertini', res.street_address
235
+ assert_equal 'google', res.provider
242
236
  end
243
237
 
244
238
  def test_reverse_geocode
245
- VCR.use_cassette("google_reverse_madrid") do
246
- madrid = Geokit::GeoLoc.new
247
- madrid.lat, madrid.lng = "40.4167413", "-3.7032498"
248
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&latlng=#{Geokit::Inflector.url_escape(madrid.ll)}"
239
+ madrid = Geokit::GeoLoc.new
240
+ madrid.lat, madrid.lng = '40.4167413', '-3.7032498'
241
+ url = "#{@base_url}?sensor=false&latlng=#{escape(madrid.ll)}"
249
242
  TestHelper.expects(:last_url).with(url)
250
- res = Geokit::Geocoders::GoogleGeocoder.do_reverse_geocode(madrid.ll)
243
+ res = reverse_geocode(madrid.ll, :google_reverse_madrid)
251
244
 
252
245
  assert_equal madrid.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5)
253
246
  assert_equal madrid.lng.to_s.slice(1..5), res.lng.to_s.slice(1..5)
254
- assert_equal "ES", res.country_code
255
- assert_equal "google", res.provider
247
+ assert_equal 'ES', res.country_code
248
+ assert_equal 'google', res.provider
256
249
 
257
- assert_equal "Madrid", res.city
258
- assert_equal "Community of Madrid", res.state
250
+ assert_equal 'Madrid', res.city
251
+ assert_equal 'Community of Madrid', res.state
259
252
 
260
- assert_equal "Spain", res.country
261
- assert_equal "28013", res.zip
253
+ assert_equal 'Spain', res.country
254
+ assert_equal '28013', res.zip
262
255
  assert_equal true, res.success
263
- end
264
256
  end
265
257
 
266
258
  def test_reverse_geocode_language
267
- VCR.use_cassette("google_reverse_madrid_es") do
268
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&latlng=40.416%2C-3.703&language=es"
259
+ url = "#{@base_url}?sensor=false&latlng=40.416%2C-3.703&language=es"
269
260
  TestHelper.expects(:last_url).with(url)
270
- language_result = Geokit::Geocoders::GoogleGeocoder.reverse_geocode("40.416,-3.703", language: "es")
261
+ language_result = reverse_geocode('40.416,-3.703', :google_reverse_madrid_es, language: 'es')
271
262
 
272
- assert_equal "ES", language_result.country_code
273
- assert_equal "Madrid", language_result.city
274
- end
263
+ assert_equal 'ES', language_result.country_code
264
+ assert_equal 'Madrid', language_result.city
275
265
  end
276
266
 
277
267
  def test_language_response
278
- VCR.use_cassette("google_language_response_fr") do
279
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=Hanoi&language=FR"
268
+ url = "#{@base_url}?sensor=false&address=Hanoi&language=FR"
280
269
  TestHelper.expects(:last_url).with(url)
281
- language_result = Geokit::Geocoders::GoogleGeocoder.geocode("Hanoi", language: "FR")
270
+ language_result = geocode('Hanoi', :google_language_response_fr, language: 'FR')
282
271
 
283
- assert_equal "VN", language_result.country_code
272
+ assert_equal 'VN', language_result.country_code
284
273
  assert_equal "Hanoï", language_result.city
285
- end
286
274
  end
287
275
 
288
276
  def test_too_many_queries
289
277
  response = MockSuccess.new
290
278
  response.expects(:body).returns '{"status": "OVER_QUERY_LIMIT", "error_message": "quota exceeded!"}'
291
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
292
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
279
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
280
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
293
281
  err = assert_raise Geokit::Geocoders::TooManyQueriesError do
294
- Geokit::Geocoders::GoogleGeocoder.geocode(@address)
282
+ geocode(@address)
295
283
  end
296
- assert_equal "quota exceeded!", err.message
284
+ assert_equal 'quota exceeded!', err.message
297
285
  end
298
286
 
299
287
  def test_access_denied
300
288
  response = MockSuccess.new
301
289
  response.expects(:body).returns '{"status": "REQUEST_DENIED", "error_message": "access denied!"}'
302
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
303
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
290
+ url = "#{@base_url}?sensor=false&address=#{escape(@address)}"
291
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
304
292
  err = assert_raise Geokit::Geocoders::AccessDeniedError do
305
- Geokit::Geocoders::GoogleGeocoder.geocode(@address)
293
+ geocode(@address)
306
294
  end
307
- assert_equal "access denied!", err.message
295
+ assert_equal 'access denied!', err.message
308
296
  end
309
297
 
310
298
  def test_invalid_request
311
299
  response = MockSuccess.new
312
300
  response.expects(:body).returns '{"results" : [], "status" : "INVALID_REQUEST", "error_message": "error!" }'
313
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape("3961 V\u00EDa Marisol")}"
314
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
301
+ url = "#{@base_url}?sensor=false&address=#{escape("3961 V\u00EDa Marisol")}"
302
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
315
303
  err = assert_raise Geokit::Geocoders::GeocodeError do
316
- Geokit::Geocoders::GoogleGeocoder.geocode("3961 V\u00EDa Marisol")
304
+ geocode("3961 V\u00EDa Marisol")
317
305
  end
318
- assert_equal "error!", err.message
306
+ assert_equal 'error!', err.message
319
307
  end
320
308
 
321
309
  def test_country_code_biasing_toledo
322
- VCR.use_cassette("google_country_code_biased_result_toledo") do
323
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=toledo&region=es"
324
- TestHelper.expects(:last_url).with(url)
325
- biased_result = Geokit::Geocoders::GoogleGeocoder.geocode("toledo", bias: "es")
310
+ url = "#{@base_url}?sensor=false&address=toledo&region=es"
311
+ TestHelper.expects(:last_url).with(url)
312
+ biased_result = geocode('toledo', :google_country_code_biased_result_toledo, bias: 'es')
326
313
 
327
- assert_equal "ES", biased_result.country_code
328
- assert_equal "CM", biased_result.state
329
- end
314
+ assert_equal 'ES', biased_result.country_code
315
+ assert_equal 'CM', biased_result.state
330
316
 
331
- VCR.use_cassette("google_result_toledo_default_bias") do
332
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=toledo"
333
- TestHelper.expects(:last_url).with(url)
334
- biased_result = Geokit::Geocoders::GoogleGeocoder.geocode("toledo")
317
+ url = "#{@base_url}?sensor=false&address=toledo"
318
+ TestHelper.expects(:last_url).with(url)
319
+ biased_result = geocode('toledo', :google_result_toledo_default_bias)
335
320
 
336
- assert_equal "US", biased_result.country_code
337
- assert_equal "OH", biased_result.state
338
- end
321
+ assert_equal 'US', biased_result.country_code
322
+ assert_equal 'OH', biased_result.state
339
323
  end
340
324
 
341
325
  def test_country_code_biasing_orly
342
- VCR.use_cassette("google_country_code_biased_result_orly") do
343
- url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=orly&region=fr"
344
- TestHelper.expects(:last_url).with(url)
345
- biased_result = Geokit::Geocoders::GoogleGeocoder.geocode("orly", bias: "fr")
326
+ url = "#{@base_url}?sensor=false&address=orly&region=fr"
327
+ TestHelper.expects(:last_url).with(url)
328
+ biased_result = geocode('orly', :google_country_code_biased_result_orly, bias: 'fr')
346
329
 
347
- assert_equal "FR", biased_result.country_code
348
- assert_equal "Orly, France", biased_result.full_address
349
- end
330
+ assert_equal 'FR', biased_result.country_code
331
+ assert_equal 'Orly, France', biased_result.full_address
332
+ end
333
+
334
+
335
+ def test_component_filtering
336
+ url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&address=austin'
337
+ TestHelper.expects(:last_url).with(url)
338
+ filtered_result = geocode('austin', :test_component_filtering_off)
339
+
340
+ assert_equal 'TX', filtered_result.state
341
+ assert_equal 'Austin, TX, USA', filtered_result.full_address
342
+
343
+ url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&address=austin&components=administrative_area:il%7Ccountry:us'
344
+ TestHelper.expects(:last_url).with(url)
345
+ filtered_result = geocode('austin',
346
+ :test_component_filtering_on,
347
+ components: { administrative_area: 'IL', country: 'US' })
348
+
349
+ assert_equal 'IL', filtered_result.state
350
+ assert_equal 'Austin, Chicago, IL, USA', filtered_result.full_address
351
+
352
+ url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&address=austin'
353
+ TestHelper.expects(:last_url).with(url)
354
+ filtered_result = geocode('austin', :test_component_filtering_on_without_filter, components: nil)
355
+
356
+ assert_equal 'TX', filtered_result.state
357
+ assert_equal 'Austin, TX, USA', filtered_result.full_address
350
358
  end
351
359
  end