geocoder 1.1.3 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. data/.travis.yml +4 -0
  2. data/{CHANGELOG.rdoc → CHANGELOG.md} +109 -35
  3. data/README.md +645 -0
  4. data/examples/autoexpire_cache.rb +28 -0
  5. data/lib/generators/geocoder/config/templates/initializer.rb +18 -22
  6. data/lib/geocoder/cache.rb +2 -1
  7. data/lib/geocoder/calculations.rb +13 -13
  8. data/lib/geocoder/cli.rb +17 -19
  9. data/lib/geocoder/configuration.rb +68 -42
  10. data/lib/geocoder/configuration_hash.rb +11 -0
  11. data/lib/geocoder/exceptions.rb +3 -0
  12. data/lib/geocoder/lookup.rb +81 -0
  13. data/lib/geocoder/lookups/base.rb +106 -56
  14. data/lib/geocoder/lookups/bing.rb +25 -11
  15. data/lib/geocoder/lookups/freegeoip.rb +11 -7
  16. data/lib/geocoder/lookups/geocoder_ca.rb +17 -9
  17. data/lib/geocoder/lookups/google.rb +29 -8
  18. data/lib/geocoder/lookups/google_premier.rb +20 -11
  19. data/lib/geocoder/lookups/mapquest.rb +29 -19
  20. data/lib/geocoder/lookups/maxmind.rb +72 -0
  21. data/lib/geocoder/lookups/nominatim.rb +21 -13
  22. data/lib/geocoder/lookups/test.rb +12 -6
  23. data/lib/geocoder/lookups/yahoo.rb +63 -13
  24. data/lib/geocoder/lookups/yandex.rb +26 -11
  25. data/lib/geocoder/models/mongoid.rb +1 -2
  26. data/lib/geocoder/query.rb +103 -0
  27. data/lib/geocoder/request.rb +7 -1
  28. data/lib/geocoder/results/base.rb +8 -2
  29. data/lib/geocoder/results/google.rb +6 -0
  30. data/lib/geocoder/results/mapquest.rb +19 -34
  31. data/lib/geocoder/results/maxmind.rb +136 -0
  32. data/lib/geocoder/results/nominatim.rb +14 -3
  33. data/lib/geocoder/results/yahoo.rb +9 -2
  34. data/lib/geocoder/sql.rb +106 -0
  35. data/lib/geocoder/stores/active_record.rb +75 -142
  36. data/lib/geocoder/stores/base.rb +3 -2
  37. data/lib/geocoder/stores/mongo_base.rb +2 -2
  38. data/lib/geocoder/version.rb +1 -1
  39. data/lib/geocoder.rb +11 -89
  40. data/lib/hash_recursive_merge.rb +74 -0
  41. data/lib/oauth_util.rb +112 -0
  42. data/test/active_record_test.rb +15 -0
  43. data/test/cache_test.rb +19 -0
  44. data/test/calculations_test.rb +11 -4
  45. data/test/configuration_test.rb +26 -51
  46. data/test/error_handling_test.rb +15 -11
  47. data/test/fixtures/bing_invalid_key +1 -0
  48. data/test/fixtures/mapquest_madison_square_garden +52 -0
  49. data/test/fixtures/mapquest_no_results +7 -0
  50. data/test/fixtures/maxmind_24_24_24_21 +1 -0
  51. data/test/fixtures/maxmind_24_24_24_22 +1 -0
  52. data/test/fixtures/maxmind_24_24_24_23 +1 -0
  53. data/test/fixtures/maxmind_24_24_24_24 +1 -0
  54. data/test/fixtures/maxmind_74_200_247_59 +1 -0
  55. data/test/fixtures/maxmind_invalid_key +1 -0
  56. data/test/fixtures/maxmind_no_results +1 -0
  57. data/test/fixtures/yahoo_error +1 -0
  58. data/test/fixtures/yahoo_invalid_key +2 -0
  59. data/test/fixtures/yahoo_madison_square_garden +52 -0
  60. data/test/fixtures/yahoo_no_results +10 -0
  61. data/test/fixtures/yahoo_over_limit +2 -0
  62. data/test/https_test.rb +3 -3
  63. data/test/integration/smoke_test.rb +8 -6
  64. data/test/lookup_test.rb +92 -13
  65. data/test/near_test.rb +11 -0
  66. data/test/oauth_util_test.rb +30 -0
  67. data/test/proxy_test.rb +2 -2
  68. data/test/query_test.rb +42 -0
  69. data/test/result_test.rb +4 -3
  70. data/test/services_test.rb +164 -26
  71. data/test/test_helper.rb +93 -112
  72. data/test/test_mode_test.rb +3 -4
  73. metadata +52 -35
  74. data/README.rdoc +0 -552
  75. data/test/fixtures/mapquest_madison_square_garden.json +0 -27
  76. data/test/fixtures/mapquest_no_results.json +0 -1
  77. data/test/fixtures/yahoo_garbage.json +0 -50
  78. data/test/fixtures/yahoo_madison_square_garden.json +0 -46
  79. data/test/fixtures/yahoo_no_results.json +0 -10
  80. data/test/input_handling_test.rb +0 -43
  81. /data/test/fixtures/{bing_madison_square_garden.json → bing_madison_square_garden} +0 -0
  82. /data/test/fixtures/{bing_no_results.json → bing_no_results} +0 -0
  83. /data/test/fixtures/{bing_reverse.json → bing_reverse} +0 -0
  84. /data/test/fixtures/{freegeoip_74_200_247_59.json → freegeoip_74_200_247_59} +0 -0
  85. /data/test/fixtures/{freegeoip_no_results.json → freegeoip_no_results} +0 -0
  86. /data/test/fixtures/{geocoder_ca_madison_square_garden.json → geocoder_ca_madison_square_garden} +0 -0
  87. /data/test/fixtures/{geocoder_ca_no_results.json → geocoder_ca_no_results} +0 -0
  88. /data/test/fixtures/{geocoder_ca_reverse.json → geocoder_ca_reverse} +0 -0
  89. /data/test/fixtures/{google_garbage.json → google_garbage} +0 -0
  90. /data/test/fixtures/{google_madison_square_garden.json → google_madison_square_garden} +0 -0
  91. /data/test/fixtures/{google_no_city_data.json → google_no_city_data} +0 -0
  92. /data/test/fixtures/{google_no_locality.json → google_no_locality} +0 -0
  93. /data/test/fixtures/{google_no_results.json → google_no_results} +0 -0
  94. /data/test/fixtures/{nominatim_madison_square_garden.json → nominatim_madison_square_garden} +0 -0
  95. /data/test/fixtures/{nominatim_no_results.json → nominatim_no_results} +0 -0
  96. /data/test/fixtures/{yandex_invalid_key.json → yandex_invalid_key} +0 -0
  97. /data/test/fixtures/{yandex_kremlin.json → yandex_kremlin} +0 -0
  98. /data/test/fixtures/{yandex_no_results.json → yandex_no_results} +0 -0
