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
@@ -1,12 +1,13 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
|
-
class GeocoderTest <
|
5
|
+
class GeocoderTest < GeocoderTestCase
|
5
6
|
|
6
7
|
def test_distance_to_returns_float
|
7
|
-
v =
|
8
|
+
v = Place.new(*geocoded_object_params(:msg))
|
8
9
|
v.latitude, v.longitude = [40.750354, -73.993371]
|
9
|
-
assert (
|
10
|
+
assert (v.distance_to([30, -94])).is_a?(Float)
|
10
11
|
end
|
11
12
|
|
12
13
|
def test_coordinates_method_returns_array
|
@@ -26,27 +27,27 @@ class GeocoderTest < Test::Unit::TestCase
|
|
26
27
|
end
|
27
28
|
|
28
29
|
def test_geocode_assigns_and_returns_coordinates
|
29
|
-
v =
|
30
|
+
v = Place.new(*geocoded_object_params(:msg))
|
30
31
|
coords = [40.750354, -73.993371]
|
31
32
|
assert_equal coords, v.geocode
|
32
33
|
assert_equal coords, [v.latitude, v.longitude]
|
33
34
|
end
|
34
35
|
|
35
36
|
def test_geocode_block_executed_when_no_results
|
36
|
-
v =
|
37
|
+
v = PlaceWithCustomResultsHandling.new("Nowhere", "no results")
|
37
38
|
v.geocode
|
38
39
|
assert_equal "NOT FOUND", v.coords_string
|
39
40
|
end
|
40
41
|
|
41
42
|
def test_reverse_geocode_assigns_and_returns_address
|
42
|
-
v =
|
43
|
+
v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
|
43
44
|
address = "4 Penn Plaza, New York, NY 10001, USA"
|
44
45
|
assert_equal address, v.reverse_geocode
|
45
46
|
assert_equal address, v.address
|
46
47
|
end
|
47
48
|
|
48
49
|
def test_forward_and_reverse_geocoding_on_same_model_works
|
49
|
-
g =
|
50
|
+
g = PlaceWithForwardAndReverseGeocoding.new("Exxon")
|
50
51
|
g.address = "404 New St, Middletown, CT"
|
51
52
|
g.geocode
|
52
53
|
assert_not_nil g.lat
|
@@ -56,4 +57,22 @@ class GeocoderTest < Test::Unit::TestCase
|
|
56
57
|
g.reverse_geocode
|
57
58
|
assert_not_nil g.location
|
58
59
|
end
|
60
|
+
|
61
|
+
def test_geocode_with_custom_lookup_param
|
62
|
+
v = PlaceWithCustomLookup.new(*geocoded_object_params(:msg))
|
63
|
+
v.geocode
|
64
|
+
assert_equal Geocoder::Result::Nominatim, v.result_class
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_geocode_with_custom_lookup_proc_param
|
68
|
+
v = PlaceWithCustomLookupProc.new(*geocoded_object_params(:msg))
|
69
|
+
v.geocode
|
70
|
+
assert_equal Geocoder::Result::Nominatim, v.result_class
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_reverse_geocode_with_custom_lookup_param
|
74
|
+
v = PlaceReverseGeocodedWithCustomLookup.new(*reverse_geocoded_object_params(:msg))
|
75
|
+
v.reverse_geocode
|
76
|
+
assert_equal Geocoder::Result::Nominatim, v.result_class
|
77
|
+
end
|
59
78
|
end
|
@@ -1,16 +1,17 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), "..")
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
|
-
class HttpsTest <
|
5
|
+
class HttpsTest < GeocoderTestCase
|
5
6
|
|
6
7
|
def test_uses_https_for_secure_query
|
7
8
|
Geocoder.configure(:use_https => true)
|
8
9
|
g = Geocoder::Lookup::Google.new
|
9
|
-
assert_match
|
10
|
+
assert_match(/^https:/, g.query_url(Geocoder::Query.new("test")))
|
10
11
|
end
|
11
12
|
|
12
13
|
def test_uses_http_by_default
|
13
14
|
g = Geocoder::Lookup::Google.new
|
14
|
-
assert_match
|
15
|
+
assert_match(/^http:/, g.query_url(Geocoder::Query.new("test")))
|
15
16
|
end
|
16
17
|
end
|
@@ -0,0 +1,24 @@
|
|
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("232.65.123.94.43").valid?
|
12
|
+
assert !Geocoder::IpAddress.new("232.65.123").valid?
|
13
|
+
assert !Geocoder::IpAddress.new("::ffff:123.456.789").valid?
|
14
|
+
assert !Geocoder::IpAddress.new("Test\n232.65.123.94").valid?
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_loopback
|
18
|
+
assert Geocoder::IpAddress.new("0.0.0.0").loopback?
|
19
|
+
assert Geocoder::IpAddress.new("127.0.0.1").loopback?
|
20
|
+
assert !Geocoder::IpAddress.new("232.65.123.234").loopback?
|
21
|
+
assert !Geocoder::IpAddress.new("127 Main St.").loopback?
|
22
|
+
assert !Geocoder::IpAddress.new("John Doe\n127 Main St.\nAnywhere, USA").loopback?
|
23
|
+
end
|
24
|
+
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 LookupTest <
|
5
|
+
class LookupTest < GeocoderTestCase
|
5
6
|
|
6
7
|
def test_responds_to_name_method
|
7
8
|
Geocoder::Lookup.all_services.each do |l|
|
@@ -22,14 +23,14 @@ class LookupTest < Test::Unit::TestCase
|
|
22
23
|
|
23
24
|
def test_query_url_contains_values_in_params_hash
|
24
25
|
Geocoder::Lookup.all_services_except_test.each do |l|
|
25
|
-
next if l == :freegeoip # does not use query string
|
26
|
+
next if l == :freegeoip || l == :maxmind_local # does not use query string
|
26
27
|
set_api_key!(l)
|
27
28
|
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
|
28
29
|
"test", :params => {:one_in_the_hand => "two in the bush"}
|
29
30
|
))
|
30
31
|
# should be "+"s for all lookups except Yahoo
|
31
|
-
assert_match
|
32
|
-
"Lookup #{l} does not appear to support arbitrary params in URL"
|
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")
|
33
34
|
end
|
34
35
|
end
|
35
36
|
|
@@ -50,15 +51,15 @@ class LookupTest < Test::Unit::TestCase
|
|
50
51
|
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
|
51
52
|
"test", :params => {p => "xxxx"}
|
52
53
|
))
|
53
|
-
assert_match
|
54
|
-
"Param passed to #{l} lookup does not override configuration value"
|
54
|
+
assert_match(/#{p}=xxxx/, url,
|
55
|
+
"Param passed to #{l} lookup does not override configuration value")
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
58
59
|
def test_raises_exception_on_invalid_key
|
59
60
|
Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
|
60
61
|
#Geocoder::Lookup.all_services_except_test.each do |l|
|
61
|
-
[:bing, :yahoo, :yandex, :maxmind].each do |l|
|
62
|
+
[:bing, :yahoo, :yandex, :maxmind, :baidu, :baidu_ip].each do |l|
|
62
63
|
lookup = Geocoder::Lookup.get(l)
|
63
64
|
assert_raises Geocoder::InvalidApiKey do
|
64
65
|
lookup.send(:results, Geocoder::Query.new("invalid key"))
|
@@ -67,22 +68,20 @@ class LookupTest < Test::Unit::TestCase
|
|
67
68
|
end
|
68
69
|
|
69
70
|
def test_returns_empty_array_on_invalid_key
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
71
|
+
silence_warnings do
|
72
|
+
#Geocoder::Lookup.all_services_except_test.each do |l|
|
73
|
+
[:bing, :yahoo, :yandex, :maxmind, :cloudmade, :baidu, :baidu_ip].each do |l|
|
74
|
+
Geocoder.configure(:lookup => l)
|
75
|
+
set_api_key!(l)
|
76
|
+
assert_equal [], Geocoder.search("invalid key")
|
77
|
+
end
|
77
78
|
end
|
78
|
-
ensure
|
79
|
-
$VERBOSE = orig
|
80
79
|
end
|
81
80
|
|
82
81
|
def test_does_not_choke_on_nil_address
|
83
82
|
Geocoder::Lookup.all_services.each do |l|
|
84
83
|
Geocoder.configure(:lookup => l)
|
85
|
-
assert_nothing_raised {
|
84
|
+
assert_nothing_raised { Place.new("Place", nil).geocode }
|
86
85
|
end
|
87
86
|
end
|
88
87
|
|
@@ -91,6 +90,18 @@ class LookupTest < Test::Unit::TestCase
|
|
91
90
|
assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
|
92
91
|
end
|
93
92
|
|
93
|
+
def test_baidu_api_key
|
94
|
+
Geocoder.configure(:api_key => "MY_KEY")
|
95
|
+
g = Geocoder::Lookup::BaiduIp.new
|
96
|
+
assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("232.65.123.94"))
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_baidu_ip_api_key
|
100
|
+
Geocoder.configure(:api_key => "MY_KEY")
|
101
|
+
g = Geocoder::Lookup::Baidu.new
|
102
|
+
assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
|
103
|
+
end
|
104
|
+
|
94
105
|
def test_google_api_key
|
95
106
|
Geocoder.configure(:api_key => "MY_KEY")
|
96
107
|
g = Geocoder::Lookup::Google.new
|
@@ -104,9 +115,11 @@ class LookupTest < Test::Unit::TestCase
|
|
104
115
|
end
|
105
116
|
|
106
117
|
def test_raises_configuration_error_on_missing_key
|
107
|
-
|
108
|
-
Geocoder
|
109
|
-
|
118
|
+
[:bing, :baidu].each do |l|
|
119
|
+
assert_raises Geocoder::ConfigurationError do
|
120
|
+
Geocoder.configure(:lookup => l, :api_key => nil)
|
121
|
+
Geocoder.search("Madison Square Garden, New York, NY 10001, United States")
|
122
|
+
end
|
110
123
|
end
|
111
124
|
end
|
112
125
|
|
@@ -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\/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\/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\/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\/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
|