geokit 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +6 -14
  2. data/.travis.yml +1 -0
  3. data/CHANGELOG.md +11 -0
  4. data/Gemfile +2 -1
  5. data/MIT-LICENSE +20 -0
  6. data/README.markdown +44 -39
  7. data/Rakefile +15 -0
  8. data/fixtures/vcr_cassettes/bing_full.yml +102 -0
  9. data/fixtures/vcr_cassettes/bing_full_au.yml +91 -0
  10. data/fixtures/vcr_cassettes/bing_full_de.yml +91 -0
  11. data/fixtures/vcr_cassettes/fcc_reverse_geocode.yml +37 -0
  12. data/fixtures/vcr_cassettes/free_geo_ip_geocode.yml +36 -0
  13. data/fixtures/vcr_cassettes/geo_plugin_geocode.yml +38 -0
  14. data/fixtures/vcr_cassettes/geonames_geocode.yml +304 -0
  15. data/fixtures/vcr_cassettes/{google3_city.yml → google_city.yml} +0 -0
  16. data/fixtures/vcr_cassettes/{google3_country_code_biased_result.yml → google_country_code_biased_result.yml} +0 -0
  17. data/fixtures/vcr_cassettes/{google3_full.yml → google_full.yml} +0 -0
  18. data/fixtures/vcr_cassettes/{google3_full_short.yml → google_full_short.yml} +0 -0
  19. data/fixtures/vcr_cassettes/{google3_language_response_fr.yml → google_language_response_fr.yml} +0 -0
  20. data/fixtures/vcr_cassettes/{google3_multi.yml → google_multi.yml} +0 -0
  21. data/fixtures/vcr_cassettes/{google3_reverse_madrid.yml → google_reverse_madrid.yml} +0 -0
  22. data/fixtures/vcr_cassettes/ripe_geocode.yml +66 -0
  23. data/fixtures/vcr_cassettes/ripe_geocode_au.yml +66 -0
  24. data/geokit.gemspec +1 -1
  25. data/lib/geokit.rb +5 -0
  26. data/lib/geokit/bounds.rb +96 -0
  27. data/lib/geokit/core_ext.rb +17 -0
  28. data/lib/geokit/geo_loc.rb +134 -0
  29. data/lib/geokit/geocoders.rb +48 -35
  30. data/lib/geokit/geocoders/base_ip.rb +43 -0
  31. data/lib/geokit/geocoders/bing.rb +101 -0
  32. data/lib/geokit/geocoders/ca_geocoder.rb +50 -0
  33. data/lib/geokit/{services → geocoders}/fcc.rb +17 -20
  34. data/lib/geokit/geocoders/free_geo_ip.rb +34 -0
  35. data/lib/geokit/geocoders/geo_plugin.rb +33 -0
  36. data/lib/geokit/geocoders/geonames.rb +53 -0
  37. data/lib/geokit/{services/google3.rb → geocoders/google.rb} +59 -57
  38. data/lib/geokit/geocoders/ip.rb +69 -0
  39. data/lib/geokit/geocoders/mapquest.rb +72 -0
  40. data/lib/geokit/geocoders/maxmind.rb +29 -0
  41. data/lib/geokit/geocoders/openstreetmap.rb +119 -0
  42. data/lib/geokit/geocoders/ripe.rb +41 -0
  43. data/lib/geokit/{services → geocoders}/us_geocoder.rb +15 -20
  44. data/lib/geokit/{services → geocoders}/yahoo.rb +52 -55
  45. data/lib/geokit/geocoders/yandex.rb +61 -0
  46. data/lib/geokit/inflectors.rb +1 -2
  47. data/lib/geokit/lat_lng.rb +129 -0
  48. data/lib/geokit/mappable.rb +41 -424
  49. data/lib/geokit/multi_geocoder.rb +6 -2
  50. data/lib/geokit/polygon.rb +46 -0
  51. data/lib/geokit/version.rb +1 -1
  52. data/test/helper.rb +2 -12
  53. data/test/test_base_geocoder.rb +0 -10
  54. data/test/test_bing_geocoder.rb +60 -0
  55. data/test/test_fcc_geocoder.rb +23 -0
  56. data/test/test_free_geo_ip_geocoder.rb +23 -0
  57. data/test/test_geo_plugin_geocoder.rb +23 -0
  58. data/test/test_geonames_geocoder.rb +23 -0
  59. data/test/test_google_geocoder.rb +208 -235
  60. data/test/test_maxmind_geocoder.rb +35 -4
  61. data/test/test_multi_geocoder.rb +3 -1
  62. data/test/test_ripe_geocoder.rb +35 -0
  63. data/test/test_yahoo_geocoder.rb +0 -12
  64. metadata +78 -52
  65. data/LICENSE +0 -25
  66. data/Manifest.txt +0 -21
  67. data/data/GeoLiteCity.dat +0 -0
  68. data/lib/geokit/services/ca_geocoder.rb +0 -55
  69. data/lib/geokit/services/geo_plugin.rb +0 -31
  70. data/lib/geokit/services/geonames.rb +0 -53
  71. data/lib/geokit/services/google.rb +0 -158
  72. data/lib/geokit/services/ip.rb +0 -103
  73. data/lib/geokit/services/maxmind.rb +0 -39
  74. data/lib/geokit/services/openstreetmap.rb +0 -119
  75. data/lib/geokit/services/ripe.rb +0 -32
  76. data/lib/geokit/services/yandex.rb +0 -51
  77. data/test/test_google_geocoder3.rb +0 -238
  78. data/test/test_google_reverse_geocoder.rb +0 -49