data/test/lookup_test.rb CHANGED
@@ -3,35 +3,114 @@ require 'test_helper'
3
3
 
4
4
  class LookupTest < Test::Unit::TestCase
5
5
 
6
+ def test_responds_to_name_method
7
+ Geocoder::Lookup.all_services.each do |l|
8
+ lookup = Geocoder::Lookup.get(l)
9
+ assert lookup.respond_to?(:name),
10
+ "Lookup #{l} does not respond to #name method."
11
+ end
12
+ end
13
+
6
14
  def test_search_returns_empty_array_when_no_results
7
- all_lookups_except_test.each do |l|
8
- lookup = Geocoder.send(:get_lookup, l)
9
- assert_equal [], lookup.send(:results, "no results"),
15
+ Geocoder::Lookup.all_services_except_test.each do |l|
16
+ lookup = Geocoder::Lookup.get(l)
17
+ set_api_key!(l)
18
+ assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")),
10
19
  "Lookup #{l} does not return empty array when no results."
11
20
  end
12
21
  end
13
22
 
23
+ def test_query_url_contains_values_in_params_hash
24
+ Geocoder::Lookup.all_services_except_test.each do |l|
25
+ next if l == :freegeoip # does not use query string
26
+ set_api_key!(l)
27
+ url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
28
+ "test", :params => {:one_in_the_hand => "two in the bush"}
29
+ ))
30
+ # should be "+"s for all lookups except Yahoo
31
+ assert_match /one_in_the_hand=two(%20|\+)in(%20|\+)the(%20|\+)bush/, url,
32
+ "Lookup #{l} does not appear to support arbitrary params in URL"
33
+ end
34
+ end
35
+
36
+ {
37
+ :bing => :key,
38
+ :geocoder_ca => :auth,
39
+ :google => :language,
40
+ :google_premier => :language,
41
+ :mapquest => :key,
42
+ :maxmind => :l,
43
+ :nominatim => :"accept-language",
44
+ :yahoo => :locale,
45
+ :yandex => :plng
46
+ }.each do |l,p|
47
+ define_method "test_passing_param_to_#{l}_query_overrides_configuration_value" do
48
+ set_api_key!(l)
49
+ url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
50
+ "test", :params => {p => "xxxx"}
51
+ ))
52
+ assert_match /#{p}=xxxx/, url,
53
+ "Param passed to #{l} lookup does not override configuration value"
54
+ end
55
+ end
56
+
57
+ def test_raises_exception_on_invalid_key
58
+ Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
59
+ #Geocoder::Lookup.all_services_except_test.each do |l|
60
+ [:bing, :yahoo, :yandex, :maxmind].each do |l|
61
+ lookup = Geocoder::Lookup.get(l)
62
+ assert_raises Geocoder::InvalidApiKey do
63
+ lookup.send(:results, Geocoder::Query.new("invalid key"))
64
+ end
65
+ end
66
+ end
67
+
68
+ def test_returns_empty_array_on_invalid_key
69
+ # keep test output clean: suppress timeout warning
70
+ orig = $VERBOSE; $VERBOSE = nil
71
+ #Geocoder::Lookup.all_services_except_test.each do |l|
72
+ [:bing, :yahoo, :yandex, :maxmind].each do |l|
73
+ Geocoder.configure(:lookup => l)
74
+ set_api_key!(l)
75
+ assert_equal [], Geocoder.search("invalid key")
76
+ end
77
+ ensure
78
+ $VERBOSE = orig
79
+ end
80
+
81
+ def test_does_not_choke_on_nil_address
82
+ Geocoder::Lookup.all_services.each do |l|
83
+ Geocoder.configure(:lookup => l)
84
+ assert_nothing_raised { Venue.new("Venue", nil).geocode }
85
+ end
86
+ end
87
+
14
88
  def test_hash_to_query
