geocoder-sgonyea 1.1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +5 -0
  2. data/.travis.yml +23 -0
  3. data/CHANGELOG.md +298 -0
  4. data/LICENSE +20 -0
  5. data/README.md +656 -0
  6. data/Rakefile +25 -0
  7. data/bin/geocode +5 -0
  8. data/examples/autoexpire_cache.rb +28 -0
  9. data/gemfiles/Gemfile.mongoid-2.4.x +15 -0
  10. data/lib/generators/geocoder/config/config_generator.rb +14 -0
  11. data/lib/generators/geocoder/config/templates/initializer.rb +21 -0
  12. data/lib/geocoder.rb +55 -0
  13. data/lib/geocoder/cache.rb +85 -0
  14. data/lib/geocoder/calculations.rb +319 -0
  15. data/lib/geocoder/cli.rb +114 -0
  16. data/lib/geocoder/configuration.rb +130 -0
  17. data/lib/geocoder/configuration_hash.rb +11 -0
  18. data/lib/geocoder/exceptions.rb +21 -0
  19. data/lib/geocoder/lookup.rb +82 -0
  20. data/lib/geocoder/lookups/base.rb +250 -0
  21. data/lib/geocoder/lookups/bing.rb +47 -0
  22. data/lib/geocoder/lookups/freegeoip.rb +47 -0
  23. data/lib/geocoder/lookups/geocoder_ca.rb +54 -0
  24. data/lib/geocoder/lookups/google.rb +62 -0
  25. data/lib/geocoder/lookups/google_premier.rb +47 -0
  26. data/lib/geocoder/lookups/mapquest.rb +43 -0
  27. data/lib/geocoder/lookups/maxmind.rb +88 -0
  28. data/lib/geocoder/lookups/nominatim.rb +45 -0
  29. data/lib/geocoder/lookups/ovi.rb +52 -0
  30. data/lib/geocoder/lookups/test.rb +38 -0
  31. data/lib/geocoder/lookups/yahoo.rb +84 -0
  32. data/lib/geocoder/lookups/yandex.rb +54 -0
  33. data/lib/geocoder/models/active_record.rb +46 -0
  34. data/lib/geocoder/models/base.rb +42 -0
  35. data/lib/geocoder/models/mongo_base.rb +60 -0
  36. data/lib/geocoder/models/mongo_mapper.rb +26 -0
  37. data/lib/geocoder/models/mongoid.rb +32 -0
  38. data/lib/geocoder/query.rb +103 -0
  39. data/lib/geocoder/railtie.rb +26 -0
  40. data/lib/geocoder/request.rb +23 -0
  41. data/lib/geocoder/results/base.rb +67 -0
  42. data/lib/geocoder/results/bing.rb +48 -0
  43. data/lib/geocoder/results/freegeoip.rb +45 -0
  44. data/lib/geocoder/results/geocoder_ca.rb +60 -0
  45. data/lib/geocoder/results/google.rb +106 -0
  46. data/lib/geocoder/results/google_premier.rb +6 -0
  47. data/lib/geocoder/results/mapquest.rb +51 -0
  48. data/lib/geocoder/results/maxmind.rb +136 -0
  49. data/lib/geocoder/results/nominatim.rb +94 -0
  50. data/lib/geocoder/results/ovi.rb +62 -0
  51. data/lib/geocoder/results/test.rb +16 -0
  52. data/lib/geocoder/results/yahoo.rb +55 -0
  53. data/lib/geocoder/results/yandex.rb +80 -0
  54. data/lib/geocoder/sql.rb +106 -0
  55. data/lib/geocoder/stores/active_record.rb +259 -0
  56. data/lib/geocoder/stores/base.rb +120 -0
  57. data/lib/geocoder/stores/mongo_base.rb +85 -0
  58. data/lib/geocoder/stores/mongo_mapper.rb +13 -0
  59. data/lib/geocoder/stores/mongoid.rb +13 -0
  60. data/lib/geocoder/version.rb +3 -0
  61. data/lib/hash_recursive_merge.rb +74 -0
  62. data/lib/oauth_util.rb +112 -0
  63. data/lib/tasks/geocoder.rake +25 -0
  64. data/test/active_record_test.rb +15 -0
  65. data/test/cache_test.rb +19 -0
  66. data/test/calculations_test.rb +195 -0
  67. data/test/configuration_test.rb +78 -0
  68. data/test/custom_block_test.rb +32 -0
  69. data/test/error_handling_test.rb +43 -0
  70. data/test/fixtures/bing_invalid_key +1 -0
  71. data/test/fixtures/bing_madison_square_garden +40 -0
  72. data/test/fixtures/bing_no_results +16 -0
  73. data/test/fixtures/bing_reverse +42 -0
  74. data/test/fixtures/freegeoip_74_200_247_59 +12 -0
  75. data/test/fixtures/freegeoip_no_results +1 -0
  76. data/test/fixtures/geocoder_ca_madison_square_garden +1 -0
  77. data/test/fixtures/geocoder_ca_no_results +1 -0
  78. data/test/fixtures/geocoder_ca_reverse +34 -0
  79. data/test/fixtures/google_garbage +456 -0
  80. data/test/fixtures/google_madison_square_garden +57 -0
  81. data/test/fixtures/google_no_city_data +44 -0
  82. data/test/fixtures/google_no_locality +51 -0
  83. data/test/fixtures/google_no_results +4 -0
  84. data/test/fixtures/mapquest_madison_square_garden +52 -0
  85. data/test/fixtures/mapquest_no_results +7 -0
  86. data/test/fixtures/maxmind_24_24_24_21 +1 -0
  87. data/test/fixtures/maxmind_24_24_24_22 +1 -0
  88. data/test/fixtures/maxmind_24_24_24_23 +1 -0
  89. data/test/fixtures/maxmind_24_24_24_24 +1 -0
  90. data/test/fixtures/maxmind_74_200_247_59 +1 -0
  91. data/test/fixtures/maxmind_invalid_key +1 -0
  92. data/test/fixtures/maxmind_no_results +1 -0
  93. data/test/fixtures/nominatim_madison_square_garden +150 -0
  94. data/test/fixtures/nominatim_no_results +1 -0
  95. data/test/fixtures/ovi_madison_square_garden +72 -0
  96. data/test/fixtures/ovi_no_results +8 -0
  97. data/test/fixtures/yahoo_error +1 -0
  98. data/test/fixtures/yahoo_invalid_key +2 -0
  99. data/test/fixtures/yahoo_madison_square_garden +52 -0
  100. data/test/fixtures/yahoo_no_results +10 -0
  101. data/test/fixtures/yahoo_over_limit +2 -0
  102. data/test/fixtures/yandex_invalid_key +1 -0
  103. data/test/fixtures/yandex_kremlin +48 -0
  104. data/test/fixtures/yandex_no_city_and_town +112 -0
  105. data/test/fixtures/yandex_no_results +16 -0
  106. data/test/geocoder_test.rb +59 -0
  107. data/test/https_test.rb +16 -0
  108. data/test/integration/smoke_test.rb +26 -0
  109. data/test/lookup_test.rb +116 -0
  110. data/test/method_aliases_test.rb +25 -0
  111. data/test/mongoid_test.rb +39 -0
  112. data/test/mongoid_test_helper.rb +43 -0
  113. data/test/near_test.rb +43 -0
  114. data/test/oauth_util_test.rb +30 -0
  115. data/test/proxy_test.rb +23 -0
  116. data/test/query_test.rb +51 -0
  117. data/test/request_test.rb +29 -0
  118. data/test/result_test.rb +42 -0
  119. data/test/services_test.rb +277 -0
  120. data/test/test_helper.rb +279 -0
  121. data/test/test_mode_test.rb +50 -0
  122. metadata +170 -0
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class MethodAliasesTest < Test::Unit::TestCase
5
+
6
+ def test_distance_from_is_alias_for_distance_to
7
+ v = Venue.new(*venue_params(:msg))
8
+ v.latitude, v.longitude = [40.750354, -73.993371]
9
+ assert_equal v.distance_from([30, -94]), v.distance_to([30, -94])
10
+ end
11
+
12
+ def test_fetch_coordinates_is_alias_for_geocode
13
+ v = Venue.new(*venue_params(:msg))
14
+ coords = [40.750354, -73.993371]
15
+ assert_equal coords, v.fetch_coordinates
16
+ assert_equal coords, [v.latitude, v.longitude]
17
+ end
18
+
19
+ def test_fetch_address_is_alias_for_reverse_geocode
20
+ v = Landmark.new(*landmark_params(:msg))
21
+ address = "4 Penn Plaza, New York, NY 10001, USA"
22
+ assert_equal address, v.fetch_address
23
+ assert_equal address, v.address
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ require 'mongoid_test_helper'
3
+
4
+ class MongoidTest < Test::Unit::TestCase
5
+ def test_geocoded_check
6
+ p = Place.new(*venue_params(:msg))
7
+ p.location = [40.750354, -73.993371]
8
+ assert p.geocoded?
9
+ end
10
+
11
+ def test_distance_to_returns_float
12
+ p = Place.new(*venue_params(:msg))
13
+ p.location = [40.750354, -73.993371]
14
+ assert p.distance_to([30, -94]).is_a?(Float)
15
+ end
16
+
17
+ def test_custom_coordinate_field_near_scope
18
+ location = [40.750354, -73.993371]
19
+ p = Place.near(location)
20
+ key = Mongoid::VERSION >= "3" ? "location" : :location
21
+ assert_equal p.selector[key]['$nearSphere'], location.reverse
22
+ end
23
+
24
+ def test_model_configuration
25
+ p = Place.new(*venue_params(:msg))
26
+ p.location = [0, 0]
27
+
28
+ Place.geocoded_by :address, :coordinates => :location, :units => :km
29
+ assert_equal 111, p.distance_to([0,1]).round
30
+
31
+ Place.geocoded_by :address, :coordinates => :location, :units => :mi
32
+ assert_equal 69, p.distance_to([0,1]).round
33
+ end
34
+
35
+ def test_index_is_skipped_if_skip_option_flag
36
+ result = PlaceWithoutIndex.index_options.keys.flatten[0] == :coordinates
37
+ assert !result
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'test_helper'
4
+ require 'mongoid'
5
+ require 'geocoder/models/mongoid'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+
10
+ if (::Mongoid::VERSION >= "3")
11
+ Mongoid.logger = Logger.new($stderr, :debug)
12
+ else
13
+ Mongoid.configure do |config|
14
+ config.logger = Logger.new($stderr, :debug)
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Geocoded model.
20
+ #
21
+ class Place
22
+ include Mongoid::Document
23
+ include Geocoder::Model::Mongoid
24
+
25
+ geocoded_by :address, :coordinates => :location
26
+ field :name
27
+ field :address
28
+ field :location, :type => Array
29
+
30
+ def initialize(name, address)
31
+ super()
32
+ write_attribute :name, name
33
+ write_attribute :address, address
34
+ end
35
+ end
36
+
37
+ class PlaceWithoutIndex
38
+ include Mongoid::Document
39
+ include Geocoder::Model::Mongoid
40
+
41
+ field :location, :type => Array
42
+ geocoded_by :location, :skip_index => true
43
+ end
data/test/near_test.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class NearTest < Test::Unit::TestCase
4
+
5
+ def test_near_scope_options_without_sqlite_includes_bounding_box_condition
6
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5)
7
+
8
+ 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 /,
9
+ result[:conditions][0]
10
+ end
11
+
12
+ def test_near_scope_options_with_defaults
13
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5)
14
+
15
+ assert_match /AS distance/, result[:select]
16
+ assert_match /AS bearing/, result[:select]
17
+ assert_no_consecutive_comma(result[:select])
18
+ end
19
+
20
+ def test_near_scope_options_with_no_distance
21
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5, :select_distance => false)
22
+
23
+ assert_no_match /AS distance/, result[:select]
24
+ assert_match /AS bearing/, result[:select]
25
+ assert_no_match /distance/, result[:condition]
26
+ assert_no_match /distance/, result[:order]
27
+ assert_no_consecutive_comma(result[:select])
28
+ end
29
+
30
+ def test_near_scope_options_with_no_bearing
31
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5, :select_bearing => false)
32
+
33
+ assert_match /AS distance/, result[:select]
34
+ assert_no_match /AS bearing/, result[:select]
35
+ assert_no_consecutive_comma(result[:select])
36
+ end
37
+
38
+ private
39
+
40
+ def assert_no_consecutive_comma(string)
41
+ assert_no_match /, *,/, string, "two consecutive commas"
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+ require 'cgi'
4
+ require 'uri'
5
+
6
+ class OauthUtilTest < Test::Unit::TestCase
7
+ def test_query_string_escapes_single_quote
8
+ base_url = "http://example.com?location=d'iberville"
9
+
10
+ o = OauthUtil.new
11
+ o.consumer_key = 'consumer_key'
12
+ o.consumer_secret = 'consumer_secret'
13
+
14
+ query_string = o.sign(URI.parse(base_url)).query_string
15
+
16
+ assert_match "location=d%27iberville", query_string
17
+ end
18
+
19
+ def test_query_string_sorts_url_keys
20
+ base_url = "http://example.com?a_param=a&z_param=b&b_param=c&n_param=d"
21
+
22
+ o = OauthUtil.new
23
+ o.consumer_key = 'consumer_key'
24
+ o.consumer_secret = 'consumer_secret'
25
+
26
+ query_string = o.sign(URI.parse(base_url)).query_string
27
+
28
+ assert_match /.*a_param=.*b_param=.*n_param=.*z_param=.*/, query_string
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ProxyTest < Test::Unit::TestCase
5
+
6
+ def test_uses_proxy_when_specified
7
+ Geocoder.configure(:http_proxy => 'localhost')
8
+ lookup = Geocoder::Lookup::Google.new
9
+ assert lookup.send(:http_client).proxy_class?
10
+ end
11
+
12
+ def test_doesnt_use_proxy_when_not_specified
13
+ lookup = Geocoder::Lookup::Google.new
14
+ assert !lookup.send(:http_client).proxy_class?
15
+ end
16
+
17
+ def test_exception_raised_on_bad_proxy_url
18
+ Geocoder.configure(:http_proxy => ' \\_O< Quack Quack')
19
+ assert_raise Geocoder::ConfigurationError do
20
+ Geocoder::Lookup::Google.new.send(:http_client)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class QueryTest < Test::Unit::TestCase
5
+
6
+ def test_ip_address_detection
7
+ assert Geocoder::Query.new("232.65.123.94").ip_address?
8
+ assert Geocoder::Query.new("666.65.123.94").ip_address? # technically invalid
9
+ assert Geocoder::Query.new("::ffff:12.34.56.78").ip_address?
10
+ assert !Geocoder::Query.new("232.65.123.94.43").ip_address?
11
+ assert !Geocoder::Query.new("232.65.123").ip_address?
12
+ assert !Geocoder::Query.new("::ffff:123.456.789").ip_address?
13
+ assert !Geocoder::Query.new("Test\n232.65.123.94").ip_address?
14
+ end
15
+
16
+ def test_blank_query_detection
17
+ assert Geocoder::Query.new(nil).blank?
18
+ assert Geocoder::Query.new("").blank?
19
+ assert Geocoder::Query.new("\t ").blank?
20
+ assert !Geocoder::Query.new("a").blank?
21
+ assert !Geocoder::Query.new("Москва").blank? # no ASCII characters
22
+ assert !Geocoder::Query.new("\na").blank?
23
+
24
+ assert Geocoder::Query.new(nil, :params => {}).blank?
25
+ assert !Geocoder::Query.new(nil, :params => {:woeid => 1234567}).blank?
26
+ end
27
+
28
+ def test_blank_query_detection_for_coordinates
29
+ assert Geocoder::Query.new([nil,nil]).blank?
30
+ assert Geocoder::Query.new([87,nil]).blank?
31
+ end
32
+
33
+ def test_coordinates_detection
34
+ assert Geocoder::Query.new("51.178844,5").coordinates?
35
+ assert Geocoder::Query.new("51.178844, -1.826189").coordinates?
36
+ assert !Geocoder::Query.new("232.65.123").coordinates?
37
+ assert !Geocoder::Query.new("Test\n51.178844, -1.826189").coordinates?
38
+ end
39
+
40
+ def test_loopback_ip_address
41
+ assert Geocoder::Query.new("0.0.0.0").loopback_ip_address?
42
+ assert Geocoder::Query.new("127.0.0.1").loopback_ip_address?
43
+ assert !Geocoder::Query.new("232.65.123.234").loopback_ip_address?
44
+ assert !Geocoder::Query.new("127 Main St.").loopback_ip_address?
45
+ assert !Geocoder::Query.new("John Doe\n127 Main St.\nAnywhere, USA").loopback_ip_address?
46
+ end
47
+
48
+ def test_lookup
49
+ assert_equal Geocoder::Lookup::Bing, Geocoder::Query.new("4227 Main St.", :lookup => :bing).lookup.class
50
+ end
51
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class RequestTest < Test::Unit::TestCase
5
+ class MockRequest
6
+ include Geocoder::Request
7
+ attr_accessor :env, :ip
8
+ def initialize(env={}, ip="")
9
+ @env = env
10
+ @ip = ip
11
+ end
12
+ end
13
+ def test_http_x_real_ip
14
+ req = MockRequest.new({"HTTP_X_REAL_IP" => "74.200.247.59"})
15
+ assert req.location.is_a?(Geocoder::Result::Freegeoip)
16
+ end
17
+ def test_http_x_forwarded_for_without_proxy
18
+ req = MockRequest.new({"HTTP_X_FORWARDED_FOR" => "74.200.247.59"})
19
+ assert req.location.is_a?(Geocoder::Result::Freegeoip)
20
+ end
21
+ def test_http_x_forwarded_for_with_proxy
22
+ req = MockRequest.new({"HTTP_X_FORWARDED_FOR" => "74.200.247.59, 74.200.247.59"})
23
+ assert req.location.is_a?(Geocoder::Result::Freegeoip)
24
+ end
25
+ def test_with_request_ip
26
+ req = MockRequest.new({}, "74.200.247.59")
27
+ assert req.location.is_a?(Geocoder::Result::Freegeoip)
28
+ end
29
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ResultTest < Test::Unit::TestCase
5
+
6
+ def test_result_has_required_attributes
7
+ Geocoder::Lookup.all_services_except_test.each do |l|
8
+ Geocoder.configure(:lookup => l)
9
+ set_api_key!(l)
10
+ result = Geocoder.search([45.423733, -75.676333]).first
11
+ assert_result_has_required_attributes(result)
12
+ end
13
+ end
14
+
15
+ def test_yandex_result_without_city_does_not_raise_exception
16
+ assert_nothing_raised do
17
+ Geocoder.configure(:lookup => :yandex)
18
+ set_api_key!(:yandex)
19
+ result = Geocoder.search("no city and town").first
20
+ assert_equal "", result.city
21
+ end
22
+ end
23
+
24
+
25
+ private # ------------------------------------------------------------------
26
+
27
+ def assert_result_has_required_attributes(result)
28
+ m = "Lookup #{Geocoder.config.lookup} does not support %s attribute."
29
+ assert result.coordinates.is_a?(Array), m % "coordinates"
30
+ assert result.latitude.is_a?(Float), m % "latitude"
31
+ assert result.longitude.is_a?(Float), m % "longitude"
32
+ assert result.city.is_a?(String), m % "city"
33
+ assert result.state.is_a?(String), m % "state"
34
+ assert result.state_code.is_a?(String), m % "state_code"
35
+ assert result.province.is_a?(String), m % "province"
36
+ assert result.province_code.is_a?(String), m % "province_code"
37
+ assert result.postal_code.is_a?(String), m % "postal_code"
38
+ assert result.country.is_a?(String), m % "country"
39
+ assert result.country_code.is_a?(String), m % "country_code"
40
+ assert_not_nil result.address, m % "address"
41
+ end
42
+ end
@@ -0,0 +1,277 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ServicesTest < Test::Unit::TestCase
5
+
6
+ # --- Google ---
7
+
8
+ def test_google_result_components
9
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
10
+ assert_equal "Manhattan",
11
+ result.address_components_of_type(:sublocality).first['long_name']
12
+ end
13
+
14
+ def test_google_result_components_contains_route
15
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
16
+ assert_equal "Penn Plaza",
17
+ result.address_components_of_type(:route).first['long_name']
18
+ end
19
+
20
+ def test_google_result_components_contains_street_number
21
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
22
+ assert_equal "4",
23
+ result.address_components_of_type(:street_number).first['long_name']
24
+ end
25
+
26
+ def test_google_returns_city_when_no_locality_in_result
27
+ result = Geocoder.search("no locality").first
28
+ assert_equal "Haram", result.city
29
+ end
30
+
31
+ def test_google_city_results_returns_nil_if_no_matching_component_types
32
+ result = Geocoder.search("no city data").first
33
+ assert_equal nil, result.city
34
+ end
35
+
36
+ def test_google_street_address_returns_formatted_street_address
37
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
38
+ assert_equal "4 Penn Plaza", result.street_address
39
+ end
40
+
41
+ def test_google_precision
42
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
43
+ assert_equal "ROOFTOP",
44
+ result.precision
45
+ end
46
+
47
+ def test_google_query_url_contains_bounds
48
+ lookup = Geocoder::Lookup::Google.new
49
+ url = lookup.query_url(Geocoder::Query.new(
50
+ "Some Intersection",
51
+ :bounds => [[40.0, -120.0], [39.0, -121.0]]
52
+ ))
53
+ assert_match /bounds=40.0+%2C-120.0+%7C39.0+%2C-121.0+/, url
54
+ end
55
+
56
+ def test_google_query_url_contains_region
57
+ lookup = Geocoder::Lookup::Google.new
58
+ url = lookup.query_url(Geocoder::Query.new(
59
+ "Some Intersection",
60
+ :region => "gb"
61
+ ))
62
+ assert_match /region=gb/, url
63
+ end
64
+
65
+ def test_google_query_url_contains_components_when_given_as_string
66
+ lookup = Geocoder::Lookup::Google.new
67
+ url = lookup.query_url(Geocoder::Query.new(
68
+ "Some Intersection",
69
+ :components => "locality:ES"
70
+ ))
71
+ formatted = "components=" + CGI.escape("locality:ES")
72
+ assert url.include?(formatted), "Expected #{formatted} to be included in #{url}"
73
+ end
74
+
75
+ def test_google_query_url_contains_components_when_given_as_array
76
+ lookup = Geocoder::Lookup::Google.new
77
+ url = lookup.query_url(Geocoder::Query.new(
78
+ "Some Intersection",
79
+ :components => ["country:ES", "locality:ES"]
80
+ ))
81
+ formatted = "components=" + CGI.escape("country:ES|locality:ES")
82
+ assert url.include?(formatted), "Expected #{formatted} to be included in #{url}"
83
+ end
84
+
85
+ # --- Google Premier ---
86
+
87
+ def test_google_premier_result_components
88
+ Geocoder.configure(:lookup => :google_premier)
89
+ set_api_key!(:google_premier)
90
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
91
+ assert_equal "Manhattan",
92
+ result.address_components_of_type(:sublocality).first['long_name']
93
+ end
94
+
95
+ def test_google_premier_query_url
96
+ Geocoder.configure(:api_key => ["deadbeef", "gme-test", "test-dev"])
97
+ assert_equal "http://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&channel=test-dev&client=gme-test&language=en&sensor=false&signature=doJvJqX7YJzgV9rJ0DnVkTGZqTg=",
98
+ Geocoder::Lookup::GooglePremier.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY"))
99
+ end
100
+
101
+
102
+ # --- Yahoo ---
103
+
104
+ def test_yahoo_no_results
105
+ Geocoder.configure(:lookup => :yahoo)
106
+ set_api_key!(:yahoo)
107
+ assert_equal [], Geocoder.search("no results")
108
+ end
109
+
110
+ def test_yahoo_error
111
+ Geocoder.configure(:lookup => :yahoo)
112
+ set_api_key!(:yahoo)
113
+ # keep test output clean: suppress timeout warning
114
+ orig = $VERBOSE; $VERBOSE = nil
115
+ assert_equal [], Geocoder.search("error")
116
+ ensure
117
+ $VERBOSE = orig
118
+ end
119
+
120
+ def test_yahoo_result_components
121
+ Geocoder.configure(:lookup => :yahoo)
122
+ set_api_key!(:yahoo)
123
+ result = Geocoder.search("madison square garden").first
124
+ assert_equal "10001", result.postal_code
125
+ end
126
+
127
+ def test_yahoo_address_formatting
128
+ Geocoder.configure(:lookup => :yahoo)
129
+ set_api_key!(:yahoo)
130
+ result = Geocoder.search("madison square garden").first
131
+ assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address
132
+ end
133
+
134
+ def test_yahoo_raises_exception_when_over_query_limit
135
+ Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
136
+ l = Geocoder::Lookup.get(:yahoo)
137
+ assert_raises Geocoder::OverQueryLimitError do
138
+ l.send(:results, Geocoder::Query.new("over limit"))
139
+ end
140
+ end
141
+
142
+ # --- Geocoder.ca ---
143
+
144
+ def test_geocoder_ca_result_components
145
+ Geocoder.configure(:lookup => :geocoder_ca)
146
+ set_api_key!(:geocoder_ca)
147
+ result = Geocoder.search([45.423733, -75.676333]).first
148
+ assert_equal "CA", result.country_code
149
+ assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address
150
+ end
151
+
152
+
153
+ # --- FreeGeoIp ---
154
+
155
+ def test_freegeoip_result_on_ip_address_search
156
+ result = Geocoder.search("74.200.247.59").first
157
+ assert result.is_a?(Geocoder::Result::Freegeoip)
158
+ end
159
+
160
+ def test_freegeoip_result_components
161
+ result = Geocoder.search("74.200.247.59").first
162
+ assert_equal "Plano, TX 75093, United States", result.address
163
+ end
164
+
165
+ # --- MaxMind ---
166
+
167
+ def test_maxmind_result_on_ip_address_search
168
+ Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city_isp_org})
169
+ result = Geocoder.search("74.200.247.59").first
170
+ assert result.is_a?(Geocoder::Result::Maxmind)
171
+ end
172
+
173
+ def test_maxmind_result_knows_country_service_name
174
+ Geocoder.configure(:ip_lookup => :maxmind)
175
+ assert_equal :country, Geocoder.search("24.24.24.21").first.service_name
176
+ end
177
+
178
+ def test_maxmind_result_knows_city_service_name
179
+ Geocoder.configure(:ip_lookup => :maxmind)
180
+ assert_equal :city, Geocoder.search("24.24.24.22").first.service_name
181
+ end
182
+
183
+ def test_maxmind_result_knows_city_isp_org_service_name
184
+ Geocoder.configure(:ip_lookup => :maxmind)
185
+ assert_equal :city_isp_org, Geocoder.search("24.24.24.23").first.service_name
186
+ end
187
+
188
+ def test_maxmind_result_knows_omni_service_name
189
+ Geocoder.configure(:ip_lookup => :maxmind)
190
+ assert_equal :omni, Geocoder.search("24.24.24.24").first.service_name
191
+ end
192
+
193
+ def test_maxmind_special_result_components
194
+ Geocoder.configure(:ip_lookup => :maxmind)
195
+ result = Geocoder.search("24.24.24.24").first
196
+ assert_equal "Road Runner", result.isp_name
197
+ assert_equal "Cable/DSL", result.netspeed
198
+ assert_equal "rr.com", result.domain
199
+ end
200
+
201
+ def test_maxmind_raises_exception_when_service_not_configured
202
+ Geocoder.configure(:ip_lookup => :maxmind)
203
+ Geocoder.configure(:maxmind => {:service => nil})
204
+ assert_raises Geocoder::ConfigurationError do
205
+ Geocoder::Query.new("24.24.24.24").url
206
+ end
207
+ end
208
+
209
+
210
+ # --- Bing ---
211
+
212
+ def test_bing_result_components
213
+ Geocoder.configure(:lookup => :bing)
214
+ set_api_key!(:bing)
215
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
216
+ assert_equal "Madison Square Garden, NY", result.address
217
+ assert_equal "NY", result.state
218
+ assert_equal "New York", result.city
219
+ end
220
+
221
+ def test_bing_no_results
222
+ Geocoder.configure(:lookup => :bing)
223
+ set_api_key!(:bing)
224
+ results = Geocoder.search("no results")
225
+ assert_equal 0, results.length
226
+ end
227
+
228
+ # --- Nominatim ---
229
+
230
+ def test_nominatim_result_components
231
+ Geocoder.configure(:lookup => :nominatim)
232
+ set_api_key!(:nominatim)
233
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
234
+ assert_equal "10001", result.postal_code
235
+ end
236
+
237
+ def test_nominatim_address_formatting
238
+ Geocoder.configure(:lookup => :nominatim)
239
+ set_api_key!(:nominatim)
240
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
241
+ assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America",
242
+ result.address
243
+ end
244
+
245
+ def test_nominatim_host_config
246
+ Geocoder.configure(:lookup => :nominatim, :nominatim => {:host => "local.com"})
247
+ lookup = Geocoder::Lookup::Nominatim.new
248
+ query = Geocoder::Query.new("Bluffton, SC")
249
+ assert_match %r(http://local\.com), lookup.query_url(query)
250
+ end
251
+
252
+ # --- MapQuest ---
253
+
254
+ def test_api_route
255
+ Geocoder.configure(:lookup => :mapquest, :api_key => "abc123")
256
+ lookup = Geocoder::Lookup::Mapquest.new
257
+ query = Geocoder::Query.new("Bluffton, SC")
258
+ res = lookup.query_url(query)
259
+ assert_equal "http://www.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC",
260
+ res
261
+ end
262
+
263
+ def test_mapquest_result_components
264
+ Geocoder.configure(:lookup => :mapquest)
265
+ set_api_key!(:mapquest)
266
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
267
+ assert_equal "10001", result.postal_code
268
+ end
269
+
270
+ def test_mapquest_address_formatting
271
+ Geocoder.configure(:lookup => :mapquest)
272
+ set_api_key!(:mapquest)
273
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
274
+ assert_equal "46 West 31st Street, New York, NY, 10001, US",
275
+ result.address
276
+ end
277
+ end