@@ -27,8 +27,8 @@ module Geokit
27
27
  provider_order = geocode_ip ? Geokit::Geocoders::ip_provider_order : Geokit::Geocoders::provider_order
28
28
 
29
29
  provider_order.each do |provider|
30
+ klass = geocoder(provider)
30
31
  begin
31
- klass = Geokit::Geocoders.const_get "#{Geokit::Inflector::camelize(provider.to_s)}Geocoder"
32
32
  res = klass.send :geocode, address, options
33
33
  return res if res.success?
34
34
  rescue => e
@@ -44,8 +44,8 @@ module Geokit
44
44
  # to try to reverse geocode a geographical point.
45
45
  def self.do_reverse_geocode(latlng)
46
46
  Geokit::Geocoders::provider_order.each do |provider|
47
+ klass = geocoder(provider)
47
48
  begin
48
- klass = Geokit::Geocoders.const_get "#{Geokit::Inflector::camelize(provider.to_s)}Geocoder"
49
49
  res = klass.send :reverse_geocode, latlng
50
50
  return res if res.success?
51
51
  rescue => e
@@ -55,6 +55,10 @@ module Geokit
55
55
  # If we get here, we failed completely.
56
56
  GeoLoc.new
57
57
  end
58
+
59
+ def self.geocoder(provider)
60
+ Geokit::Geocoders.const_get "#{Geokit::Inflector::camelize(provider.to_s)}Geocoder"
61
+ end
58
62
  end
59
63
  end
60
64
  end
@@ -0,0 +1,46 @@
1
+ module Geokit
2
+ # A complex polygon made of multiple points. End point must equal start point to close the poly.
3
+ class Polygon
4
+
5
+ attr_accessor :poly_y, :poly_x
6
+
7
+ def initialize(points)
8
+ # Pass in an array of Geokit::LatLng
9
+ @poly_x = []
10
+ @poly_y = []
11
+
12
+ points.each do |point|
13
+ @poly_x << point.lng
14
+ @poly_y << point.lat
15
+ end
16
+
17
+ # A Polygon must be 'closed', the last point equal to the first point
18
+ if @poly_x[0] != @poly_x[-1] || @poly_y[0] != @poly_y[-1]
19
+ # Append the first point to the array to close the polygon
20
+ @poly_x << @poly_x[0]
21
+ @poly_y << @poly_y[0]
22
+ end
23
+
24
+ end
25
+
26
+ def contains?(point)
27
+ j = @poly_x.length - 1
28
+ oddNodes = false
29
+ x = point.lng
30
+ y = point.lat
31
+
32
+ for i in (0..j)
33
+ if (@poly_y[i] < y && @poly_y[j] >= y ||
34
+ @poly_y[j] < y && @poly_y[i] >= y)
35
+ if (@poly_x[i] + (y - @poly_y[i]) / (@poly_y[j] - @poly_y[i]) * (@poly_x[j] - @poly_x[i]) < x)
36
+ oddNodes = !oddNodes
37
+ end
38
+ end
39
+
40
+ j=i
41
+ end
42
+
43
+ oddNodes
44
+ end # contains?
45
+ end # class Polygon
46
+ end
@@ -1,3 +1,3 @@
1
1
  module Geokit
2
- VERSION = '1.7.1'
2
+ VERSION = '1.8.0'
3
3
  end
data/test/helper.rb CHANGED
@@ -11,13 +11,13 @@ end
11
11
  require 'geoip'
12
12
 
13
13
  if ENV['COVERAGE']
14
- COVERAGE_THRESHOLD = 84
14
+ COVERAGE_THRESHOLD = 90
15
15
  require 'simplecov'
16
16
  require 'simplecov-rcov'
17
17
  require 'coveralls'
18
18
  Coveralls.wear!
19
19
 
20
- SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
20
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
21
21
  SimpleCov.start do
22
22
  add_filter '/test/'
23
23
  add_group 'lib', 'lib'
@@ -126,14 +126,4 @@ class BaseGeocoderTest < Test::Unit::TestCase #:nodoc: all
126
126
  Geokit::Geocoders::Geocoder.expects(:do_get).with(url).returns("SUCCESS")
127
127
  assert_equal "SUCCESS", Geokit::Geocoders::Geocoder.call_geocoder_service(url)
128
128
  end
129
-
130
- def test_find_geocoder_methods
131
- public_methods = Geokit::Geocoders::Geocoder.public_methods.map { |m| m.to_s }
132
- assert public_methods.include?("yahoo_geocoder")
133
- assert public_methods.include?("google_geocoder")
134
- assert public_methods.include?("ca_geocoder")
135
- assert public_methods.include?("us_geocoder")
136
- assert public_methods.include?("multi_geocoder")
137
- assert public_methods.include?("ip_geocoder")
138
- end
139
129
  end
@@ -31,14 +31,4 @@ class BaseGeocoderTest < Test::Unit::TestCase #:nodoc: all
31
31
  Geokit::Geocoders::Geocoder.expects(:do_get).with(url).returns("SUCCESS")
32
32
  assert_equal "SUCCESS", Geokit::Geocoders::Geocoder.call_geocoder_service(url)
33
33
  end
34
-
35
- def test_find_geocoder_methods
36
- public_methods = Geokit::Geocoders::Geocoder.public_methods.map { |m| m.to_s }
37
- assert public_methods.include?("yahoo_geocoder")
38
- assert public_methods.include?("google_geocoder")
39
- assert public_methods.include?("ca_geocoder")
40
- assert public_methods.include?("us_geocoder")
41
- assert public_methods.include?("multi_geocoder")
42
- assert public_methods.include?("ip_geocoder")
43
- end
44
34
  end