15
89
  g = Geocoder::Lookup::Google.new
16
90
  assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
17
91
  end
18
92
 
19
93
  def test_google_api_key
20
- Geocoder::Configuration.api_key = "MY_KEY"
94
+ Geocoder.configure(:api_key => "MY_KEY")
21
95
  g = Geocoder::Lookup::Google.new
22
- assert_match "key=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
23
- end
24
-
25
- def test_yahoo_app_id
26
- Geocoder::Configuration.api_key = "MY_KEY"
27
- g = Geocoder::Lookup::Yahoo.new
28
- assert_match "appid=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
96
+ assert_match "key=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
29
97
  end
30
98
 
31
99
  def test_geocoder_ca_showpostal
32
- Geocoder::Configuration.api_key = "MY_KEY"
100
+ Geocoder.configure(:api_key => "MY_KEY")
33
101
  g = Geocoder::Lookup::GeocoderCa.new
34
- assert_match "showpostal=1", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
102
+ assert_match "showpostal=1", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
103
+ end
104
+
105
+ def test_raises_configuration_error_on_missing_key
106
+ assert_raises Geocoder::ConfigurationError do
107
+ Geocoder.configure(:lookup => :bing, :api_key => nil)
108
+ Geocoder.search("Madison Square Garden, New York, NY 10001, United States")
109
+ end
35
110
  end
