geocoder-kb 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +31 -0
- data/CHANGELOG.md +384 -0
- data/LICENSE +20 -0
- data/README.md +1085 -0
- data/Rakefile +25 -0
- data/bin/geocode +5 -0
- data/examples/autoexpire_cache_dalli.rb +62 -0
- data/examples/autoexpire_cache_redis.rb +28 -0
- data/examples/cache_bypass.rb +48 -0
- data/examples/sidekiq_worker.rb +16 -0
- data/gemfiles/Gemfile.mongoid-2.4.x +16 -0
- data/lib/generators/geocoder/config/config_generator.rb +14 -0
- data/lib/generators/geocoder/config/templates/initializer.rb +21 -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 +30 -0
- data/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +17 -0
- data/lib/geocoder.rb +47 -0
- data/lib/geocoder/cache.rb +90 -0
- data/lib/geocoder/calculations.rb +428 -0
- data/lib/geocoder/cli.rb +121 -0
- data/lib/geocoder/configuration.rb +124 -0
- data/lib/geocoder/configuration_hash.rb +11 -0
- data/lib/geocoder/exceptions.rb +21 -0
- data/lib/geocoder/ip_address.rb +21 -0
- data/lib/geocoder/lookup.rb +102 -0
- data/lib/geocoder/lookups/amap.rb +55 -0
- data/lib/geocoder/lookups/baidu.rb +55 -0
- data/lib/geocoder/lookups/baidu_ip.rb +54 -0
- data/lib/geocoder/lookups/base.rb +302 -0
- data/lib/geocoder/lookups/bing.rb +59 -0
- data/lib/geocoder/lookups/dstk.rb +20 -0
- data/lib/geocoder/lookups/esri.rb +48 -0
- data/lib/geocoder/lookups/freegeoip.rb +47 -0
- data/lib/geocoder/lookups/geocoder_ca.rb +54 -0
- data/lib/geocoder/lookups/geocoder_us.rb +39 -0
- data/lib/geocoder/lookups/geocodio.rb +42 -0
- data/lib/geocoder/lookups/geoip2.rb +40 -0
- data/lib/geocoder/lookups/google.rb +67 -0
- data/lib/geocoder/lookups/google_places_details.rb +50 -0
- data/lib/geocoder/lookups/google_premier.rb +47 -0
- data/lib/geocoder/lookups/here.rb +62 -0
- data/lib/geocoder/lookups/mapquest.rb +60 -0
- data/lib/geocoder/lookups/maxmind.rb +90 -0
- data/lib/geocoder/lookups/maxmind_local.rb +58 -0
- data/lib/geocoder/lookups/nominatim.rb +52 -0
- data/lib/geocoder/lookups/okf.rb +43 -0
- data/lib/geocoder/lookups/opencagedata.rb +58 -0
- data/lib/geocoder/lookups/ovi.rb +62 -0
- data/lib/geocoder/lookups/pointpin.rb +68 -0
- data/lib/geocoder/lookups/postcode_anywhere_uk.rb +51 -0
- data/lib/geocoder/lookups/smarty_streets.rb +45 -0
- data/lib/geocoder/lookups/telize.rb +40 -0
- data/lib/geocoder/lookups/test.rb +44 -0
- data/lib/geocoder/lookups/yahoo.rb +88 -0
- data/lib/geocoder/lookups/yandex.rb +54 -0
- data/lib/geocoder/models/active_record.rb +50 -0
- data/lib/geocoder/models/base.rb +39 -0
- data/lib/geocoder/models/mongo_base.rb +64 -0
- data/lib/geocoder/models/mongo_mapper.rb +26 -0
- data/lib/geocoder/models/mongoid.rb +32 -0
- data/lib/geocoder/query.rb +111 -0
- data/lib/geocoder/railtie.rb +26 -0
- data/lib/geocoder/request.rb +25 -0
- data/lib/geocoder/results/amap.rb +85 -0
- data/lib/geocoder/results/baidu.rb +79 -0
- data/lib/geocoder/results/baidu_ip.rb +62 -0
- data/lib/geocoder/results/base.rb +67 -0
- data/lib/geocoder/results/bing.rb +48 -0
- data/lib/geocoder/results/dstk.rb +6 -0
- data/lib/geocoder/results/esri.rb +51 -0
- data/lib/geocoder/results/freegeoip.rb +45 -0
- data/lib/geocoder/results/geocoder_ca.rb +60 -0
- data/lib/geocoder/results/geocoder_us.rb +39 -0
- data/lib/geocoder/results/geocodio.rb +66 -0
- data/lib/geocoder/results/geoip2.rb +64 -0
- data/lib/geocoder/results/google.rb +124 -0
- data/lib/geocoder/results/google_places_details.rb +35 -0
- data/lib/geocoder/results/google_premier.rb +6 -0
- data/lib/geocoder/results/here.rb +62 -0
- data/lib/geocoder/results/mapquest.rb +51 -0
- data/lib/geocoder/results/maxmind.rb +135 -0
- data/lib/geocoder/results/maxmind_local.rb +49 -0
- data/lib/geocoder/results/nominatim.rb +94 -0
- data/lib/geocoder/results/okf.rb +106 -0
- data/lib/geocoder/results/opencagedata.rb +82 -0
- data/lib/geocoder/results/ovi.rb +62 -0
- data/lib/geocoder/results/pointpin.rb +44 -0
- data/lib/geocoder/results/postcode_anywhere_uk.rb +42 -0
- data/lib/geocoder/results/smarty_streets.rb +106 -0
- data/lib/geocoder/results/telize.rb +45 -0
- data/lib/geocoder/results/test.rb +33 -0
- data/lib/geocoder/results/yahoo.rb +55 -0
- data/lib/geocoder/results/yandex.rb +84 -0
- data/lib/geocoder/sql.rb +107 -0
- data/lib/geocoder/stores/active_record.rb +289 -0
- data/lib/geocoder/stores/base.rb +127 -0
- data/lib/geocoder/stores/mongo_base.rb +89 -0
- data/lib/geocoder/stores/mongo_mapper.rb +13 -0
- data/lib/geocoder/stores/mongoid.rb +13 -0
- data/lib/geocoder/version.rb +3 -0
- data/lib/hash_recursive_merge.rb +74 -0
- data/lib/maxmind_database.rb +109 -0
- data/lib/oauth_util.rb +112 -0
- data/lib/tasks/geocoder.rake +29 -0
- data/lib/tasks/maxmind.rake +73 -0
- data/test/fixtures/baidu_invalid_key +1 -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/baidu_no_results +1 -0
- data/test/fixtures/baidu_reverse +1 -0
- data/test/fixtures/baidu_shanghai_pearl_tower +12 -0
- data/test/fixtures/bing_invalid_key +1 -0
- data/test/fixtures/bing_madison_square_garden +40 -0
- data/test/fixtures/bing_no_results +16 -0
- data/test/fixtures/bing_reverse +42 -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/esri_madison_square_garden +59 -0
- data/test/fixtures/esri_no_results +8 -0
- data/test/fixtures/esri_reverse +21 -0
- data/test/fixtures/freegeoip_74_200_247_59 +12 -0
- data/test/fixtures/freegeoip_no_results +1 -0
- data/test/fixtures/geocoder_ca_madison_square_garden +1 -0
- data/test/fixtures/geocoder_ca_no_results +1 -0
- data/test/fixtures/geocoder_ca_reverse +34 -0
- data/test/fixtures/geocoder_us_madison_square_garden +1 -0
- data/test/fixtures/geocoder_us_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/google_garbage +456 -0
- data/test/fixtures/google_madison_square_garden +57 -0
- data/test/fixtures/google_no_city_data +44 -0
- data/test/fixtures/google_no_locality +51 -0
- data/test/fixtures/google_no_results +4 -0
- data/test/fixtures/google_over_limit +4 -0
- data/test/fixtures/google_places_details_invalid_request +4 -0
- data/test/fixtures/google_places_details_madison_square_garden +120 -0
- data/test/fixtures/google_places_details_no_results +4 -0
- data/test/fixtures/google_places_details_no_reviews +60 -0
- data/test/fixtures/google_places_details_no_types +66 -0
- data/test/fixtures/here_madison_square_garden +72 -0
- data/test/fixtures/here_no_results +8 -0
- data/test/fixtures/mapquest_error +16 -0
- data/test/fixtures/mapquest_invalid_api_key +16 -0
- data/test/fixtures/mapquest_invalid_request +16 -0
- data/test/fixtures/mapquest_madison_square_garden +52 -0
- data/test/fixtures/mapquest_no_results +16 -0
- data/test/fixtures/maxmind_24_24_24_21 +1 -0
- data/test/fixtures/maxmind_24_24_24_22 +1 -0
- data/test/fixtures/maxmind_24_24_24_23 +1 -0
- data/test/fixtures/maxmind_24_24_24_24 +1 -0
- data/test/fixtures/maxmind_74_200_247_59 +1 -0
- data/test/fixtures/maxmind_invalid_key +1 -0
- data/test/fixtures/maxmind_no_results +1 -0
- data/test/fixtures/nominatim_madison_square_garden +150 -0
- data/test/fixtures/nominatim_no_results +1 -0
- data/test/fixtures/nominatim_over_limit +1 -0
- data/test/fixtures/okf_kirstinmaki +67 -0
- data/test/fixtures/okf_no_results +4 -0
- data/test/fixtures/opencagedata_invalid_api_key +25 -0
- data/test/fixtures/opencagedata_invalid_request +26 -0
- data/test/fixtures/opencagedata_madison_square_garden +73 -0
- data/test/fixtures/opencagedata_no_results +29 -0
- data/test/fixtures/opencagedata_over_limit +31 -0
- data/test/fixtures/ovi_madison_square_garden +72 -0
- data/test/fixtures/ovi_no_results +8 -0
- data/test/fixtures/pointpin_10_10_10_10 +1 -0
- data/test/fixtures/pointpin_555_555_555_555 +1 -0
- data/test/fixtures/pointpin_80_111_555_555 +1 -0
- data/test/fixtures/pointpin_no_results +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_WR26NJ +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_generic_error +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_key_limit_exceeded +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_no_results +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_romsey +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_unknown_key +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/telize_10_10_10_10 +1 -0
- data/test/fixtures/telize_555_555_555_555 +4 -0
- data/test/fixtures/telize_74_200_247_59 +1 -0
- data/test/fixtures/telize_no_results +1 -0
- data/test/fixtures/yahoo_error +1 -0
- data/test/fixtures/yahoo_invalid_key +2 -0
- data/test/fixtures/yahoo_madison_square_garden +52 -0
- data/test/fixtures/yahoo_no_results +10 -0
- data/test/fixtures/yahoo_over_limit +2 -0
- data/test/fixtures/yandex_canada_rue_dupuis_14 +446 -0
- data/test/fixtures/yandex_invalid_key +1 -0
- data/test/fixtures/yandex_kremlin +48 -0
- data/test/fixtures/yandex_new_york +1 -0
- data/test/fixtures/yandex_no_city_and_town +112 -0
- data/test/fixtures/yandex_no_results +16 -0
- data/test/integration/http_client_test.rb +31 -0
- data/test/mongoid_test_helper.rb +43 -0
- data/test/test_helper.rb +416 -0
- data/test/unit/active_record_test.rb +16 -0
- data/test/unit/cache_test.rb +37 -0
- data/test/unit/calculations_test.rb +220 -0
- data/test/unit/configuration_test.rb +55 -0
- data/test/unit/error_handling_test.rb +56 -0
- data/test/unit/geocoder_test.rb +78 -0
- data/test/unit/https_test.rb +17 -0
- data/test/unit/ip_address_test.rb +27 -0
- data/test/unit/lookup_test.rb +153 -0
- 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/geoip2_test.rb +27 -0
- data/test/unit/lookups/google_places_details_test.rb +122 -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/okf_test.rb +38 -0
- data/test/unit/lookups/opencagedata_test.rb +64 -0
- data/test/unit/lookups/pointpin_test.rb +30 -0
- data/test/unit/lookups/postcode_anywhere_uk_test.rb +70 -0
- data/test/unit/lookups/smarty_streets_test.rb +71 -0
- data/test/unit/lookups/telize_test.rb +36 -0
- data/test/unit/lookups/yahoo_test.rb +35 -0
- data/test/unit/method_aliases_test.rb +26 -0
- data/test/unit/model_test.rb +38 -0
- data/test/unit/mongoid_test.rb +47 -0
- data/test/unit/near_test.rb +87 -0
- data/test/unit/oauth_util_test.rb +31 -0
- data/test/unit/proxy_test.rb +37 -0
- data/test/unit/query_test.rb +52 -0
- data/test/unit/rake_task_test.rb +21 -0
- data/test/unit/request_test.rb +35 -0
- data/test/unit/result_test.rb +72 -0
- data/test/unit/test_mode_test.rb +70 -0
- metadata +294 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class OpencagedataTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :opencagedata)
|
9
|
+
set_api_key!(:opencagedata)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_result_components
|
13
|
+
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
14
|
+
assert_equal "West 31st Street", result.street
|
15
|
+
assert_match /46, West 31st Street, Koreatown, New York County, 10011, New York City, New York, United States of America/, result.address
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_opencagedata_query_url_contains_bounds
|
20
|
+
lookup = Geocoder::Lookup::Opencagedata.new
|
21
|
+
url = lookup.query_url(Geocoder::Query.new(
|
22
|
+
"Some street",
|
23
|
+
:bounds => [[40.0, -120.0], [39.0, -121.0]]
|
24
|
+
))
|
25
|
+
assert_match(/bounds=40.0+%2C-120.0+%2C39.0+%2C-121.0+/, url)
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def test_no_results
|
30
|
+
results = Geocoder.search("no results")
|
31
|
+
assert_equal 0, results.length
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def test_opencagedata_reverse_url
|
36
|
+
query = Geocoder::Query.new([45.423733, -75.676333])
|
37
|
+
assert_match /\bq=45.423733%2C-75.676333\b/, query.url
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
def test_raises_exception_when_invalid_request
|
43
|
+
Geocoder.configure(always_raise: [Geocoder::InvalidRequest])
|
44
|
+
assert_raises Geocoder::InvalidRequest do
|
45
|
+
Geocoder.search("invalid request")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_raises_exception_when_invalid_api_key
|
50
|
+
Geocoder.configure(always_raise: [Geocoder::InvalidApiKey])
|
51
|
+
assert_raises Geocoder::InvalidApiKey do
|
52
|
+
Geocoder.search("invalid api key")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def test_raises_exception_when_over_query_limit
|
58
|
+
Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
|
59
|
+
l = Geocoder::Lookup.get(:opencagedata)
|
60
|
+
assert_raises Geocoder::OverQueryLimitError do
|
61
|
+
l.send(:results, Geocoder::Query.new("over limit"))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class PointpinTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(ip_lookup: :pointpin, api_key: "abc123")
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_result_on_ip_address_search
|
12
|
+
result = Geocoder.search("80.111.555.555").first
|
13
|
+
assert result.is_a?(Geocoder::Result::Pointpin)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_result_components
|
17
|
+
result = Geocoder.search("80.111.555.555").first
|
18
|
+
assert_equal "Dublin, Dublin City, 8, Ireland", result.address
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_no_results
|
22
|
+
results = Geocoder.search("10.10.10.10")
|
23
|
+
assert_equal 0, results.length
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_invalid_address
|
27
|
+
results = Geocoder.search("555.555.555.555")
|
28
|
+
assert_equal 0, results.length
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), '..', '..')
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class PostcodeAnywhereUkTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :postcode_anywhere_uk)
|
9
|
+
set_api_key!(:postcode_anywhere_uk)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_result_components_with_placename_search
|
13
|
+
results = Geocoder.search('Romsey')
|
14
|
+
|
15
|
+
assert_equal 1, results.size
|
16
|
+
assert_equal 'Romsey, Hampshire', results.first.address
|
17
|
+
assert_equal 'SU 35270 21182', results.first.os_grid
|
18
|
+
assert_equal [50.9889, -1.4989], results.first.coordinates
|
19
|
+
assert_equal 'Romsey', results.first.city
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_result_components_with_postcode
|
23
|
+
results = Geocoder.search('WR26NJ')
|
24
|
+
|
25
|
+
assert_equal 1, results.size
|
26
|
+
assert_equal 'Moseley Road, Hallow, Worcester', results.first.address
|
27
|
+
assert_equal 'SO 81676 59425', results.first.os_grid
|
28
|
+
assert_equal [52.2327, -2.2697], results.first.coordinates
|
29
|
+
assert_equal 'Hallow', results.first.city
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_result_components_with_county
|
33
|
+
results = Geocoder.search('hampshire')
|
34
|
+
|
35
|
+
assert_equal 1, results.size
|
36
|
+
assert_equal 'Hampshire', results.first.address
|
37
|
+
assert_equal 'SU 48701 26642', results.first.os_grid
|
38
|
+
assert_equal [51.037, -1.3068], results.first.coordinates
|
39
|
+
assert_equal '', results.first.city
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_no_results
|
43
|
+
assert_equal [], Geocoder.search('no results')
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_key_limit_exceeded_error
|
47
|
+
Geocoder.configure(always_raise: [Geocoder::OverQueryLimitError])
|
48
|
+
|
49
|
+
assert_raises Geocoder::OverQueryLimitError do
|
50
|
+
Geocoder.search('key limit exceeded')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_unknown_key_error
|
55
|
+
Geocoder.configure(always_raise: [Geocoder::InvalidApiKey])
|
56
|
+
|
57
|
+
assert_raises Geocoder::InvalidApiKey do
|
58
|
+
Geocoder.search('unknown key')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_generic_error
|
63
|
+
Geocoder.configure(always_raise: [Geocoder::Error])
|
64
|
+
|
65
|
+
exception = assert_raises(Geocoder::Error) do
|
66
|
+
Geocoder.search('generic error')
|
67
|
+
end
|
68
|
+
assert_equal 'A generic error occured.', exception.message
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class SmartyStreetsTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :smarty_streets)
|
9
|
+
set_api_key!(:smarty_streets)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_url_contains_api_key
|
13
|
+
Geocoder.configure(:smarty_streets => {:api_key => 'blah'})
|
14
|
+
query = Geocoder::Query.new("Bluffton, SC")
|
15
|
+
assert_equal "http://api.smartystreets.com/street-address?auth-token=blah&street=Bluffton%2C+SC", query.url
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_query_for_address_geocode
|
19
|
+
query = Geocoder::Query.new("42 Wallaby Way Sydney, AU")
|
20
|
+
assert_match /api\.smartystreets\.com\/street-address\?/, query.url
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_query_for_zipcode_geocode
|
24
|
+
query = Geocoder::Query.new("22204")
|
25
|
+
assert_match /api\.smartystreets\.com\/zipcode\?/, query.url
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_query_for_zipfour_geocode
|
29
|
+
query = Geocoder::Query.new("22204-1603")
|
30
|
+
assert_match /api\.smartystreets\.com\/zipcode\?/, query.url
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_smarty_streets_result_components
|
34
|
+
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
35
|
+
assert_equal "Penn", result.street
|
36
|
+
assert_equal "10121", result.zipcode
|
37
|
+
assert_equal "1703", result.zip4
|
38
|
+
assert_equal "New York", result.city
|
39
|
+
assert_equal "36061", result.fips
|
40
|
+
assert !result.zipcode_endpoint?
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_smarty_streets_result_components_with_zipcode_only_query
|
44
|
+
result = Geocoder.search("11211").first
|
45
|
+
assert_equal "Brooklyn", result.city
|
46
|
+
assert_equal "New York", result.state
|
47
|
+
assert_equal "NY", result.state_code
|
48
|
+
assert result.zipcode_endpoint?
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_no_results
|
52
|
+
results = Geocoder.search("no results")
|
53
|
+
assert_equal 0, results.length
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_raises_exception_on_error_http_status
|
57
|
+
error_statuses = {
|
58
|
+
'400' => Geocoder::InvalidRequest,
|
59
|
+
'401' => Geocoder::RequestDenied,
|
60
|
+
'402' => Geocoder::OverQueryLimitError
|
61
|
+
}
|
62
|
+
Geocoder.configure(always_raise: error_statuses.values)
|
63
|
+
lookup = Geocoder::Lookup.get(:smarty_streets)
|
64
|
+
error_statuses.each do |code, err|
|
65
|
+
assert_raises err do
|
66
|
+
response = MockHttpResponse.new(code: code.to_i)
|
67
|
+
lookup.send(:check_response_for_errors!, response)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class TelizeTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(ip_lookup: :telize)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_result_on_ip_address_search
|
12
|
+
result = Geocoder.search("74.200.247.59").first
|
13
|
+
assert result.is_a?(Geocoder::Result::Telize)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_result_components
|
17
|
+
result = Geocoder.search("74.200.247.59").first
|
18
|
+
assert_equal "Plano, TX 75093, United States", result.address
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_no_results
|
22
|
+
results = Geocoder.search("10.10.10.10")
|
23
|
+
assert_equal 0, results.length
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_invalid_address
|
27
|
+
results = Geocoder.search("555.555.555.555")
|
28
|
+
assert_equal 0, results.length
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_uses_http_even_if_use_https_true
|
32
|
+
Geocoder.configure(use_https: true)
|
33
|
+
result = Geocoder.search("74.200.247.59").first
|
34
|
+
assert result.is_a?(Geocoder::Result::Telize)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class YahooTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :yahoo)
|
9
|
+
set_api_key!(:yahoo)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_no_results
|
13
|
+
assert_equal [], Geocoder.search("no results")
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_error
|
17
|
+
silence_warnings do
|
18
|
+
assert_equal [], Geocoder.search("error")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_result_components
|
23
|
+
result = Geocoder.search("madison square garden").first
|
24
|
+
assert_equal "10001", result.postal_code
|
25
|
+
assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_raises_exception_when_over_query_limit
|
29
|
+
Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
|
30
|
+
l = Geocoder::Lookup.get(:yahoo)
|
31
|
+
assert_raises Geocoder::OverQueryLimitError do
|
32
|
+
l.send(:results, Geocoder::Query.new("over limit"))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class MethodAliasesTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def test_distance_from_is_alias_for_distance_to
|
8
|
+
v = Place.new(*geocoded_object_params(:msg))
|
9
|
+
v.latitude, v.longitude = [40.750354, -73.993371]
|
10
|
+
assert_equal v.distance_from([30, -94]), v.distance_to([30, -94])
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_fetch_coordinates_is_alias_for_geocode
|
14
|
+
v = Place.new(*geocoded_object_params(:msg))
|
15
|
+
coords = [40.750354, -73.993371]
|
16
|
+
assert_equal coords, v.fetch_coordinates
|
17
|
+
assert_equal coords, [v.latitude, v.longitude]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_fetch_address_is_alias_for_reverse_geocode
|
21
|
+
v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
|
22
|
+
address = "4 Penn Plaza, New York, NY 10001, USA"
|
23
|
+
assert_equal address, v.fetch_address
|
24
|
+
assert_equal address, v.address
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ModelTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def test_geocode_with_block_runs_block
|
8
|
+
e = PlaceWithCustomResultsHandling.new(*geocoded_object_params(:msg))
|
9
|
+
coords = [40.750354, -73.993371]
|
10
|
+
e.geocode
|
11
|
+
assert_equal coords.map{ |c| c.to_s }.join(','), e.coords_string
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_geocode_with_block_doesnt_auto_assign_coordinates
|
15
|
+
e = PlaceWithCustomResultsHandling.new(*geocoded_object_params(:msg))
|
16
|
+
e.geocode
|
17
|
+
assert_nil e.latitude
|
18
|
+
assert_nil e.longitude
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_reverse_geocode_with_block_runs_block
|
22
|
+
e = PlaceReverseGeocodedWithCustomResultsHandling.new(*reverse_geocoded_object_params(:msg))
|
23
|
+
e.reverse_geocode
|
24
|
+
assert_equal "US", e.country
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_reverse_geocode_with_block_doesnt_auto_assign_address
|
28
|
+
e = PlaceReverseGeocodedWithCustomResultsHandling.new(*reverse_geocoded_object_params(:msg))
|
29
|
+
e.reverse_geocode
|
30
|
+
assert_nil e.address
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_units_and_method
|
34
|
+
PlaceReverseGeocoded.reverse_geocoded_by :latitude, :longitude, method: :spherical, units: :km
|
35
|
+
assert_equal :km, PlaceReverseGeocoded.geocoder_options[:units]
|
36
|
+
assert_equal :spherical, PlaceReverseGeocoded.geocoder_options[:method]
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
3
|
+
require 'mongoid_test_helper'
|
4
|
+
|
5
|
+
class MongoidTest < GeocoderTestCase
|
6
|
+
def test_geocoded_check
|
7
|
+
p = PlaceUsingMongoid.new(*geocoded_object_params(:msg))
|
8
|
+
p.location = [40.750354, -73.993371]
|
9
|
+
assert p.geocoded?
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_distance_to_returns_float
|
13
|
+
p = PlaceUsingMongoid.new(*geocoded_object_params(:msg))
|
14
|
+
p.location = [40.750354, -73.993371]
|
15
|
+
assert p.distance_to([30, -94]).is_a?(Float)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_custom_coordinate_field_near_scope
|
19
|
+
location = [40.750354, -73.993371]
|
20
|
+
p = PlaceUsingMongoid.near(location)
|
21
|
+
key = Mongoid::VERSION >= "3" ? "location" : :location
|
22
|
+
assert_equal p.selector[key]['$nearSphere'], location.reverse
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_model_configuration
|
26
|
+
p = PlaceUsingMongoid.new(*geocoded_object_params(:msg))
|
27
|
+
p.location = [0, 0]
|
28
|
+
|
29
|
+
PlaceUsingMongoid.geocoded_by :address, :coordinates => :location, :units => :km
|
30
|
+
assert_equal 111, p.distance_to([0,1]).round
|
31
|
+
|
32
|
+
PlaceUsingMongoid.geocoded_by :address, :coordinates => :location, :units => :mi
|
33
|
+
assert_equal 69, p.distance_to([0,1]).round
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_index_is_skipped_if_skip_option_flag
|
37
|
+
result = PlaceUsingMongoidWithoutIndex.index_options.keys.flatten[0] == :coordinates
|
38
|
+
assert !result
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_nil_radius_omits_max_distance
|
42
|
+
location = [40.750354, -73.993371]
|
43
|
+
p = PlaceUsingMongoid.near(location, nil)
|
44
|
+
key = Mongoid::VERSION >= "3" ? "location" : :location
|
45
|
+
assert_equal nil, p.selector[key]['$maxDistance']
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class NearTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def test_near_scope_options_without_sqlite_includes_bounding_box_condition
|
8
|
+
result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5)
|
9
|
+
assert_match(/test_table_name.latitude BETWEEN 0.9276\d* AND 1.0723\d* AND test_table_name.longitude BETWEEN 1.9276\d* AND 2.0723\d* AND /, result[:conditions][0])
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_near_scope_options_without_sqlite_includes_radius_condition
|
13
|
+
result = Place.send(:near_scope_options, 1.0, 2.0, 5)
|
14
|
+
assert_match(/BETWEEN \? AND \?$/, result[:conditions][0])
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_near_scope_options_without_sqlite_includes_radius_default_min_radius
|
18
|
+
result = Place.send(:near_scope_options, 1.0, 2.0, 5)
|
19
|
+
|
20
|
+
assert_equal(0, result[:conditions][1])
|
21
|
+
assert_equal(5, result[:conditions][2])
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_near_scope_options_without_sqlite_includes_radius_custom_min_radius
|
25
|
+
result = Place.send(:near_scope_options, 1.0, 2.0, 5, :min_radius => 3)
|
26
|
+
|
27
|
+
assert_equal(3, result[:conditions][1])
|
28
|
+
assert_equal(5, result[:conditions][2])
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_near_scope_options_without_sqlite_includes_radius_bogus_min_radius
|
32
|
+
result = Place.send(:near_scope_options, 1.0, 2.0, 5, :min_radius => 'bogus')
|
33
|
+
|
34
|
+
assert_equal(0, result[:conditions][1])
|
35
|
+
assert_equal(5, result[:conditions][2])
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_near_scope_options_with_defaults
|
39
|
+
result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5)
|
40
|
+
|
41
|
+
assert_match(/AS distance/, result[:select])
|
42
|
+
assert_match(/AS bearing/, result[:select])
|
43
|
+
assert_no_consecutive_comma(result[:select])
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_near_scope_options_with_no_distance
|
47
|
+
result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5, :select_distance => false)
|
48
|
+
|
49
|
+
assert_no_match(/AS distance/, result[:select])
|
50
|
+
assert_match(/AS bearing/, result[:select])
|
51
|
+
assert_no_match(/distance/, result[:condition])
|
52
|
+
assert_no_match(/distance/, result[:order])
|
53
|
+
assert_no_consecutive_comma(result[:select])
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_near_scope_options_with_no_bearing
|
57
|
+
result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5, :select_bearing => false)
|
58
|
+
|
59
|
+
assert_match(/AS distance/, result[:select])
|
60
|
+
assert_no_match(/AS bearing/, result[:select])
|
61
|
+
assert_no_consecutive_comma(result[:select])
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_near_scope_options_with_custom_distance_column
|
65
|
+
result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5, :distance_column => 'calculated_distance')
|
66
|
+
|
67
|
+
assert_no_match(/AS distance/, result[:select])
|
68
|
+
assert_match(/AS calculated_distance/, result[:select])
|
69
|
+
assert_no_match(/\bdistance\b/, result[:order])
|
70
|
+
assert_match(/calculated_distance/, result[:order])
|
71
|
+
assert_no_consecutive_comma(result[:select])
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_near_scope_options_with_custom_bearing_column
|
75
|
+
result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5, :bearing_column => 'calculated_bearing')
|
76
|
+
|
77
|
+
assert_no_match(/AS bearing/, result[:select])
|
78
|
+
assert_match(/AS calculated_bearing/, result[:select])
|
79
|
+
assert_no_consecutive_comma(result[:select])
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def assert_no_consecutive_comma(string)
|
85
|
+
assert_no_match(/, *,/, string, "two consecutive commas")
|
86
|
+
end
|
87
|
+
end
|