@@ -0,0 +1,60 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ Geokit::Geocoders::bing = 'AuWcmtBIoPeOubm9BtcN44hTmWw_wNoJ5NEO2L0RaKrGAUE_nlwciKAqwapdq7k7'
4
+
5
+ class BingGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ def setup
8
+ super
9
+ end
10
+
11
+ def assert_url(expected_url)
12
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '')
13
+ end
14
+
15
+ # the testing methods themselves
16
+ def test_bing_full_address
17
+ VCR.use_cassette('bing_full') do
18
+ url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(@full_address)}?key=#{Geokit::Geocoders::bing}&o=xml"
19
+ res = Geokit::Geocoders::BingGeocoder.geocode(@full_address)
20
+ assert_equal "CA", res.state
21
+ assert_equal "San Francisco", res.city
22
+ assert_array_in_delta [37.792332, -122.393791], res.to_a
23
+ assert res.country == 'United States'
24
+ assert_equal "100 Spear St, San Francisco, CA 94105", res.full_address
25
+ assert_equal "bing", res.provider
26
+ assert_url url
27
+ end
28
+ end
29
+
30
+ def test_bing_full_address_au
31
+ address = '440 King William Street, Adelaide, Australia'
32
+ VCR.use_cassette('bing_full_au') do
33
+ url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address)}?key=#{Geokit::Geocoders::bing}&o=xml"
34
+ res = Geokit::Geocoders::BingGeocoder.geocode(address)
35
+ assert_equal "SA", res.state
36
+ assert_equal "Adelaide", res.city
37
+ assert_array_in_delta [-34.934582, 138.600784], res.to_a
38
+ assert res.country == 'Australia'
39
+ assert_equal "402-440 King William St, Adelaide, SA 5000", res.full_address
40
+ assert_equal "Australia", res.country
41
+ assert_equal "bing", res.provider
42
+ assert_url url
43
+ end
44
+ end
45
+
46
+ def test_bing_full_address_de
47
+ address = "Platz der Republik 1, 11011 Berlin, Germany"
48
+ VCR.use_cassette('bing_full_de') do
49
+ url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address)}?key=#{Geokit::Geocoders::bing}&o=xml"
50
+ res = Geokit::Geocoders::BingGeocoder.geocode(address)
51
+ assert_equal "BE", res.state
52
+ assert_equal "Berlin", res.city
53
+ assert_array_in_delta [52.518596, 13.375502], res.to_a
54
+ assert res.country == 'Germany'
55
+ assert_equal "Platz der Republik 1, 10557 Berlin", res.full_address
56
+ assert_equal "bing", res.provider
57
+ assert_url url
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class FCCGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+ def setup
5
+ super
6
+ @la = Geokit::LatLng.new(34.05, -118.25)
7
+ end
8
+
9
+ def assert_url(expected_url)
10
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
11
+ end
12
+
13
+ def test_fcc_reverse_geocode
14
+ VCR.use_cassette('fcc_reverse_geocode') do
15
+ url = "http://data.fcc.gov/api/block/find?format=json&latitude=34.05&longitude=-118.25"
16
+ res = Geokit::Geocoders::FCCGeocoder.reverse_geocode(@la)
17
+ assert_url url
18
+ assert_equal res.country_code, 'US'
19
+ assert_equal res.state, 'CA'
20
+ assert_equal res.district, 'Los Angeles'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class FreeGeoIpGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+ def setup
5
+ super
6
+ @ip = '74.125.237.209'
7
+ end
8
+
9
+ def assert_url(expected_url)
10
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
11
+ end
12
+
13
+ def test_free_geo_ip_geocode
14
+ VCR.use_cassette('free_geo_ip_geocode') do
15
+ url = "http://freegeoip.net/xml/#{@ip}"
16
+ res = Geokit::Geocoders::FreeGeoIpGeocoder.geocode(@ip)
17
+ assert_url url
18
+ assert_equal res.city, 'Mountain View'
19
+ assert_equal res.state, 'CA'
20
+ assert_equal res.country_code, 'US'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class GeoPluginGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+ def setup
5
+ super
6
+ @ip = '74.125.237.209'
7
+ end
8
+
9
+ def assert_url(expected_url)
10
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
11
+ end
12
+
13
+ def test_geo_plugin_geocode
14
+ VCR.use_cassette('geo_plugin_geocode') do
15
+ url = "http://www.geoplugin.net/xml.gp?ip=#{@ip}"
16
+ res = Geokit::Geocoders::GeoPluginGeocoder.geocode(@ip)
17
+ assert_url url
18
+ assert_equal res.city, 'Mountain View'
19
+ assert_equal res.state, 'CA'
20
+ assert_equal res.country_code, 'US'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class GeonamesGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+ def setup
5
+ super
6
+ @city = 'Adelaide'
7
+ end
8
+
9
+ def assert_url(expected_url)
10
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
11
+ end
12
+
13
+ def test_geonames_geocode
14
+ VCR.use_cassette('geonames_geocode') do
15
+ url = "http://ws.geonames.org/postalCodeSearch?placename=#{@city}&maxRows=10"
16
+ res = Geokit::Geocoders::GeonamesGeocoder.geocode(@city)
17
+ assert_url url
18
+ assert_equal res.country_code, 'AU'
19
+ assert_equal res.state, 'South Australia'
20
+ assert_equal res.city, 'Adelaide'
21
+ end
22
+ end
23
+ end
@@ -1,264 +1,237 @@
1
- # encoding: UTF-8
1
+ # -*- coding: utf-8 -*-
2
2
  require File.join(File.dirname(__FILE__), 'helper')