36
111
 
112
+ def test_handle
113
+ assert_equal :google, Geocoder::Lookup::Google.new.handle
114
+ assert_equal :geocoder_ca, Geocoder::Lookup::GeocoderCa.new.handle
115
+ end
37
116
  end
data/test/near_test.rb ADDED
@@ -0,0 +1,11 @@
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
+ 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
data/test/proxy_test.rb CHANGED
@@ -4,7 +4,7 @@ require 'test_helper'
4
4
  class ProxyTest < Test::Unit::TestCase
5
5
 
6
6
  def test_uses_proxy_when_specified
7
- Geocoder::Configuration.http_proxy = 'localhost'
7
+ Geocoder.configure(:http_proxy => 'localhost')
8
8
  lookup = Geocoder::Lookup::Google.new
9
9
  assert lookup.send(:http_client).proxy_class?
10
10
  end
@@ -15,7 +15,7 @@ class ProxyTest < Test::Unit::TestCase
15
15
  end
16
16
 
17
17
  def test_exception_raised_on_bad_proxy_url
18
- Geocoder::Configuration.http_proxy = ' \\_O< Quack Quack'
18
+ Geocoder.configure(:http_proxy => ' \\_O< Quack Quack')
19
19
  assert_raise Geocoder::ConfigurationError do
20
20
  Geocoder::Lookup::Google.new.send(:http_client)
21
21
  end
@@ -0,0 +1,42 @@
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
+ end
14
+
15
+ def test_blank_query_detection
16
+ assert Geocoder::Query.new(nil).blank?
17
+ assert Geocoder::Query.new("").blank?
18
+ assert Geocoder::Query.new("\t ").blank?
19
+ assert !Geocoder::Query.new("a").blank?
20
+ assert !Geocoder::Query.new("Москва").blank? # no ASCII characters
21
+
22
+ assert Geocoder::Query.new(nil, :params => {}).blank?
23
+ assert !Geocoder::Query.new(nil, :params => {:woeid => 1234567}).blank?
24
+ end
25
+
26
+ def test_blank_query_detection_for_coordinates
27
+ assert Geocoder::Query.new([nil,nil]).blank?
28
+ assert Geocoder::Query.new([87,nil]).blank?
29
+ end
30
+
31
+ def test_coordinates_detection
32
+ assert Geocoder::Query.new("51.178844,5").coordinates?
33
+ assert Geocoder::Query.new("51.178844, -1.826189").coordinates?
34
+ assert !Geocoder::Query.new("232.65.123").coordinates?
35
+ end
36
+
37
+ def test_loopback_ip_address
38
+ assert Geocoder::Query.new("0.0.0.0").loopback_ip_address?
39
+ assert Geocoder::Query.new("127.0.0.1").loopback_ip_address?
40
+ assert !Geocoder::Query.new("232.65.123.234").loopback_ip_address?
41
+ end
42
+ end
data/test/result_test.rb CHANGED
@@ -4,8 +4,9 @@ require 'test_helper'
4
4
  class ResultTest < Test::Unit::TestCase
5
5
 
6
6
  def test_result_has_required_attributes
7
- all_lookups_except_test.each do |l|
8
- Geocoder::Configuration.lookup = l
7
+ Geocoder::Lookup.all_services_except_test.each do |l|
8
+ Geocoder.configure(:lookup => l)
9
+ set_api_key!(l)
9
10
  result = Geocoder.search([45.423733, -75.676333]).first
10
11
  assert_result_has_required_attributes(result)
11
12
  end
@@ -15,7 +16,7 @@ class ResultTest < Test::Unit::TestCase
15
16
  private # ------------------------------------------------------------------
16
17
 
17
18
  def assert_result_has_required_attributes(result)
