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,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class HttpsTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def test_uses_https_for_secure_query
|
8
|
+
Geocoder.configure(:use_https => true)
|
9
|
+
g = Geocoder::Lookup::Google.new
|
10
|
+
assert_match(/^https:/, g.query_url(Geocoder::Query.new("test")))
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_uses_http_by_default
|
14
|
+
g = Geocoder::Lookup::Google.new
|
15
|
+
assert_match(/^http:/, g.query_url(Geocoder::Query.new("test")))
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class IpAddressTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def test_valid
|
8
|
+
assert Geocoder::IpAddress.new("232.65.123.94").valid?
|
9
|
+
assert Geocoder::IpAddress.new("666.65.123.94").valid? # technically invalid
|
10
|
+
assert Geocoder::IpAddress.new("::ffff:12.34.56.78").valid?
|
11
|
+
assert Geocoder::IpAddress.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").valid?
|
12
|
+
assert Geocoder::IpAddress.new("::1").valid?
|
13
|
+
assert !Geocoder::IpAddress.new("232.65.123.94.43").valid?
|
14
|
+
assert !Geocoder::IpAddress.new("232.65.123").valid?
|
15
|
+
assert !Geocoder::IpAddress.new("::ffff:123.456.789").valid?
|
16
|
+
assert !Geocoder::IpAddress.new("Test\n232.65.123.94").valid?
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_loopback
|
20
|
+
assert Geocoder::IpAddress.new("0.0.0.0").loopback?
|
21
|
+
assert Geocoder::IpAddress.new("127.0.0.1").loopback?
|
22
|
+
assert Geocoder::IpAddress.new("::1").loopback?
|
23
|
+
assert !Geocoder::IpAddress.new("232.65.123.234").loopback?
|
24
|
+
assert !Geocoder::IpAddress.new("127 Main St.").loopback?
|
25
|
+
assert !Geocoder::IpAddress.new("John Doe\n127 Main St.\nAnywhere, USA").loopback?
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class LookupTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def test_responds_to_name_method
|
8
|
+
Geocoder::Lookup.all_services.each do |l|
|
9
|
+
lookup = Geocoder::Lookup.get(l)
|
10
|
+
assert lookup.respond_to?(:name),
|
11
|
+
"Lookup #{l} does not respond to #name method."
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_search_returns_empty_array_when_no_results
|
16
|
+
Geocoder::Lookup.all_services_except_test.each do |l|
|
17
|
+
lookup = Geocoder::Lookup.get(l)
|
18
|
+
set_api_key!(l)
|
19
|
+
assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")),
|
20
|
+
"Lookup #{l} does not return empty array when no results."
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_query_url_contains_values_in_params_hash
|
25
|
+
Geocoder::Lookup.all_services_except_test.each do |l|
|
26
|
+
next if [:freegeoip, :maxmind_local, :telize, :pointpin, :geoip2].include? l # does not use query string
|
27
|
+
set_api_key!(l)
|
28
|
+
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
|
29
|
+
"test", :params => {:one_in_the_hand => "two in the bush"}
|
30
|
+
))
|
31
|
+
# should be "+"s for all lookups except Yahoo
|
32
|
+
assert_match(/one_in_the_hand=two(%20|\+)in(%20|\+)the(%20|\+)bush/, url,
|
33
|
+
"Lookup #{l} does not appear to support arbitrary params in URL")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
{
|
38
|
+
:esri => :l,
|
39
|
+
:bing => :key,
|
40
|
+
:geocoder_ca => :auth,
|
41
|
+
:google => :language,
|
42
|
+
:google_premier => :language,
|
43
|
+
:mapquest => :key,
|
44
|
+
:maxmind => :l,
|
45
|
+
:nominatim => :"accept-language",
|
46
|
+
:yahoo => :locale,
|
47
|
+
:yandex => :plng
|
48
|
+
}.each do |l,p|
|
49
|
+
define_method "test_passing_param_to_#{l}_query_overrides_configuration_value" do
|
50
|
+
set_api_key!(l)
|
51
|
+
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
|
52
|
+
"test", :params => {p => "xxxx"}
|
53
|
+
))
|
54
|
+
assert_match(/#{p}=xxxx/, url,
|
55
|
+
"Param passed to #{l} lookup does not override configuration value")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
{
|
60
|
+
:google => :language,
|
61
|
+
:google_premier => :language,
|
62
|
+
:nominatim => :"accept-language",
|
63
|
+
:yahoo => :locale,
|
64
|
+
:yandex => :plng
|
65
|
+
}.each do |l,p|
|
66
|
+
define_method "test_passing_language_to_#{l}_query_overrides_configuration_value" do
|
67
|
+
set_api_key!(l)
|
68
|
+
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
|
69
|
+
"test", :language => 'xxxx'
|
70
|
+
))
|
71
|
+
assert_match(/#{p}=xxxx/, url,
|
72
|
+
"Param passed to #{l} lookup does not override configuration value")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_raises_exception_on_invalid_key
|
77
|
+
Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
|
78
|
+
#Geocoder::Lookup.all_services_except_test.each do |l|
|
79
|
+
[:bing, :yahoo, :yandex, :maxmind, :baidu, :baidu_ip].each do |l|
|
80
|
+
lookup = Geocoder::Lookup.get(l)
|
81
|
+
assert_raises Geocoder::InvalidApiKey do
|
82
|
+
lookup.send(:results, Geocoder::Query.new("invalid key"))
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_returns_empty_array_on_invalid_key
|
88
|
+
silence_warnings do
|
89
|
+
#Geocoder::Lookup.all_services_except_test.each do |l|
|
90
|
+
[:bing, :yahoo, :yandex, :maxmind, :baidu, :baidu_ip].each do |l|
|
91
|
+
Geocoder.configure(:lookup => l)
|
92
|
+
set_api_key!(l)
|
93
|
+
assert_equal [], Geocoder.search("invalid key")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_does_not_choke_on_nil_address
|
99
|
+
Geocoder::Lookup.all_services.each do |l|
|
100
|
+
Geocoder.configure(:lookup => l)
|
101
|
+
assert_nothing_raised { Place.new("Place", nil).geocode }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_hash_to_query
|
106
|
+
g = Geocoder::Lookup::Google.new
|
107
|
+
assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_baidu_api_key
|
111
|
+
Geocoder.configure(:api_key => "MY_KEY")
|
112
|
+
g = Geocoder::Lookup::BaiduIp.new
|
113
|
+
assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("232.65.123.94"))
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_baidu_ip_api_key
|
117
|
+
Geocoder.configure(:api_key => "MY_KEY")
|
118
|
+
g = Geocoder::Lookup::Baidu.new
|
119
|
+
assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_pointpin_api_key
|
123
|
+
Geocoder.configure(:api_key => "MY_KEY")
|
124
|
+
g = Geocoder::Lookup::Pointpin.new
|
125
|
+
assert_match "/MY_KEY/", g.query_url(Geocoder::Query.new("232.65.123.94"))
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_google_api_key
|
129
|
+
Geocoder.configure(:api_key => "MY_KEY")
|
130
|
+
g = Geocoder::Lookup::Google.new
|
131
|
+
assert_match "key=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_geocoder_ca_showpostal
|
135
|
+
Geocoder.configure(:api_key => "MY_KEY")
|
136
|
+
g = Geocoder::Lookup::GeocoderCa.new
|
137
|
+
assert_match "showpostal=1", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_raises_configuration_error_on_missing_key
|
141
|
+
[:bing, :baidu].each do |l|
|
142
|
+
assert_raises Geocoder::ConfigurationError do
|
143
|
+
Geocoder.configure(:lookup => l, :api_key => nil)
|
144
|
+
Geocoder.search("Madison Square Garden, New York, NY 10001, United States")
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_handle
|
150
|
+
assert_equal :google, Geocoder::Lookup::Google.new.handle
|
151
|
+
assert_equal :geocoder_ca, Geocoder::Lookup::GeocoderCa.new.handle
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class BingTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :bing)
|
9
|
+
set_api_key!(:bing)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_query_for_reverse_geocode
|
13
|
+
lookup = Geocoder::Lookup::Bing.new
|
14
|
+
url = lookup.query_url(Geocoder::Query.new([45.423733, -75.676333]))
|
15
|
+
assert_match(/Locations\/45.423733/, url)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_result_components
|
19
|
+
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
20
|
+
assert_equal "Madison Square Garden, NY", result.address
|
21
|
+
assert_equal "NY", result.state
|
22
|
+
assert_equal "New York", result.city
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_no_results
|
26
|
+
results = Geocoder.search("no results")
|
27
|
+
assert_equal 0, results.length
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_query_url_contains_region
|
31
|
+
lookup = Geocoder::Lookup::Bing.new
|
32
|
+
url = lookup.query_url(Geocoder::Query.new(
|
33
|
+
"manchester",
|
34
|
+
:region => "uk"
|
35
|
+
))
|
36
|
+
assert_match(/Locations\/uk\?q=manchester/, url)
|
37
|
+
assert_no_match(/query/, url)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_query_url_without_region
|
41
|
+
lookup = Geocoder::Lookup::Bing.new
|
42
|
+
url = lookup.query_url(Geocoder::Query.new(
|
43
|
+
"manchester"
|
44
|
+
))
|
45
|
+
assert_match(/Locations\?q=manchester/, url)
|
46
|
+
assert_no_match(/query/, url)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_query_url_contains_address_with_spaces
|
50
|
+
lookup = Geocoder::Lookup::Bing.new
|
51
|
+
url = lookup.query_url(Geocoder::Query.new(
|
52
|
+
"manchester, lancashire",
|
53
|
+
:region => "uk"
|
54
|
+
))
|
55
|
+
assert_match(/Locations\/uk\?q=manchester,%20lancashire/, url)
|
56
|
+
assert_no_match(/query/, url)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_query_url_contains_address_with_trailing_and_leading_spaces
|
60
|
+
lookup = Geocoder::Lookup::Bing.new
|
61
|
+
url = lookup.query_url(Geocoder::Query.new(
|
62
|
+
" manchester, lancashire ",
|
63
|
+
:region => "uk"
|
64
|
+
))
|
65
|
+
assert_match(/Locations\/uk\?q=manchester,%20lancashire/, url)
|
66
|
+
assert_no_match(/query/, url)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class DstkTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :dstk)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_dstk_result_components
|
12
|
+
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
13
|
+
assert_equal "Manhattan", result.address_components_of_type(:sublocality).first['long_name']
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_dstk_query_url
|
17
|
+
query = Geocoder::Query.new("Madison Square Garden, New York, NY")
|
18
|
+
assert_equal "http://www.datasciencetoolkit.org/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false", query.url
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_dstk_query_url_with_custom_host
|
22
|
+
Geocoder.configure(dstk: {host: 'NOT_AN_ACTUAL_HOST'})
|
23
|
+
query = Geocoder::Query.new("Madison Square Garden, New York, NY")
|
24
|
+
assert_equal "http://NOT_AN_ACTUAL_HOST/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false", query.url
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class EsriTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :esri)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_query_for_geocode
|
12
|
+
query = Geocoder::Query.new("Bluffton, SC")
|
13
|
+
lookup = Geocoder::Lookup.get(:esri)
|
14
|
+
res = lookup.query_url(query)
|
15
|
+
assert_equal "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&outFields=%2A&text=Bluffton%2C+SC",
|
16
|
+
res
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_query_for_reverse_geocode
|
20
|
+
query = Geocoder::Query.new([45.423733, -75.676333])
|
21
|
+
lookup = Geocoder::Lookup.get(:esri)
|
22
|
+
res = lookup.query_url(query)
|
23
|
+
assert_equal "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode?f=pjson&location=-75.676333%2C45.423733&outFields=%2A",
|
24
|
+
res
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_results_component
|
28
|
+
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
29
|
+
assert_equal "10001", result.postal_code
|
30
|
+
assert_equal "USA", result.country
|
31
|
+
assert_equal "Madison Square Garden", result.address
|
32
|
+
assert_equal "New York", result.city
|
33
|
+
assert_equal "New York", result.state
|
34
|
+
assert_equal(40.75004981300049, result.coordinates[0])
|
35
|
+
assert_equal(-73.99423889799965, result.coordinates[1])
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_results_component_when_reverse_geocoding
|
39
|
+
result = Geocoder.search([45.423733, -75.676333]).first
|
40
|
+
assert_equal "75007", result.postal_code
|
41
|
+
assert_equal "FRA", result.country
|
42
|
+
assert_equal "4 Avenue Gustave Eiffel", result.address
|
43
|
+
assert_equal "Paris", result.city
|
44
|
+
assert_equal "Île-de-France", result.state
|
45
|
+
assert_equal(48.858129997357558, result.coordinates[0])
|
46
|
+
assert_equal(2.2956200048981574, result.coordinates[1])
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class FreegeoipTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(ip_lookup: :freegeoip)
|
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::Freegeoip)
|
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_host_config
|
22
|
+
Geocoder.configure(freegeoip: {host: "local.com"})
|
23
|
+
lookup = Geocoder::Lookup::Freegeoip.new
|
24
|
+
query = Geocoder::Query.new("24.24.24.23")
|
25
|
+
assert_match %r(http://local\.com), lookup.query_url(query)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class GeocoderCaTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :geocoder_ca)
|
9
|
+
set_api_key!(:geocoder_ca)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_result_components
|
13
|
+
result = Geocoder.search([45.423733, -75.676333]).first
|
14
|
+
assert_equal "CA", result.country_code
|
15
|
+
assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..", "..")
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class GeocodioTest < GeocoderTestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Geocoder.configure(lookup: :geocodio)
|
9
|
+
set_api_key!(:geocodio)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_result_components
|
13
|
+
result = Geocoder.search("1101 Pennsylvania Ave NW, Washington DC").first
|
14
|
+
assert_equal 1.0, result.accuracy
|
15
|
+
assert_equal "1101", result.number
|
16
|
+
assert_equal "Ave", result.suffix
|
17
|
+
assert_equal "DC", result.state
|
18
|
+
assert_equal "20004", result.zip
|
19
|
+
assert_equal "NW", result.postdirectional
|
20
|
+
assert_equal "Washington", result.city
|
21
|
+
assert_equal "1101 Pennsylvania Ave NW, Washington DC, 20004", result.formatted_address
|
22
|
+
assert_equal({ "lat" => "38.895019", "lng" => "-77.028095" }, result.location)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_no_results
|
26
|
+
results = Geocoder.search("no results")
|
27
|
+
assert_equal 0, results.length
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_geocodio_reverse_url
|
31
|
+
query = Geocoder::Query.new([45.423733, -75.676333])
|
32
|
+
assert_match /reverse/, query.url
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_raises_invalid_request_exception
|
36
|
+
Geocoder.configure Geocoder.configure(:always_raise => [Geocoder::InvalidRequest])
|
37
|
+
assert_raises Geocoder::InvalidRequest do
|
38
|
+
Geocoder.search("invalid")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_raises_api_key_exception
|
43
|
+
Geocoder.configure Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
|
44
|
+
assert_raises Geocoder::InvalidApiKey do
|
45
|
+
Geocoder.search("bad api key")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_raises_over_limit_exception
|
50
|
+
Geocoder.configure Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
|
51
|
+
assert_raises Geocoder::OverQueryLimitError do
|
52
|
+
Geocoder.search("over query limit")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|