3
3
 
4
- Geokit::Geocoders::google = 'Google'
4
+ Geokit::Geocoders::google_client_id = nil
5
+ Geokit::Geocoders::google_cryptographic_key = nil
6
+ Geokit::Geocoders::google_channel = nil
5
7
 
6
8
  class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
7
9
 
8
- GOOGLE_FULL=<<-EOF.strip
9
- <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>100 spear st, san francisco, ca</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>100 Spear St, San Francisco, CA 94105, USA</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>San Francisco</SubAdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName><Thoroughfare><ThoroughfareName>100 Spear St</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>94105</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.393985,37.792501,0</coordinates></Point></Placemark></Response></kml>
10
- EOF
11
-
12
- GOOGLE_RESULT_WITH_SUGGESTED_BOUNDS=<<-EOF.strip
13
- <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>100 spear st, san francisco, ca</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>100 Spear St, San Francisco, CA 94105, USA</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>San Francisco</SubAdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName><Thoroughfare><ThoroughfareName>100 Spear St</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>94105</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="37.7956328" south="37.7893376" east="-122.3908573" west="-122.3971525" /></ExtendedData><Point><coordinates>-122.393985,37.792501,0</coordinates></Point></Placemark></Response></kml>
14
- EOF
15
-
16
- GOOGLE_CITY=<<-EOF.strip
17
- <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>San Francisco</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>San Francisco, CA, USA</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName></Locality></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.418333,37.775000,0</coordinates></Point></Placemark></Response></kml>
18
- EOF
19
-
20
- GOOGLE_MULTI="<?xml version='1.0' encoding='UTF-8'?>\n<kml xmlns='http://earth.google.com/kml/2.0'><Response>\n <name>via Sandro Pertini 8, Ossona, MI</name>\n <Status>\n <code>200</code>\n <request>geocode</request>\n </Status>\n <Placemark id='p1'>\n <address>Via Sandro Pertini, 8, 20010 Mesero MI, Italy</address>\n <AddressDetails Accuracy='8' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>IT</CountryNameCode><CountryName>Italy</CountryName><AdministrativeArea><AdministrativeAreaName>Lombardy</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Milan</SubAdministrativeAreaName><Locality><LocalityName>Mesero</LocalityName><Thoroughfare><ThoroughfareName>8 Via Sandro Pertini</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>20010</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>\n <Point><coordinates>8.8527131,45.4966243,0</coordinates></Point>\n </Placemark>\n <Placemark id='p2'>\n <address>Via Sandro Pertini, 20010 Ossona MI, Italy</address>\n <AddressDetails Accuracy='6' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>IT</CountryNameCode><CountryName>Italy</CountryName><AdministrativeArea><AdministrativeAreaName>Lombardy</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Milan</SubAdministrativeAreaName><Locality><LocalityName>Ossona</LocalityName><Thoroughfare><ThoroughfareName>Via Sandro Pertini</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>20010</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>\n <Point><coordinates>8.9023200,45.5074444,0</coordinates></Point>\n </Placemark>\n</Response></kml>\n"
21
-
22
- GOOGLE_REVERSE_MADRID="<?xml version='1.0' encoding='UTF-8' ?><kml xmlns='http://earth.google.com/kml/2.0'><Response><name>40.416741,-3.703250</name><Status><code>200</code><request>geocode</request></Status><Placemark id='p1'><address>Plaza de la Puerta del Sol, 28013, Madrid, Spain</address><AddressDetails Accuracy='6' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>ES</CountryNameCode><CountryName>Spain</CountryName><AdministrativeArea><AdministrativeAreaName>Madrid</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Madrid</SubAdministrativeAreaName><Locality><LocalityName>Madrid</LocalityName><Thoroughfare><ThoroughfareName>Plaza de la Puerta del Sol</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>28013</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north='40.4199522' south='40.4136570' east='-3.7001138' west='-3.7064091' /></ExtendedData><Point><coordinates>-3.7032537,40.4168023,0</coordinates></Point></Placemark><Placemark id='p2'><address>28013, Madrid, Spain</address><AddressDetails Accuracy='5' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>ES</CountryNameCode><CountryName>Spain</CountryName><AdministrativeArea><AdministrativeAreaName>Madrid</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Madrid</SubAdministrativeAreaName><Locality><LocalityName>Madrid</LocalityName><PostalCode><PostalCodeNumber>28013</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north='40.4244113' south='40.4142840' east='-3.6969862' west='-3.7224820' /></ExtendedData><Point><coordinates>-3.7117806,40.4189645,0</coordinates></Point></Placemark><Placemark id='p3'><address>Madrid, Spain</address><AddressDetails Accuracy='4' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>ES</CountryNameCode><CountryName>Spain</CountryName><AdministrativeArea><AdministrativeAreaName>Madrid</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Madrid</SubAdministrativeAreaName><Locality><LocalityName>Madrid</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north='40.6435181' south='40.3120713' east='-3.5180102' west='-3.8890049' /></ExtendedData><Point><coordinates>-3.7032498,40.4167413,0</coordinates></Point></Placemark><Placemark id='p4'><address>Madrid, Spain</address><AddressDetails Accuracy='2' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>ES</CountryNameCode><CountryName>Spain</CountryName><AdministrativeArea><AdministrativeAreaName>Madrid</AdministrativeAreaName></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north='41.1649106' south='39.8845366' east='-3.0531322' west='-4.5791745' /></ExtendedData><Point><coordinates>-3.5812692,40.4167088,0</coordinates></Point></Placemark><Placemark id='p5'><address>Madrid, Spain</address><AddressDetails Accuracy='3' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>ES</CountryNameCode><CountryName>Spain</CountryName><AdministrativeArea><AdministrativeAreaName>Madrid</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Madrid</SubAdministrativeAreaName></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north='41.1649106' south='39.8845366' east='-3.0531322' west='-4.5791745' /></ExtendedData><Point><coordinates>-3.5812692,40.4167088,0</coordinates></Point></Placemark><Placemark id='p6'><address>Spain</address><AddressDetails Accuracy='1' xmlns='urn:oasis:names:tc:ciq:xsdschema:xAL:2.0'><Country><CountryNameCode>ES</CountryNameCode><CountryName>Spain</CountryName></Country></AddressDetails><ExtendedData><LatLonBox north='43.7903881' south='27.6377504' east='4.3279851' west='-18.1606948' /></ExtendedData><Point><coordinates>-3.7492200,40.4636670,0</coordinates></Point></Placemark></Response></kml>"
23
-
24
- GOOGLE_COUNTRY_CODE_BIASED_RESULT = <<-EOF.strip
25
- <?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>Syracuse</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>Syracuse, Italy</address><AddressDetails Accuracy="3" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>IT</CountryNameCode><CountryName>Italy</CountryName><AdministrativeArea><AdministrativeAreaName>Sicily</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Syracuse</SubAdministrativeAreaName></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="37.4125978" south="36.6441736" east="15.3367367" west="14.7724913" /></ExtendedData><Point><coordinates>14.9856176,37.0630218,0</coordinates></Point></Placemark></Response></kml>
26
- EOF
27
-
28
- GOOGLE_BOUNDS_BIASED_RESULT = <<-EOF.strip
29
- <?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>Winnetka</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>Winnetka, California, USA</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><AddressLine>Winnetka</AddressLine></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="34.2353090" south="34.1791050" east="-118.5534191" west="-118.5883200" /></ExtendedData><Point><coordinates>-118.5710220,34.2131710,0</coordinates></Point></Placemark></Response></kml>
30
- EOF
31
-
32
- GOOGLE_TOO_MANY=<<-EOF.strip
33
- <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>100 spear st, san francisco, ca</name><Status><code>620</code><request>geocode</request></Status></Response></kml>
34
- EOF
35
-
36
- GOOGLE_UTF8_RESULT=<<-EOF
37
- <?xml version="1.0" encoding="UTF-8" ?>
38
- <kml xmlns="http://earth.google.com/kml/2.0"><Response>
39
- <name>Mosjøen, Norway</name>
40
- <Status>
41
- <code>200</code>
42
- <request>geocode</request>
43
- </Status>
44
- <Placemark id="p1">
45
- <address>Mosjøen, Norway</address>
46
- <AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>NO</CountryNameCode><CountryName>Norge</CountryName><AdministrativeArea><AdministrativeAreaName>Nordland</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Vefsn</SubAdministrativeAreaName><Locality><LocalityName>Mosjøen</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
47
- <ExtendedData>
48
- <LatLonBox north="65.8551869" south="65.8186481" east="13.2574307" west="13.1293713" />
49
- </ExtendedData>
50
- <Point><coordinates>13.1934010,65.8369240,0</coordinates></Point>
51
- </Placemark>
52
- </Response></kml>
53
- EOF
54
-
55
10
  def setup