18
- m = "Lookup #{Geocoder::Configuration.lookup} does not support %s attribute."
19
+ m = "Lookup #{Geocoder.config.lookup} does not support %s attribute."
19
20
  assert result.coordinates.is_a?(Array), m % "coordinates"
20
21
  assert result.latitude.is_a?(Float), m % "latitude"
21
22
  assert result.longitude.is_a?(Float), m % "longitude"
@@ -3,7 +3,6 @@ require 'test_helper'
3
3
 
4
4
  class ServicesTest < Test::Unit::TestCase
5
5
 
6
-
7
6
  # --- Google ---
8
7
 
9
8
  def test_google_result_components
@@ -12,6 +11,12 @@ class ServicesTest < Test::Unit::TestCase
12
11
  result.address_components_of_type(:sublocality).first['long_name']
13
12
  end
14
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
+
15
20
  def test_google_returns_city_when_no_locality_in_result
16
21
  result = Geocoder.search("no locality").first
17
22
  assert_equal "Haram", result.city
@@ -28,54 +33,106 @@ class ServicesTest < Test::Unit::TestCase
28
33
  result.precision
29
34
  end
30
35
 
36
+ def test_google_query_url_contains_bounds
37
+ lookup = Geocoder::Lookup::Google.new
38
+ url = lookup.query_url(Geocoder::Query.new(
39
+ "Some Intersection",
40
+ :bounds => [[40.0, -120.0], [39.0, -121.0]]
41
+ ))
42
+ assert_match /bounds=40.0+%2C-120.0+%7C39.0+%2C-121.0+/, url
43
+ end
44
+
45
+ def test_google_query_url_contains_region
46
+ lookup = Geocoder::Lookup::Google.new
47
+ url = lookup.query_url(Geocoder::Query.new(
48
+ "Some Intersection",
49
+ :region => "gb"
50
+ ))
51
+ assert_match /region=gb/, url
52
+ end
53
+
54
+ def test_google_query_url_contains_components_when_given_as_string
55
+ lookup = Geocoder::Lookup::Google.new
56
+ url = lookup.query_url(Geocoder::Query.new(
57
+ "Some Intersection",
58
+ :components => "locality:ES"
59
+ ))
60
+ formatted = "components=" + CGI.escape("locality:ES")
61
+ assert url.include?(formatted), "Expected #{formatted} to be included in #{url}"
62
+ end
63
+
64
+ def test_google_query_url_contains_components_when_given_as_array
65
+ lookup = Geocoder::Lookup::Google.new
66
+ url = lookup.query_url(Geocoder::Query.new(
67
+ "Some Intersection",
68
+ :components => ["country:ES", "locality:ES"]
69
+ ))
70
+ formatted = "components=" + CGI.escape("country:ES|locality:ES")
71
+ assert url.include?(formatted), "Expected #{formatted} to be included in #{url}"
72
+ end
31
73
 
32
74
  # --- Google Premier ---
33
75
 
34
76
  def test_google_premier_result_components
35
- Geocoder::Configuration.lookup = :google_premier
77
+ Geocoder.configure(:lookup => :google_premier)
78
+ set_api_key!(:google_premier)
36
79
  result = Geocoder.search("Madison Square Garden, New York, NY").first
37
80
  assert_equal "Manhattan",
38
81
  result.address_components_of_type(:sublocality).first['long_name']
39
82
  end
40
83
 
41
84
  def test_google_premier_query_url
42
- Geocoder::Configuration.api_key = ["deadbeef", "gme-test", "test-dev"]
85
+ Geocoder.configure(:api_key => ["deadbeef", "gme-test", "test-dev"])
43
86
  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=",
44
- Geocoder::Lookup::GooglePremier.new.send(:query_url, "Madison Square Garden, New York, NY", false)
87
+ Geocoder::Lookup::GooglePremier.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY"))
45
88
  end
46
89
 
47
90
 
48
91
  # --- Yahoo ---
49
92
 
