geocoder 1.1.9 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of geocoder might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +9 -5
- data/CHANGELOG.md +19 -0
- data/README.md +175 -10
- data/Rakefile +1 -1
- data/gemfiles/Gemfile.mongoid-2.4.x +1 -0
- data/lib/generators/geocoder/maxmind/geolite_city_generator.rb +28 -0
- data/lib/generators/geocoder/maxmind/geolite_country_generator.rb +28 -0
- data/lib/generators/geocoder/maxmind/templates/migration/geolite_city.rb +27 -0
- data/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +17 -0
- data/lib/geocoder.rb +4 -12
- data/lib/geocoder/cache.rb +3 -2
- data/lib/geocoder/calculations.rb +39 -0
- data/lib/geocoder/configuration.rb +1 -7
- data/lib/geocoder/ip_address.rb +12 -0
- data/lib/geocoder/lookup.rb +10 -1
- data/lib/geocoder/lookups/baidu.rb +7 -6
- data/lib/geocoder/lookups/baidu_ip.rb +54 -0
- data/lib/geocoder/lookups/base.rb +37 -9
- data/lib/geocoder/lookups/bing.rb +10 -5
- data/lib/geocoder/lookups/cloudmade.rb +35 -0
- data/lib/geocoder/lookups/freegeoip.rb +5 -1
- data/lib/geocoder/lookups/geocodio.rb +42 -0
- data/lib/geocoder/lookups/google_premier.rb +1 -1
- data/lib/geocoder/lookups/here.rb +62 -0
- data/lib/geocoder/lookups/mapquest.rb +2 -1
- data/lib/geocoder/lookups/maxmind_local.rb +58 -0
- data/lib/geocoder/lookups/nominatim.rb +8 -0
- data/lib/geocoder/lookups/smarty_streets.rb +45 -0
- data/lib/geocoder/lookups/yahoo.rb +1 -1
- data/lib/geocoder/models/active_record.rb +5 -3
- data/lib/geocoder/models/base.rb +1 -4
- data/lib/geocoder/models/mongo_base.rb +4 -2
- data/lib/geocoder/query.rb +4 -4
- data/lib/geocoder/railtie.rb +1 -1
- data/lib/geocoder/request.rb +10 -8
- data/lib/geocoder/results/baidu_ip.rb +62 -0
- data/lib/geocoder/results/cloudmade.rb +39 -0
- data/lib/geocoder/results/geocodio.rb +66 -0
- data/lib/geocoder/results/here.rb +62 -0
- data/lib/geocoder/results/maxmind_local.rb +49 -0
- data/lib/geocoder/results/smarty_streets.rb +106 -0
- data/lib/geocoder/results/test.rb +20 -3
- data/lib/geocoder/results/yandex.rb +7 -3
- data/lib/geocoder/sql.rb +16 -15
- data/lib/geocoder/stores/active_record.rb +6 -2
- data/lib/geocoder/stores/base.rb +8 -1
- data/lib/geocoder/version.rb +1 -1
- data/lib/maxmind_database.rb +109 -0
- data/lib/oauth_util.rb +1 -1
- data/lib/tasks/geocoder.rake +3 -1
- data/lib/tasks/maxmind.rake +73 -0
- data/test/fixtures/baidu_ip_202_198_16_3 +19 -0
- data/test/fixtures/baidu_ip_invalid_key +1 -0
- data/test/fixtures/baidu_ip_no_results +1 -0
- data/test/fixtures/cloudmade_invalid_key +1 -0
- data/test/fixtures/cloudmade_madison_square_garden +1 -0
- data/test/fixtures/cloudmade_no_results +1 -0
- data/test/fixtures/geocodio_1101_pennsylvania_ave +1 -0
- data/test/fixtures/geocodio_bad_api_key +3 -0
- data/test/fixtures/geocodio_invalid +4 -0
- data/test/fixtures/geocodio_no_results +1 -0
- data/test/fixtures/geocodio_over_query_limit +4 -0
- data/test/fixtures/here_madison_square_garden +72 -0
- data/test/fixtures/here_no_results +8 -0
- data/test/fixtures/nominatim_over_limit +1 -0
- data/test/fixtures/smarty_streets_11211 +1 -0
- data/test/fixtures/smarty_streets_madison_square_garden +47 -0
- data/test/fixtures/smarty_streets_no_results +1 -0
- data/test/fixtures/yandex_canada_rue_dupuis_14 +446 -0
- data/test/fixtures/yandex_new_york +1 -0
- data/test/integration/http_client_test.rb +25 -0
- data/test/mongoid_test_helper.rb +2 -2
- data/test/test_helper.rb +98 -30
- data/test/{active_record_test.rb → unit/active_record_test.rb} +4 -3
- data/test/{cache_test.rb → unit/cache_test.rb} +3 -1
- data/test/{calculations_test.rb → unit/calculations_test.rb} +22 -13
- data/test/{configuration_test.rb → unit/configuration_test.rb} +4 -27
- data/test/{error_handling_test.rb → unit/error_handling_test.rb} +10 -9
- data/test/{geocoder_test.rb → unit/geocoder_test.rb} +26 -7
- data/test/{https_test.rb → unit/https_test.rb} +4 -3
- data/test/unit/ip_address_test.rb +24 -0
- data/test/{lookup_test.rb → unit/lookup_test.rb} +33 -20
- data/test/unit/lookups/bing_test.rb +68 -0
- data/test/unit/lookups/dstk_test.rb +26 -0
- data/test/unit/lookups/esri_test.rb +48 -0
- data/test/unit/lookups/freegeoip_test.rb +27 -0
- data/test/unit/lookups/geocoder_ca_test.rb +17 -0
- data/test/unit/lookups/geocodio_test.rb +55 -0
- data/test/unit/lookups/google_premier_test.rb +22 -0
- data/test/unit/lookups/google_test.rb +84 -0
- data/test/unit/lookups/mapquest_test.rb +60 -0
- data/test/unit/lookups/maxmind_local_test.rb +28 -0
- data/test/unit/lookups/maxmind_test.rb +63 -0
- data/test/unit/lookups/nominatim_test.rb +31 -0
- data/test/unit/lookups/smarty_streets_test.rb +71 -0
- data/test/unit/lookups/yahoo_test.rb +35 -0
- data/test/{method_aliases_test.rb → unit/method_aliases_test.rb} +5 -4
- data/test/unit/model_test.rb +38 -0
- data/test/{mongoid_test.rb → unit/mongoid_test.rb} +10 -9
- data/test/unit/near_test.rb +87 -0
- data/test/{oauth_util_test.rb → unit/oauth_util_test.rb} +3 -2
- data/test/{proxy_test.rb → unit/proxy_test.rb} +2 -1
- data/test/{query_test.rb → unit/query_test.rb} +7 -8
- data/test/unit/rake_task_test.rb +21 -0
- data/test/{request_test.rb → unit/request_test.rb} +8 -2
- data/test/{result_test.rb → unit/result_test.rb} +29 -1
- data/test/{test_mode_test.rb → unit/test_mode_test.rb} +12 -1
- metadata +80 -27
- data/test/custom_block_test.rb +0 -32
- data/test/integration/smoke_test.rb +0 -26
- data/test/near_test.rb +0 -61
- data/test/services_test.rb +0 -393
@@ -0,0 +1 @@
|
|
1
|
+
{"response":{"Attribution":"","GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"request":"New York, NY","found":"21","results":"10"}},"featureMember":[{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"province","text":"United States, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"New York","CountryNameCode":"US","CountryName":"United States","AdministrativeArea":{"AdministrativeAreaName":"New York"}}}}},"description":"United States","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"-79.762115 40.477414","upperCorner":"-71.668635 45.016078"}},"Point":{"pos":"-74.007112 40.714545"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"United States, New York, Bronx, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"New York, Bronx, New York","CountryNameCode":"US","CountryName":"United States","AdministrativeArea":{"AdministrativeAreaName":"New York","SubAdministrativeArea":{"SubAdministrativeAreaName":"Bronx","Locality":{"LocalityName":"New York"}}}}}}},"description":"Bronx, New York, United States","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"-74.258892 40.477414","upperCorner":"-73.700373 40.917616"}},"Point":{"pos":"-74.007112 40.714545"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"hydro","text":"United States, Upper New York Bay","precision":"other","AddressDetails":{"Country":{"AddressLine":"Upper New York Bay","CountryNameCode":"US","CountryName":"United States","Locality":{"Premise":{"PremiseName":"Upper New York Bay"}}}}}},"description":"United States","name":"Upper New York Bay","boundedBy":{"Envelope":{"lowerCorner":"-74.107633 40.604124","upperCorner":"-73.998434 40.706697"}},"Point":{"pos":"-74.048758 40.660893"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"United States, New Jersey, Hudson, West New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"New Jersey, Hudson, West New York","CountryNameCode":"US","CountryName":"United States","AdministrativeArea":{"AdministrativeAreaName":"New Jersey","SubAdministrativeArea":{"SubAdministrativeAreaName":"Hudson","Locality":{"LocalityName":"West New York"}}}}}}},"description":"Hudson, New Jersey, United States","name":"West New York","boundedBy":{"Envelope":{"lowerCorner":"-74.023569 40.7746","upperCorner":"-73.992936 40.796565"}},"Point":{"pos":"-74.015260 40.788325"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"United Kingdom of Great Britain and Northern Ireland, England, Lincolnshire, East Lindsey, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"England, Lincolnshire, East Lindsey, New York","CountryNameCode":"GB","CountryName":"United Kingdom of Great Britain and Northern Ireland","AdministrativeArea":{"AdministrativeAreaName":"England","SubAdministrativeArea":{"SubAdministrativeAreaName":"Lincolnshire","Locality":{"LocalityName":"New York"}}}}}}},"description":"East Lindsey, Lincolnshire, England, United Kingdom of Great Britain and Northern Ireland","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"-0.205993 53.039103","upperCorner":"-0.074192 53.11847"}},"Point":{"pos":"-0.140092 53.078805"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"South Africa, North West, Bophirima, Naledi, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"North West, Bophirima, Naledi, New York","CountryNameCode":"ZA","CountryName":"South Africa","AdministrativeArea":{"AdministrativeAreaName":"North West","SubAdministrativeArea":{"SubAdministrativeAreaName":"Bophirima","Locality":{"LocalityName":"New York"}}}}}}},"description":"Naledi, Bophirima, North West, South Africa","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"24.454037 -26.943742","upperCorner":"24.585838 -26.825556"}},"Point":{"pos":"24.519938 -26.884665"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"United States, New Mexico, Cibola, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"New Mexico, Cibola, New York","CountryNameCode":"US","CountryName":"United States","AdministrativeArea":{"AdministrativeAreaName":"New Mexico","SubAdministrativeArea":{"SubAdministrativeAreaName":"Cibola","Locality":{"LocalityName":"New York"}}}}}}},"description":"Cibola, New Mexico, United States","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"-107.592515 35.00522","upperCorner":"-107.460714 35.113594"}},"Point":{"pos":"-107.526615 35.059425"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"United States, Iowa, Wayne, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"Iowa, Wayne, New York","CountryNameCode":"US","CountryName":"United States","AdministrativeArea":{"AdministrativeAreaName":"Iowa","SubAdministrativeArea":{"SubAdministrativeAreaName":"Wayne","Locality":{"LocalityName":"New York"}}}}}}},"description":"Wayne, Iowa, United States","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"-93.32337 40.801487","upperCorner":"-93.191569 40.901567"}},"Point":{"pos":"-93.257469 40.851546"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"United States, Missouri, Caldwell, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"Missouri, Caldwell, New York","CountryNameCode":"US","CountryName":"United States","AdministrativeArea":{"AdministrativeAreaName":"Missouri","SubAdministrativeArea":{"SubAdministrativeAreaName":"Caldwell","Locality":{"LocalityName":"New York"}}}}}}},"description":"Caldwell, Missouri, United States","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"-93.97525 39.633979","upperCorner":"-93.843449 39.735813"}},"Point":{"pos":"-93.909350 39.684915"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"locality","text":"United States, Texas, Henderson, New York","precision":"other","AddressDetails":{"Country":{"AddressLine":"Texas, Henderson, New York","CountryNameCode":"US","CountryName":"United States","AdministrativeArea":{"AdministrativeAreaName":"Texas","SubAdministrativeArea":{"SubAdministrativeAreaName":"Henderson","Locality":{"LocalityName":"New York"}}}}}}},"description":"Henderson, Texas, United States","name":"New York","boundedBy":{"Envelope":{"lowerCorner":"-95.735086 32.112428","upperCorner":"-95.603285 32.224535"}},"Point":{"pos":"-95.669185 32.168499"}}}]}}}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..", "lib")
|
3
|
+
require 'pathname'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'geocoder'
|
7
|
+
require 'yaml'
|
8
|
+
|
9
|
+
class HttpClientTest < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@api_keys = YAML.load_file("api_keys.yml")
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_http_basic_auth
|
15
|
+
Geocoder.configure(lookup: :geocoder_us, api_key: @api_keys["geocoder_us"])
|
16
|
+
results = Geocoder.search "27701"
|
17
|
+
assert_not_nil results.first
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_ssl
|
21
|
+
Geocoder.configure(lookup: :esri, use_https: true)
|
22
|
+
results = Geocoder.search "27701"
|
23
|
+
assert_not_nil results.first
|
24
|
+
end
|
25
|
+
end
|
data/test/mongoid_test_helper.rb
CHANGED
@@ -18,7 +18,7 @@ end
|
|
18
18
|
##
|
19
19
|
# Geocoded model.
|
20
20
|
#
|
21
|
-
class
|
21
|
+
class PlaceUsingMongoid
|
22
22
|
include Mongoid::Document
|
23
23
|
include Geocoder::Model::Mongoid
|
24
24
|
|
@@ -34,7 +34,7 @@ class Place
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
class
|
37
|
+
class PlaceUsingMongoidWithoutIndex
|
38
38
|
include Mongoid::Document
|
39
39
|
include Geocoder::Model::Mongoid
|
40
40
|
|
data/test/test_helper.rb
CHANGED
@@ -105,6 +105,8 @@ module Geocoder
|
|
105
105
|
fixture_exists?(filename) ? filename : default_fixture_filename
|
106
106
|
end
|
107
107
|
|
108
|
+
remove_method(:make_api_request)
|
109
|
+
|
108
110
|
def make_api_request(query)
|
109
111
|
raise TimeoutError if query.text == "timeout"
|
110
112
|
raise SocketError if query.text == "socket_error"
|
@@ -147,22 +149,49 @@ module Geocoder
|
|
147
149
|
end
|
148
150
|
end
|
149
151
|
|
150
|
-
class
|
152
|
+
class MaxmindLocal
|
151
153
|
private
|
152
|
-
|
153
|
-
|
154
|
+
|
155
|
+
remove_method(:results)
|
156
|
+
|
157
|
+
def results query
|
158
|
+
return [] if query.to_s == "no results"
|
159
|
+
|
160
|
+
if query.to_s == '127.0.0.1'
|
161
|
+
[]
|
162
|
+
else
|
163
|
+
[{:request=>"8.8.8.8", :ip=>"8.8.8.8", :country_code2=>"US", :country_code3=>"USA", :country_name=>"United States", :continent_code=>"NA", :region_name=>"CA", :city_name=>"Mountain View", :postal_code=>"94043", :latitude=>37.41919999999999, :longitude=>-122.0574, :dma_code=>807, :area_code=>650, :timezone=>"America/Los_Angeles"}]
|
164
|
+
end
|
154
165
|
end
|
166
|
+
end
|
167
|
+
|
168
|
+
class Baidu
|
169
|
+
private
|
155
170
|
def default_fixture_filename
|
156
171
|
"baidu_shanghai_pearl_tower"
|
157
172
|
end
|
158
173
|
end
|
174
|
+
|
175
|
+
class BaiduIp
|
176
|
+
private
|
177
|
+
def default_fixture_filename
|
178
|
+
"baidu_ip_202_198_16_3"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
class Geocodio
|
183
|
+
private
|
184
|
+
def default_fixture_filename
|
185
|
+
"geocodio_1101_pennsylvania_ave"
|
186
|
+
end
|
187
|
+
end
|
159
188
|
end
|
160
189
|
end
|
161
190
|
|
162
191
|
##
|
163
192
|
# Geocoded model.
|
164
193
|
#
|
165
|
-
class
|
194
|
+
class Place < ActiveRecord::Base
|
166
195
|
geocoded_by :address
|
167
196
|
|
168
197
|
def initialize(name, address)
|
@@ -176,7 +205,7 @@ end
|
|
176
205
|
# Geocoded model.
|
177
206
|
# - Has user-defined primary key (not just 'id')
|
178
207
|
#
|
179
|
-
class
|
208
|
+
class PlaceWithCustomPrimaryKey < Place
|
180
209
|
|
181
210
|
class << self
|
182
211
|
def primary_key
|
@@ -186,10 +215,7 @@ class VenuePlus < Venue
|
|
186
215
|
|
187
216
|
end
|
188
217
|
|
189
|
-
|
190
|
-
# Reverse geocoded model.
|
191
|
-
#
|
192
|
-
class Landmark < ActiveRecord::Base
|
218
|
+
class PlaceReverseGeocoded < ActiveRecord::Base
|
193
219
|
reverse_geocoded_by :latitude, :longitude
|
194
220
|
|
195
221
|
def initialize(name, latitude, longitude)
|
@@ -200,10 +226,7 @@ class Landmark < ActiveRecord::Base
|
|
200
226
|
end
|
201
227
|
end
|
202
228
|
|
203
|
-
|
204
|
-
# Geocoded model with block.
|
205
|
-
#
|
206
|
-
class Event < ActiveRecord::Base
|
229
|
+
class PlaceWithCustomResultsHandling < ActiveRecord::Base
|
207
230
|
geocoded_by :address do |obj,results|
|
208
231
|
if result = results.first
|
209
232
|
obj.coords_string = "#{result.latitude},#{result.longitude}"
|
@@ -219,10 +242,7 @@ class Event < ActiveRecord::Base
|
|
219
242
|
end
|
220
243
|
end
|
221
244
|
|
222
|
-
|
223
|
-
# Reverse geocoded model with block.
|
224
|
-
#
|
225
|
-
class Party < ActiveRecord::Base
|
245
|
+
class PlaceReverseGeocodedWithCustomResultsHandling < ActiveRecord::Base
|
226
246
|
reverse_geocoded_by :latitude, :longitude do |obj,results|
|
227
247
|
if result = results.first
|
228
248
|
obj.country = result.country_code
|
@@ -237,11 +257,7 @@ class Party < ActiveRecord::Base
|
|
237
257
|
end
|
238
258
|
end
|
239
259
|
|
240
|
-
|
241
|
-
# Forward and reverse geocoded model.
|
242
|
-
# Should fill in whatever's missing (coords or address).
|
243
|
-
#
|
244
|
-
class GasStation < ActiveRecord::Base
|
260
|
+
class PlaceWithForwardAndReverseGeocoding < ActiveRecord::Base
|
245
261
|
geocoded_by :address, :latitude => :lat, :longitude => :lon
|
246
262
|
reverse_geocoded_by :lat, :lon, :address => :location
|
247
263
|
|
@@ -251,25 +267,69 @@ class GasStation < ActiveRecord::Base
|
|
251
267
|
end
|
252
268
|
end
|
253
269
|
|
270
|
+
class PlaceWithCustomLookup < ActiveRecord::Base
|
271
|
+
geocoded_by :address, :lookup => :nominatim do |obj,results|
|
272
|
+
if result = results.first
|
273
|
+
obj.result_class = result.class
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
def initialize(name, address)
|
278
|
+
super()
|
279
|
+
write_attribute :name, name
|
280
|
+
write_attribute :address, address
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
class PlaceWithCustomLookupProc < ActiveRecord::Base
|
285
|
+
geocoded_by :address, :lookup => lambda{|obj| obj.custom_lookup } do |obj,results|
|
286
|
+
if result = results.first
|
287
|
+
obj.result_class = result.class
|
288
|
+
end
|
289
|
+
end
|
254
290
|
|
255
|
-
|
291
|
+
def custom_lookup
|
292
|
+
:nominatim
|
293
|
+
end
|
256
294
|
|
257
|
-
def
|
258
|
-
|
295
|
+
def initialize(name, address)
|
296
|
+
super()
|
297
|
+
write_attribute :name, name
|
298
|
+
write_attribute :address, address
|
259
299
|
end
|
300
|
+
end
|
260
301
|
|
261
|
-
|
262
|
-
|
263
|
-
|
302
|
+
class PlaceReverseGeocodedWithCustomLookup < ActiveRecord::Base
|
303
|
+
reverse_geocoded_by :latitude, :longitude, :lookup => :nominatim do |obj,results|
|
304
|
+
if result = results.first
|
305
|
+
obj.result_class = result.class
|
306
|
+
end
|
264
307
|
end
|
265
308
|
|
266
|
-
def
|
309
|
+
def initialize(name, latitude, longitude)
|
310
|
+
super()
|
311
|
+
write_attribute :name, name
|
312
|
+
write_attribute :latitude, latitude
|
313
|
+
write_attribute :longitude, longitude
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
|
318
|
+
class GeocoderTestCase < Test::Unit::TestCase
|
319
|
+
|
320
|
+
def setup
|
321
|
+
super
|
322
|
+
Geocoder::Configuration.instance.set_defaults
|
323
|
+
Geocoder.configure(:maxmind => {:service => :city_isp_org})
|
324
|
+
end
|
325
|
+
|
326
|
+
def geocoded_object_params(abbrev)
|
267
327
|
{
|
268
328
|
:msg => ["Madison Square Garden", "4 Penn Plaza, New York, NY"]
|
269
329
|
}[abbrev]
|
270
330
|
end
|
271
331
|
|
272
|
-
def
|
332
|
+
def reverse_geocoded_object_params(abbrev)
|
273
333
|
{
|
274
334
|
:msg => ["Madison Square Garden", 40.750354, -73.993371]
|
275
335
|
}[abbrev]
|
@@ -287,3 +347,11 @@ class Test::Unit::TestCase
|
|
287
347
|
Geocoder.configure(:api_key => key)
|
288
348
|
end
|
289
349
|
end
|
350
|
+
|
351
|
+
class MockHttpResponse
|
352
|
+
attr_reader :code, :body
|
353
|
+
def initialize(options = {})
|
354
|
+
@code = options[:code].to_s
|
355
|
+
@body = options[:body]
|
356
|
+
end
|
357
|
+
end
|
@@ -1,15 +1,16 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
|
-
class ActiveRecordTest <
|
5
|
+
class ActiveRecordTest < GeocoderTestCase
|
5
6
|
|
6
7
|
def test_exclude_condition_when_model_has_a_custom_primary_key
|
7
|
-
venue =
|
8
|
+
venue = PlaceWithCustomPrimaryKey.new(*geocoded_object_params(:msg))
|
8
9
|
|
9
10
|
# just call private method directly so we don't have to stub .near scope
|
10
11
|
conditions = venue.class.send(:add_exclude_condition, ["fake_condition"], venue)
|
11
12
|
|
12
|
-
assert_match( /#{
|
13
|
+
assert_match( /#{PlaceWithCustomPrimaryKey.primary_key}/, conditions.join)
|
13
14
|
end
|
14
15
|
|
15
16
|
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
|
-
class CacheTest <
|
5
|
+
class CacheTest < GeocoderTestCase
|
5
6
|
|
6
7
|
def test_second_occurrence_of_request_is_cache_hit
|
7
8
|
Geocoder.configure(:cache => {})
|
8
9
|
Geocoder::Lookup.all_services_except_test.each do |l|
|
10
|
+
next if l == :maxmind_local # local, does not use cache
|
9
11
|
Geocoder.configure(:lookup => l)
|
10
12
|
set_api_key!(l)
|
11
13
|
results = Geocoder.search("Madison Square Garden")
|
@@ -1,7 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
|
-
class CalculationsTest <
|
5
|
+
class CalculationsTest < GeocoderTestCase
|
5
6
|
def setup
|
6
7
|
Geocoder.configure(
|
7
8
|
:units => :mi,
|
@@ -56,7 +57,7 @@ class CalculationsTest < Test::Unit::TestCase
|
|
56
57
|
|
57
58
|
def test_geographic_center_with_mixed_arguments
|
58
59
|
p1 = [0, 0]
|
59
|
-
p2 =
|
60
|
+
p2 = PlaceReverseGeocoded.new("Some Cold Place", 0, 1)
|
60
61
|
assert_equal [0.0, 0.5], Geocoder::Calculations.geographic_center([p1, p2])
|
61
62
|
end
|
62
63
|
|
@@ -66,8 +67,6 @@ class CalculationsTest < Test::Unit::TestCase
|
|
66
67
|
def test_bounding_box_calculation_in_miles
|
67
68
|
center = [51, 7] # Cologne, DE
|
68
69
|
radius = 10 # miles
|
69
|
-
dlon = radius / Geocoder::Calculations.latitude_degree_distance
|
70
|
-
dlat = radius / Geocoder::Calculations.longitude_degree_distance(center[0])
|
71
70
|
corners = [50.86, 6.77, 51.14, 7.23]
|
72
71
|
assert_equal corners.map{ |i| (i * 100).round },
|
73
72
|
Geocoder::Calculations.bounding_box(center, radius).map{ |i| (i * 100).round }
|
@@ -76,8 +75,6 @@ class CalculationsTest < Test::Unit::TestCase
|
|
76
75
|
def test_bounding_box_calculation_in_kilometers
|
77
76
|
center = [51, 7] # Cologne, DE
|
78
77
|
radius = 111 # kilometers (= 1 degree latitude)
|
79
|
-
dlon = radius / Geocoder::Calculations.latitude_degree_distance(:km)
|
80
|
-
dlat = radius / Geocoder::Calculations.longitude_degree_distance(center[0], :km)
|
81
78
|
corners = [50, 5.41, 52, 8.59]
|
82
79
|
assert_equal corners.map{ |i| (i * 100).round },
|
83
80
|
Geocoder::Calculations.bounding_box(center, radius, :units => :km).map{ |i| (i * 100).round }
|
@@ -86,10 +83,8 @@ class CalculationsTest < Test::Unit::TestCase
|
|
86
83
|
def test_bounding_box_calculation_with_object
|
87
84
|
center = [51, 7] # Cologne, DE
|
88
85
|
radius = 10 # miles
|
89
|
-
dlon = radius / Geocoder::Calculations.latitude_degree_distance
|
90
|
-
dlat = radius / Geocoder::Calculations.longitude_degree_distance(center[0])
|
91
86
|
corners = [50.86, 6.77, 51.14, 7.23]
|
92
|
-
obj =
|
87
|
+
obj = PlaceReverseGeocoded.new("Cologne", center[0], center[1])
|
93
88
|
assert_equal corners.map{ |i| (i * 100).round },
|
94
89
|
Geocoder::Calculations.bounding_box(obj, radius).map{ |i| (i * 100).round }
|
95
90
|
end
|
@@ -153,23 +148,23 @@ class CalculationsTest < Test::Unit::TestCase
|
|
153
148
|
end
|
154
149
|
|
155
150
|
def test_spherical_bearing_to
|
156
|
-
l =
|
151
|
+
l = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
|
157
152
|
assert_equal 324, l.bearing_to([50,-85], :method => :spherical).round
|
158
153
|
end
|
159
154
|
|
160
155
|
def test_spherical_bearing_from
|
161
|
-
l =
|
156
|
+
l = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
|
162
157
|
assert_equal 136, l.bearing_from([50,-85], :method => :spherical).round
|
163
158
|
end
|
164
159
|
|
165
160
|
def test_linear_bearing_from_and_to_are_exactly_opposite
|
166
|
-
l =
|
161
|
+
l = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
|
167
162
|
assert_equal l.bearing_from([50,-86.1]), l.bearing_to([50,-86.1]) - 180
|
168
163
|
end
|
169
164
|
|
170
165
|
def test_extract_coordinates
|
171
166
|
coords = [-23,47]
|
172
|
-
l =
|
167
|
+
l = PlaceReverseGeocoded.new("Madagascar", coords[0], coords[1])
|
173
168
|
assert_equal coords, Geocoder::Calculations.extract_coordinates(l)
|
174
169
|
assert_equal coords, Geocoder::Calculations.extract_coordinates(coords)
|
175
170
|
end
|
@@ -208,4 +203,18 @@ class CalculationsTest < Test::Unit::TestCase
|
|
208
203
|
value[1].nan?,
|
209
204
|
"Expected value to be [NaN, NaN] but was #{value}"
|
210
205
|
end
|
206
|
+
|
207
|
+
def test_endpoint
|
208
|
+
# test 5 time with random coordinates and headings
|
209
|
+
[0..5].each do |i|
|
210
|
+
rheading = [*0..359].sample
|
211
|
+
rdistance = [*0..100].sample
|
212
|
+
startpoint = [45.0906, 7.6596]
|
213
|
+
endpoint = Geocoder::Calculations.endpoint(startpoint, rheading, rdistance)
|
214
|
+
assert_in_delta rdistance,
|
215
|
+
Geocoder::Calculations.distance_between(startpoint, endpoint, :method => :spherical), 1E-5
|
216
|
+
assert_in_delta rheading,
|
217
|
+
Geocoder::Calculations.bearing_between(startpoint, endpoint, :method => :spherical), 1E-2
|
218
|
+
end
|
219
|
+
end
|
211
220
|
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
|
-
class ConfigurationTest <
|
5
|
+
class ConfigurationTest < GeocoderTestCase
|
5
6
|
def setup
|
6
7
|
Geocoder::Configuration.set_defaults
|
7
8
|
end
|
@@ -23,16 +24,6 @@ class ConfigurationTest < Test::Unit::TestCase
|
|
23
24
|
assert_equal :test, Geocoder.config.units
|
24
25
|
end
|
25
26
|
|
26
|
-
def test_setting_with_block_syntax
|
27
|
-
orig = $VERBOSE; $VERBOSE = nil
|
28
|
-
Geocoder.configure do |config|
|
29
|
-
config.units = :test
|
30
|
-
end
|
31
|
-
assert_equal :test, Geocoder.config.units
|
32
|
-
ensure
|
33
|
-
$VERBOSE = orig
|
34
|
-
end
|
35
|
-
|
36
27
|
def test_config_for_lookup
|
37
28
|
Geocoder.configure(
|
38
29
|
:timeout => 5,
|
@@ -45,22 +36,8 @@ class ConfigurationTest < Test::Unit::TestCase
|
|
45
36
|
assert_equal "aaa", Geocoder.config_for_lookup(:google).api_key
|
46
37
|
end
|
47
38
|
|
48
|
-
def test_model_configuration
|
49
|
-
Landmark.reverse_geocoded_by :latitude, :longitude, :method => :spherical, :units => :km
|
50
|
-
assert_equal :km, Landmark.geocoder_options[:units]
|
51
|
-
assert_equal :spherical, Landmark.geocoder_options[:method]
|
52
|
-
|
53
|
-
v = Landmark.new(*landmark_params(:msg))
|
54
|
-
v.latitude = 0
|
55
|
-
v.longitude = 0
|
56
|
-
assert_equal 111, v.distance_to([0,1]).round
|
57
|
-
v.latitude = 40.750354
|
58
|
-
v.longitude = -73.993371
|
59
|
-
assert_equal 136, v.bearing_from([50,-85]).round
|
60
|
-
end
|
61
|
-
|
62
39
|
def test_configuration_chain
|
63
|
-
v =
|
40
|
+
v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
|
64
41
|
v.latitude = 0
|
65
42
|
v.longitude = 0
|
66
43
|
|
@@ -69,7 +46,7 @@ class ConfigurationTest < Test::Unit::TestCase
|
|
69
46
|
assert_equal 69, v.distance_to([0,1], :mi).round
|
70
47
|
|
71
48
|
# per-model configuration > global configuration
|
72
|
-
|
49
|
+
PlaceReverseGeocoded.reverse_geocoded_by :latitude, :longitude, method: :spherical, units: :mi
|
73
50
|
assert_equal 69, v.distance_to([0,1]).round
|
74
51
|
|
75
52
|
# method option > per-model configuration
|
@@ -1,27 +1,27 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
|
-
class ErrorHandlingTest <
|
5
|
+
class ErrorHandlingTest < GeocoderTestCase
|
5
6
|
|
6
7
|
def teardown
|
7
8
|
Geocoder.configure(:always_raise => [])
|
8
9
|
end
|
9
10
|
|
10
11
|
def test_does_not_choke_on_timeout
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
silence_warnings do
|
13
|
+
Geocoder::Lookup.all_services_except_test.each do |l|
|
14
|
+
Geocoder.configure(:lookup => l)
|
15
|
+
set_api_key!(l)
|
16
|
+
assert_nothing_raised { Geocoder.search("timeout") }
|
17
|
+
end
|
17
18
|
end
|
18
|
-
ensure
|
19
|
-
$VERBOSE = orig
|
20
19
|
end
|
21
20
|
|
22
21
|
def test_always_raise_timeout_error
|
23
22
|
Geocoder.configure(:always_raise => [TimeoutError])
|
24
23
|
Geocoder::Lookup.all_services_except_test.each do |l|
|
24
|
+
next if l == :maxmind_local # local, does not raise timeout
|
25
25
|
lookup = Geocoder::Lookup.get(l)
|
26
26
|
set_api_key!(l)
|
27
27
|
assert_raises TimeoutError do
|
@@ -33,6 +33,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
|
|
33
33
|
def test_always_raise_socket_error
|
34
34
|
Geocoder.configure(:always_raise => [SocketError])
|
35
35
|
Geocoder::Lookup.all_services_except_test.each do |l|
|
36
|
+
next if l == :maxmind_local # local, does not raise timeout
|
36
37
|
lookup = Geocoder::Lookup.get(l)
|
37
38
|
set_api_key!(l)
|
38
39
|
assert_raises SocketError do
|