56
11
  super
57
- @google_full_hash = {:street_address=>"100 Spear St", :city=>"San Francisco", :state=>"CA", :zip=>"94105", :country_code=>"US"}
12
+ @full_address = '100 Spear St Apt. 5, San Francisco, CA, 94105-1522, US'
13
+ @full_address_short_zip = '100 Spear St Apt. 5, San Francisco, CA, 94105, US'
14
+ @google_full_hash = {:street_address=>"100 Spear St Apt. 5", :city=>"San Francisco", :state=>"CA", :zip=>"94105", :country_code=>"US"}
58
15
  @google_city_hash = {:city=>"San Francisco", :state=>"CA"}
59
16
 
60
17
  @google_full_loc = Geokit::GeoLoc.new(@google_full_hash)
61
18
  @google_city_loc = Geokit::GeoLoc.new(@google_city_hash)
62
-
63
- @address_with_special_characters = "Mosjøen, Norway"
64
19
  end
65
20
 
66
- def test_google_full_address
67
- response = MockSuccess.new
68
- response.expects(:body).returns(GOOGLE_FULL)
69
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8"
70
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
71
- res=Geokit::Geocoders::GoogleGeocoder.geocode(@address)
72
- assert_equal "CA", res.state
73
- assert_equal "San Francisco", res.city
74
- assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo
75
- assert res.is_us?
76
- assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
77
- assert_equal "google", res.provider
78
- end
79
21
 
80
- def test_google_full_address_with_geo_loc
81
- response = MockSuccess.new
82
- response.expects(:body).returns(GOOGLE_FULL)
83
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8"
84
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
85
- res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
86
- assert_equal "CA", res.state
87
- assert_equal "San Francisco", res.city
88
- assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo
89
- assert res.is_us?
90
- assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
91
- assert_equal "google", res.provider
22
+ # Example from:
23
+ # https://developers.google.com/maps/documentation/business/webservices#signature_examples
24
+ def test_google_signature
25
+ cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
26
+ query_string = '/maps/api/geocode/json?address=New+York&sensor=false&client=clientID'
27
+ signature = Geokit::Geocoders::GoogleGeocoder.send(:sign_gmap_bus_api_url, query_string, cryptographic_key)
28
+ assert_equal 'KrU1TzVQM7Ur0i8i7K3huiw3MsA=', signature
92
29
  end