93
+ def test_yahoo_no_results
94
+ Geocoder.configure(:lookup => :yahoo)
95
+ set_api_key!(:yahoo)
96
+ assert_equal [], Geocoder.search("no results")
97
+ end
98
+
99
+ def test_yahoo_error
100
+ Geocoder.configure(:lookup => :yahoo)
101
+ set_api_key!(:yahoo)
102
+ # keep test output clean: suppress timeout warning
103
+ orig = $VERBOSE; $VERBOSE = nil
104
+ assert_equal [], Geocoder.search("error")
105
+ ensure
106
+ $VERBOSE = orig
107
+ end
108
+
50
109
  def test_yahoo_result_components
51
- Geocoder::Configuration.lookup = :yahoo
52
- result = Geocoder.search("Madison Square Garden, New York, NY").first
110
+ Geocoder.configure(:lookup => :yahoo)
111
+ set_api_key!(:yahoo)
112
+ result = Geocoder.search("madison square garden").first
53
113
  assert_equal "10001", result.postal_code
54
114
  end
55
115
 
56
116
  def test_yahoo_address_formatting
57
- Geocoder::Configuration.lookup = :yahoo
58
- result = Geocoder.search("Madison Square Garden, New York, NY").first
59
- assert_equal "Madison Square Garden, New York, NY 10001, United States",
60
- result.address
117
+ Geocoder.configure(:lookup => :yahoo)
118
+ set_api_key!(:yahoo)
119
+ result = Geocoder.search("madison square garden").first
120
+ assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address
61
121
  end
62
122
 
63
-
64
- # --- Yandex ---
65
-
66
- def test_yandex_with_invalid_key
67
- # keep test output clean: suppress timeout warning
68
- orig = $VERBOSE; $VERBOSE = nil
69
- Geocoder::Configuration.lookup = :yandex
70
- assert_equal [], Geocoder.search("invalid key")
71
- $VERBOSE = orig
123
+ def test_yahoo_raises_exception_when_over_query_limit
124
+ Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
125
+ l = Geocoder::Lookup.get(:yahoo)
126
+ assert_raises Geocoder::OverQueryLimitError do
127
+ l.send(:results, Geocoder::Query.new("over limit"))
128
+ end
72
129
  end
73
130
 
74
-
75
131
  # --- Geocoder.ca ---
76
132
 
77
133
  def test_geocoder_ca_result_components
78
- Geocoder::Configuration.lookup = :geocoder_ca
134
+ Geocoder.configure(:lookup => :geocoder_ca)
135
+ set_api_key!(:geocoder_ca)
79
136
  result = Geocoder.search([45.423733, -75.676333]).first
80
137
  assert_equal "CA", result.country_code
81
138
  assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address
@@ -94,11 +151,56 @@ class ServicesTest < Test::Unit::TestCase
94
151
  assert_equal "Plano, TX 75093, United States", result.address
95
152
  end
96
153
 
154
+ # --- MaxMind ---
155
+
156
+ def test_maxmind_result_on_ip_address_search
157
+ Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city_isp_org})
158
+ result = Geocoder.search("74.200.247.59").first
159
+ assert result.is_a?(Geocoder::Result::Maxmind)
160
+ end
161
+
162
+ def test_maxmind_result_knows_country_service_name
163
+ Geocoder.configure(:ip_lookup => :maxmind)
164
+ assert_equal :country, Geocoder.search("24.24.24.21").first.service_name
165
+ end
166
+
167
+ def test_maxmind_result_knows_city_service_name
168
+ Geocoder.configure(:ip_lookup => :maxmind)
169
+ assert_equal :city, Geocoder.search("24.24.24.22").first.service_name
170
+ end
171
+
172
+ def test_maxmind_result_knows_city_isp_org_service_name
173
+ Geocoder.configure(:ip_lookup => :maxmind)
174
+ assert_equal :city_isp_org, Geocoder.search("24.24.24.23").first.service_name
175
+ end
176
+
177
+ def test_maxmind_result_knows_omni_service_name
178
+ Geocoder.configure(:ip_lookup => :maxmind)
179
+ assert_equal :omni, Geocoder.search("24.24.24.24").first.service_name
180
+ end
181
+
182
+ def test_maxmind_special_result_components
183
+ Geocoder.configure(:ip_lookup => :maxmind)
184
+ result = Geocoder.search("24.24.24.24").first
185
+ assert_equal "Road Runner", result.isp_name
186
+ assert_equal "Cable/DSL", result.netspeed
187
+ assert_equal "rr.com", result.domain
188
+ end
189
+
190
+ def test_maxmind_raises_exception_when_service_not_configured
191
+ Geocoder.configure(:ip_lookup => :maxmind)
192
+ Geocoder.configure(:maxmind => {:service => nil})
193
+ assert_raises Geocoder::ConfigurationError do
194
+ Geocoder::Query.new("24.24.24.24").url
195
+ end
196
+ end
197
+
97
198
 