93
30
 
94
- def test_google_full_address_accuracy
95
- response = MockSuccess.new
96
- response.expects(:body).returns(GOOGLE_FULL)
97
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8"
98
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
99
- res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
100
- assert_equal 8, res.accuracy
101
- end
102
31
 
103
- def test_google_city
104
- response = MockSuccess.new
105
- response.expects(:body).returns(GOOGLE_CITY)
106
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8"
107
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
108
- res=Geokit::Geocoders::GoogleGeocoder.geocode(@address)
109
- assert_equal "CA", res.state
110
- assert_equal "San Francisco", res.city
111
- assert_equal "37.775,-122.418333", res.ll
112
- assert res.is_us?
113
- assert_equal "San Francisco, CA, USA", res.full_address
114
- assert_nil res.street_address
115
- assert_equal "google", res.provider
32
+ # Example from:
33
+ # https://developers.google.com/maps/documentation/business/webservices#signature_examples
34
+ def test_google_signature_and_url
35
+ Geokit::Geocoders::google_client_id = 'clientID'
36
+ Geokit::Geocoders::google_cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
37
+ url = Geokit::Geocoders::GoogleGeocoder.send(:submit_url, '/maps/api/geocode/json?address=New+York&sensor=false')
38
+ Geokit::Geocoders::google_client_id = nil
39
+ Geokit::Geocoders::google_cryptographic_key = nil
40
+ assert_equal 'http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=', url
116
41
  end
117
42
 
118
- def test_google_city_accuracy
119
- response = MockSuccess.new
120
- response.expects(:body).returns(GOOGLE_CITY)
121
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8"
122
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
43
+ def test_google_full_address
44
+ VCR.use_cassette('google_full_short') do
45
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
46
+ TestHelper.expects(:last_url).with(url)
123
47
  res=Geokit::Geocoders::GoogleGeocoder.geocode(@address)
124
- assert_equal 4, res.accuracy
125
- end
126
-
127
- def test_google_city_with_geo_loc
128
- response = MockSuccess.new
129
- response.expects(:body).returns(GOOGLE_CITY)
130
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8"
131
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
132
- res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc)
133
48
  assert_equal "CA", res.state
134
49
  assert_equal "San Francisco", res.city
135
- assert_equal "37.775,-122.418333", res.ll
50
+ assert_array_in_delta [37.7749295, -122.4194155], res.to_a # slightly dif from yahoo
136
51
  assert res.is_us?
137
- assert_equal "San Francisco, CA, USA", res.full_address
138
- assert_nil res.street_address
52
+ assert_equal "San Francisco, CA, USA", res.full_address #slightly different from yahoo
139
53
  assert_equal "google", res.provider
54
+ end
140
55
  end
141
56
 