98
199
  # --- Bing ---
99
200
 
100
201
  def test_bing_result_components
101
- Geocoder::Configuration.lookup = :bing
202
+ Geocoder.configure(:lookup => :bing)
203
+ set_api_key!(:bing)
102
204
  result = Geocoder.search("Madison Square Garden, New York, NY").first
103
205
  assert_equal "Madison Square Garden, NY", result.address
104
206
  assert_equal "NY", result.state
@@ -106,23 +208,59 @@ class ServicesTest < Test::Unit::TestCase
106
208
  end
107
209
 
108
210
  def test_bing_no_results
109
- Geocoder::Configuration.lookup = :bing
211
+ Geocoder.configure(:lookup => :bing)
212
+ set_api_key!(:bing)
110
213
  results = Geocoder.search("no results")
111
214
  assert_equal 0, results.length
112
215
  end
113
216
 
114
217
  # --- Nominatim ---
115
218
 
116
- def test_nominatim_result_components
117
- Geocoder::Configuration.lookup = :nominatim
219
+ def test_nominatim_result_components
220
+ Geocoder.configure(:lookup => :nominatim)
221
+ set_api_key!(:nominatim)
118
222
  result = Geocoder.search("Madison Square Garden, New York, NY").first
119
223
  assert_equal "10001", result.postal_code
120
224
  end
121
225
 
122
226
  def test_nominatim_address_formatting
123
- Geocoder::Configuration.lookup = :nominatim
227
+ Geocoder.configure(:lookup => :nominatim)
228
+ set_api_key!(:nominatim)
124
229
  result = Geocoder.search("Madison Square Garden, New York, NY").first
125
230
  assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America",
126
231
  result.address
127
232
  end
233
+
234
+ def test_nominatim_host_config
235
+ Geocoder.configure(:lookup => :nominatim, :nominatim => {:host => "local.com"})
236
+ lookup = Geocoder::Lookup::Nominatim.new
237
+ query = Geocoder::Query.new("Bluffton, SC")
238
+ assert_match %r(http://local\.com), lookup.query_url(query)
239
+ end
240
+
241
+ # --- MapQuest ---
242
+
243
+ def test_api_route
244
+ Geocoder.configure(:lookup => :mapquest, :api_key => "abc123")
245
+ lookup = Geocoder::Lookup::Mapquest.new
246
+ query = Geocoder::Query.new("Bluffton, SC")
247
+ res = lookup.query_url(query)
248
+ assert_equal "http://www.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC",
249
+ res
250
+ end
251
+
252
+ def test_mapquest_result_components
253
+ Geocoder.configure(:lookup => :mapquest)
254
+ set_api_key!(:mapquest)
255
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
256
+ assert_equal "10001", result.postal_code
257
+ end
258
+
259
+ def test_mapquest_address_formatting
260
+ Geocoder.configure(:lookup => :mapquest)
261
+ set_api_key!(:mapquest)
262
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
263
+ assert_equal "46 West 31st Street, New York, NY, 10001, US",
264
+ result.address
265
+ end
128
266
  end