142
- def test_google_suggested_bounds
143
- response = MockSuccess.new
144
- response.expects(:body).returns(GOOGLE_RESULT_WITH_SUGGESTED_BOUNDS)
145
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8"
146
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
147
- res = Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
148
-
149
- assert_instance_of Geokit::Bounds, res.suggested_bounds
150
- assert_equal Geokit::Bounds.new(Geokit::LatLng.new(37.7893376, -122.3971525), Geokit::LatLng.new(37.7956328, -122.3908573)), res.suggested_bounds
151
- end
152
-
153
- def test_service_unavailable
154
- response = MockFailure.new
155
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8"
156
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
157
- assert !Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc).success
158
- end
159
-
160
- def test_multiple_results
161
- #Geokit::Geocoders::GoogleGeocoder.do_geocode('via Sandro Pertini 8, Ossona, MI')
162
- response = MockSuccess.new
163
- response.expects(:body).returns(GOOGLE_MULTI)
164
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape('via Sandro Pertini 8, Ossona, MI')}&output=xml&key=Google&oe=utf-8"
165
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
166
- res=Geokit::Geocoders::GoogleGeocoder.geocode('via Sandro Pertini 8, Ossona, MI')
167
- assert_equal "Lombardy", res.state
168
- assert_equal "Mesero", res.city
169
- assert_equal "45.4966243,8.8527131", res.ll
170
- assert !res.is_us?
171
- assert_equal "Via Sandro Pertini, 8, 20010 Mesero MI, Italy", res.full_address
172
- assert_equal "8 Via Sandro Pertini", res.street_address
173
- assert_equal "google", res.provider
174
-
175
- assert_equal 2, res.all.size
176
- res = res.all[1]
177
- assert_equal "Lombardy", res.state
178
- assert_equal "Ossona", res.city
179
- assert_equal "45.5074444,8.90232", res.ll
180
- assert !res.is_us?
181
- assert_equal "Via Sandro Pertini, 20010 Ossona MI, Italy", res.full_address
182
- assert_equal "Via Sandro Pertini", res.street_address
183
- assert_equal "google", res.provider
184
- end
185
-
186
- def test_reverse_geocode
187
- #Geokit::Geocoders::GoogleGeocoder.do_reverse_geocode("40.4167413, -3.7032498")
188
- madrid = Geokit::GeoLoc.new
189
- madrid.lat, madrid.lng = "40.4167413", "-3.7032498"
190
- response = MockSuccess.new
191
- response.expects(:body).returns(GOOGLE_REVERSE_MADRID)
192
- url = "http://maps.google.com/maps/geo?ll=#{Geokit::Inflector::url_escape(madrid.ll)}&output=xml&key=#{Geokit::Geocoders::google}&oe=utf-8"
193
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).
194
- returns(response)
195
- res=Geokit::Geocoders::GoogleGeocoder.do_reverse_geocode(madrid.ll)
196
-
197
- assert_equal madrid.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5)
198
- assert_equal madrid.lng.to_s.slice(1..5), res.lng.to_s.slice(1..5)
199
- assert_equal "ES", res.country_code
200
- assert_equal "google", res.provider
201
-
202
- assert_equal "Madrid", res.city
203
- assert_equal "Madrid", res.state
204
-
205
- assert_equal "Spain", res.country
206
- assert_equal "zip+4", res.precision
207
- assert_equal true, res.success
208
-
209
- assert_equal "Plaza de la Puerta del Sol, 28013, Madrid, Spain", res.full_address
210
- assert_equal "28013", res.zip
211
- assert_equal "Plaza de la Puerta del Sol", res.street_address
212
- end
213
-
214
- def test_country_code_biasing
215
- response = MockSuccess.new
216
- response.expects(:body).returns(GOOGLE_COUNTRY_CODE_BIASED_RESULT)
217
-
218
- url = "http://maps.google.com/maps/geo?q=Syracuse&output=xml&gl=it&key=Google&oe=utf-8"
219
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
220
- biased_result = Geokit::Geocoders::GoogleGeocoder.geocode('Syracuse', :bias => 'it')
221
-
222
- assert_equal 'IT', biased_result.country_code
223
- assert_equal 'Sicily', biased_result.state
224
- end
225
-
226
- def test_bounds_biasing
227
- response = MockSuccess.new
228
- response.expects(:body).returns(GOOGLE_BOUNDS_BIASED_RESULT)
229
-
230
- url = "http://maps.google.com/maps/geo?q=Winnetka&output=xml&ll=34.197693,-118.547160&spn=0.247048,0.294914&key=Google&oe=utf-8"
231
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
232
-
233
- bounds = Geokit::Bounds.normalize([34.074081, -118.694401], [34.321129, -118.399487])
234
- biased_result = Geokit::Geocoders::GoogleGeocoder.geocode('Winnetka', :bias => bounds)
235
-
236
- assert_equal 'US', biased_result.country_code
237
- assert_equal 'CA', biased_result.state
57
+ def test_google_full_address_with_geo_loc
58
+ VCR.use_cassette('google_full') do
59
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
60
+ TestHelper.expects(:last_url).with(url)
61
+ res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
62
+ assert_equal "CA", res.state
63
+ assert_equal "San Francisco", res.city
64
+ assert_array_in_delta [37.7921509, -122.394], res.to_a # slightly dif from yahoo
65
+ assert res.is_us?
66
+ assert_equal "100 Spear Street #5, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
67
+ assert_equal "google", res.provider
68
+ end
69
+ end
70
+
71
+ def test_google_full_address_accuracy
72
+ VCR.use_cassette('google_full') do
73
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
74
+ TestHelper.expects(:last_url).with(url)
75
+ res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
76
+
77
+ assert_equal 9, res.accuracy
78
+ end
79
+ end
80
+
81
+ def test_google_city
82
+ VCR.use_cassette('google_city') do
83
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
84
+ TestHelper.expects(:last_url).with(url)
85
+ res=Geokit::Geocoders::GoogleGeocoder.do_geocode(@address)
86
+ assert_nil res.street_address
87
+ assert_equal "CA", res.state
88
+ assert_equal "San Francisco", res.city
89
+ assert_equal "37.7749295,-122.4194155", res.ll
90
+ assert res.is_us?
91
+ assert_equal "San Francisco, CA, USA", res.full_address
92
+ assert_equal "google", res.provider
93
+ end
94
+ end
95
+
96
+ def test_google_city_accuracy
97
+ VCR.use_cassette('google_city') do
98
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
99
+ TestHelper.expects(:last_url).with(url)
100
+ res=Geokit::Geocoders::GoogleGeocoder.geocode(@address)
101
+ assert_equal 4, res.accuracy
102
+ end
103
+ end
104
+
105
+ def test_google_city_with_geo_loc
106
+ VCR.use_cassette('google_city') do
107
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
108
+ TestHelper.expects(:last_url).with(url)
109
+ res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc)
110
+ assert_equal "CA", res.state
111
+ assert_equal "San Francisco", res.city
112
+ assert_equal "37.7749295,-122.4194155", res.ll
113
+ assert res.is_us?
114
+ assert_equal "San Francisco, CA, USA", res.full_address
115
+ assert_nil res.street_address
116
+ assert_equal "google", res.provider
117
+ end
118
+ end
119
+
120
+ def test_google_suggested_bounds
121
+ VCR.use_cassette('google_full') do
122
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
123
+ TestHelper.expects(:last_url).with(url)
124
+ res = Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
125
+ assert_instance_of Geokit::Bounds, res.suggested_bounds
126
+ assert_array_in_delta [37.7908019197085, -122.3953489802915], res.suggested_bounds.sw.to_a
127
+ assert_array_in_delta [37.7934998802915, -122.3926510197085], res.suggested_bounds.ne.to_a
128
+ end
129
+ end
130
+
131
+ def test_google_suggested_bounds_url
132
+ bounds = Geokit::Bounds.new(
133
+ Geokit::LatLng.new(33.7036917, -118.6681759),
134
+ Geokit::LatLng.new(34.3373061, -118.1552891)
135
+ )
136
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Winnetka&bounds=33.7036917%2C-118.6681759%7C34.3373061%2C-118.1552891"
137
+ Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url)
138
+ Geokit::Geocoders::GoogleGeocoder.geocode('Winnetka', :bias => bounds)
238
139
  end
239
140
 
240
- def test_too_many_queries
241
- response = MockSuccess.new
242
- response.expects(:body).returns(GOOGLE_TOO_MANY)
243
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8"
244
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
245
- assert_raise Geokit::TooManyQueriesError do
246
- res=Geokit::Geocoders::GoogleGeocoder.geocode(@address)
247
- end
248
- end
249
-
250
- def test_utf8_encoding
251
- response = MockSuccess.new
252
- body = GOOGLE_UTF8_RESULT
253
- body = body.force_encoding('ASCII-8BIT') if body.respond_to? :force_encoding
254
- response.expects(:body).returns(body)
255
- url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address_with_special_characters)}&output=xml&key=Google&oe=utf-8"
256
- Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
257
- res=Geokit::Geocoders::GoogleGeocoder.geocode(@address_with_special_characters)
258
- assert_equal "Nordland", res.state
259
- assert_equal "Mosjøen", res.city
260
- assert_equal "65.836924,13.193401", res.ll
261
- assert !res.is_us?
262
- assert_equal "google", res.provider
263
- end
141
+ def test_service_unavailable
142
+ response = MockFailure.new
143
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
144
+ Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
145
+ assert !Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc).success
146
+ end
147
+
148
+ def test_multiple_results
149
+ VCR.use_cassette('google_multi') do
150
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape('via Sandro Pertini 8, Ossona, MI')}"
151
+ TestHelper.expects(:last_url).with(url)
152
+ res=Geokit::Geocoders::GoogleGeocoder.geocode('via Sandro Pertini 8, Ossona, MI')
153
+ assert_equal 5, res.all.size
154
+ res = res.all[0]
155
+ assert_equal "Lombardy", res.state
156
+ assert_equal "Mesero", res.city
157
+ assert_array_in_delta [45.4966218, 8.852694], res.to_a
158
+ assert !res.is_us?
159
+ assert_equal "Via Sandro Pertini, 8, 20010 Mesero Milan, Italy", res.full_address
160
+ assert_equal "8 Via Sandro Pertini", res.street_address
161
+ assert_equal "google", res.provider
162
+
163
+ res = res.all[4]
164
+ assert_equal "Lombardy", res.state
165
+ assert_equal "Ossona", res.city
166
+ assert_array_in_delta [45.5074444, 8.90232], res.to_a
167
+ assert !res.is_us?
168
+ assert_equal "Via S. Pertini, 20010 Ossona Milan, Italy", res.full_address
169
+ assert_equal "Via S. Pertini", res.street_address
170
+ assert_equal "google", res.provider
171
+ end
172
+ end
173
+ #
174
+ def test_reverse_geocode
175
+ VCR.use_cassette('google_reverse_madrid') do
176
+ madrid = Geokit::GeoLoc.new
177
+ madrid.lat, madrid.lng = "40.4167413", "-3.7032498"
178
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=#{Geokit::Inflector::url_escape(madrid.ll)}"
179
+ TestHelper.expects(:last_url).with(url)
180
+ res=Geokit::Geocoders::GoogleGeocoder.do_reverse_geocode(madrid.ll)
181
+
182
+ assert_equal madrid.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5)
183
+ assert_equal madrid.lng.to_s.slice(1..5), res.lng.to_s.slice(1..5)
184
+ assert_equal "ES", res.country_code
185
+ assert_equal "google", res.provider
186
+
187
+ assert_equal "Madrid", res.city
188
+ assert_equal "Community of Madrid", res.state
189
+
190
+ assert_equal "Spain", res.country
191
+ assert_equal "28013", res.zip
192
+ assert_equal true, res.success
193
+ end
194
+ end
195
+
196
+ def test_country_code_biasing
197
+ VCR.use_cassette('google_country_code_biased_result') do
198
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Syracuse&region=it"
199
+ TestHelper.expects(:last_url).with(url)
200
+ biased_result = Geokit::Geocoders::GoogleGeocoder.geocode('Syracuse', :bias => 'it')
201
+
202
+ assert_equal 'IT', biased_result.country_code
203
+ assert_equal 'Sicilia', biased_result.state
204
+ end
205
+ end
206
+
207
+ def test_language_response
208
+ VCR.use_cassette('google_language_response_fr') do
209
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Hanoi&language=FR"
210
+ TestHelper.expects(:last_url).with(url)
211
+ language_result = Geokit::Geocoders::GoogleGeocoder.geocode('Hanoi', :language => 'FR')
212
+
213
+ assert_equal 'VN', language_result.country_code
214
+ assert_equal 'Hanoï', language_result.city
215
+ end
216
+ end
217
+
218
+ def test_too_many_queries
219
+ response = MockSuccess.new
220
+ response.expects(:body).returns %q/{"status": "OVER_QUERY_LIMIT"}/
221
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
222
+ Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
223
+ assert_raise Geokit::Geocoders::TooManyQueriesError do
224
+ res=Geokit::Geocoders::GoogleGeocoder.geocode(@address)
225
+ end
226
+ end
227
+
228
+ def test_invalid_request
229
+ response = MockSuccess.new
230
+ response.expects(:body).returns %q/{"results" : [], "status" : "INVALID_REQUEST"}/
231
+ url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape("3961 V\u00EDa Marisol")}"
232
+ Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
233
+ assert_raise Geokit::Geocoders::GeocodeError do
234
+ Geokit::Geocoders::GoogleGeocoder.geocode("3961 V\u00EDa Marisol")
235
+ end
236
+ end
264
237
  end