geokit 1.10.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.hound.yml +2 -0
  4. data/.rubocop.yml +358 -0
  5. data/.travis.yml +4 -4
  6. data/CHANGELOG.md +16 -0
  7. data/Gemfile +3 -3
  8. data/README.markdown +19 -4
  9. data/Rakefile +11 -16
  10. data/fixtures/keys.yml +9 -0
  11. data/fixtures/vcr_cassettes/google_postal_town.yml +117 -0
  12. data/fixtures/vcr_cassettes/mapbox_forward_geocode.yml +30 -30
  13. data/fixtures/vcr_cassettes/mapbox_forward_geocode_city_only.yml +25 -25
  14. data/fixtures/vcr_cassettes/mapbox_forward_geocode_state_only.yml +71 -0
  15. data/fixtures/vcr_cassettes/mapbox_reverse_geocode.yml +25 -25
  16. data/fixtures/vcr_cassettes/test_component_filtering_off.yml +390 -0
  17. data/fixtures/vcr_cassettes/test_component_filtering_on.yml +164 -0
  18. data/fixtures/vcr_cassettes/test_component_filtering_on_without_filter.yml +404 -0
  19. data/geokit.gemspec +24 -23
  20. data/lib/geokit.rb +7 -7
  21. data/lib/geokit/core_ext.rb +1 -1
  22. data/lib/geokit/geo_loc.rb +25 -19
  23. data/lib/geokit/geocoders.rb +21 -30
  24. data/lib/geokit/geocoders/bing.rb +5 -4
  25. data/lib/geokit/geocoders/ca_geocoder.rb +10 -11
  26. data/lib/geokit/geocoders/fcc.rb +9 -9
  27. data/lib/geokit/geocoders/free_geo_ip.rb +8 -8
  28. data/lib/geokit/geocoders/geo_plugin.rb +7 -7
  29. data/lib/geokit/geocoders/geobytes.rb +10 -10
  30. data/lib/geokit/geocoders/geocodio.rb +14 -14
  31. data/lib/geokit/geocoders/geonames.rb +12 -12
  32. data/lib/geokit/geocoders/google.rb +89 -61
  33. data/lib/geokit/geocoders/ip.rb +9 -14
  34. data/lib/geokit/geocoders/mapbox.rb +30 -30
  35. data/lib/geokit/geocoders/mapquest.rb +12 -12
  36. data/lib/geokit/geocoders/maxmind.rb +1 -1
  37. data/lib/geokit/geocoders/opencage.rb +19 -19
  38. data/lib/geokit/geocoders/openstreetmap.rb +21 -19
  39. data/lib/geokit/geocoders/ripe.rb +7 -7
  40. data/lib/geokit/geocoders/us_geocoder.rb +5 -5
  41. data/lib/geokit/geocoders/yahoo.rb +46 -46
  42. data/lib/geokit/geocoders/yandex.rb +18 -17
  43. data/lib/geokit/inflectors.rb +5 -5
  44. data/lib/geokit/lat_lng.rb +5 -4
  45. data/lib/geokit/multi_geocoder.rb +4 -2
  46. data/lib/geokit/net_adapter/net_http.rb +3 -2
  47. data/lib/geokit/net_adapter/typhoeus.rb +2 -1
  48. data/lib/geokit/version.rb +1 -1
  49. data/test/coverage_loader.rb +25 -0
  50. data/test/helper.rb +18 -87
  51. data/test/test_base_geocoder.rb +44 -11
  52. data/test/test_bing_geocoder.rb +40 -48
  53. data/test/test_bounds.rb +1 -1
  54. data/test/test_ca_geocoder.rb +15 -15
  55. data/test/test_fcc_geocoder.rb +8 -9
  56. data/test/test_free_geo_ip_geocoder.rb +8 -10
  57. data/test/test_geo_plugin_geocoder.rb +21 -22
  58. data/test/test_geobytes_geocoder.rb +9 -11
  59. data/test/test_geocodio_geocoder.rb +12 -14
  60. data/test/test_geoloc.rb +48 -49
  61. data/test/test_geonames_geocoder.rb +19 -23
  62. data/test/test_google_geocoder.rb +197 -189
  63. data/test/test_inflector.rb +7 -7
  64. data/test/test_ipgeocoder.rb +32 -31
  65. data/test/test_latlng.rb +28 -28
  66. data/test/test_map_quest.rb +23 -27
  67. data/test/test_mapbox_geocoder.rb +38 -28
  68. data/test/test_mappable.rb +2 -2
  69. data/test/test_maxmind_geocoder.rb +16 -16
  70. data/test/test_multi_geocoder.rb +5 -5
  71. data/test/test_multi_ip_geocoder.rb +3 -3
  72. data/test/test_net_adapter.rb +4 -4
  73. data/test/test_opencage_geocoder.rb +58 -67
  74. data/test/test_openstreetmap_geocoder.rb +67 -65
  75. data/test/test_polygon.rb +4 -22
  76. data/test/test_ripe_geocoder.rb +21 -26
  77. data/test/test_us_geocoder.rb +21 -21
  78. data/test/test_useragent.rb +46 -0
  79. data/test/test_yahoo_geocoder.rb +35 -47
  80. data/test/test_yandex_geocoder.rb +29 -27
  81. data/test/vcr_loader.rb +18 -0
  82. metadata +20 -6
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- require File.join(File.dirname(__FILE__), "helper")
2
+ require File.join(File.dirname(__FILE__), 'helper')
3
3
 
4
4
  class PolygonTest < Test::Unit::TestCase #:nodoc: all
5
5
  def setup
@@ -53,30 +53,20 @@ class PolygonTest < Test::Unit::TestCase #:nodoc: all
53
53
  end
54
54
 
55
55
  def test_point_inside_poly
56
- # puts "\n\nTesting point inside poly... {@polygon.contains?(@point_inside)}\n\n"
57
56
  assert @polygon.contains?(@point_inside)
58
57
  end
59
58
 
60
59
  def test_point_outside_poly
61
- # puts "\n\nTesting point outside poly... {@polygon.contains?(@point_outside)}\n\n"
62
60
  assert !@polygon.contains?(@point_outside)
63
61
  end
64
62
 
65
63
  def test_points_inside_complex_poly
66
- # puts "\n\nTesting points INSIDE complex poly..."
67
- # puts "\tone: {@complex_polygon.contains?(@complex_inside_one)}"
68
- # puts "\ttwo: {@complex_polygon.contains?(@complex_inside_two)}"
69
- # puts "\tthree: {@complex_polygon.contains?(@complex_inside_three)}\n\n"
70
64
  assert @complex_polygon.contains?(@complex_inside_one)
71
65
  assert @complex_polygon.contains?(@complex_inside_two)
72
66
  assert @complex_polygon.contains?(@complex_inside_three)
73
67
  end
74
68
 
75
69
  def test_points_outside_complex_poly
76
- # puts "\n\nTesting points OUTSIDE complex poly..."
77
- # puts "\tone: {@complex_polygon.contains?(@complex_outside_one)}"
78
- # puts "\ttwo: {@complex_polygon.contains?(@complex_outside_two)}"
79
- # puts "\tthree: {@complex_polygon.contains?(@complex_outside_three)}\n\n"
80
70
  assert !@complex_polygon.contains?(@complex_outside_one)
81
71
  assert !@complex_polygon.contains?(@complex_outside_two)
82
72
  assert !@complex_polygon.contains?(@complex_outside_three)
@@ -85,27 +75,19 @@ class PolygonTest < Test::Unit::TestCase #:nodoc: all
85
75
  def test_open_polygon
86
76
  # A polygon can only exist of the last point is equal to the first
87
77
  # Otherwise, it would just be a line of points.
88
-
89
- # puts "\n\nTesting intialize function to close an open polygon..."
90
- # puts "\t Does poly_x[0] (#{@open_polygon.poly_x[0]}) == poly_x[-1] (#{@open_polygon.poly_x[-1]}) ?"
91
- # puts "\t Does poly_y[0] (#{@open_polygon.poly_y[0]}) == poly_y[-1] (#{@open_polygon.poly_y[-1]}) ?"
92
-
93
78
  assert @open_polygon.points[0].lng == @open_polygon.points[-1].lng
94
79
  assert @open_polygon.points[0].lat == @open_polygon.points[-1].lat
95
80
  end
96
81
 
97
82
  def test_centroid_for_simple_poly
98
- @polygon_centroid = Geokit::LatLng.new(45.27463866133501, -93.41400121829719)
99
- assert_equal(@polygon.centroid, @polygon_centroid)
83
+ assert_ll @polygon.centroid, 45.27463866133501, -93.41400121829719
100
84
  end
101
85
 
102
86
  def test_centroid_for_complex_poly
103
- @complex_polygon_centroid = Geokit::LatLng.new(45.43622702936517, -93.5352210389731)
104
- assert_equal(@complex_polygon.centroid, @complex_polygon_centroid)
87
+ assert_ll @complex_polygon.centroid, 45.43622702936517, -93.5352210389731
105
88
  end
106
89
 
107
90
  def test_centroid_for_open_poly
108
- @open_polygon_centroid = Geokit::LatLng.new(44.95912726688109, -92.7068888186181)
109
- assert_equal(@open_polygon.centroid, @open_polygon_centroid)
91
+ assert_ll @open_polygon.centroid, 44.95912726688109, -92.7068888186181
110
92
  end
111
93
  end
@@ -1,44 +1,39 @@
1
- require File.join(File.dirname(__FILE__), "helper")
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
2
 
3
3
  class RipeGeocoderTest < BaseGeocoderTest #:nodoc: all
4
4
  def setup
5
5
  super
6
- @ip = "74.125.237.209"
7
- @ip_au = "118.210.24.54"
6
+ @ip = '74.125.237.209'
7
+ @ip_au = '118.210.24.54'
8
+ @base_url = 'http://stat.ripe.net/data/geoloc/data.json'
8
9
  end
9
10
 
10
11
  def assert_url(expected_url)
11
- assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, "").gsub("%20", "+")
12
+ assert_equal expected_url, TestHelper.last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
12
13
  end
13
14
 
14
15
  def test_45
15
- VCR.use_cassette("ripe_geocode_45") do
16
- res = Geokit::Geocoders::RipeGeocoder.geocode("45.45.45.45")
17
- assert !res.success
18
- end
16
+ res = geocode('45.45.45.45', :ripe_geocode_45)
17
+ assert !res.success
19
18
  end
20
19
 
21
20
  def test_ripe_geocode
22
- VCR.use_cassette("ripe_geocode") do
23
- url = "http://stat.ripe.net/data/geoloc/data.json?resource=#{@ip}"
24
- res = Geokit::Geocoders::RipeGeocoder.geocode(@ip)
25
- assert_url url
26
- assert_equal res.city, "Mountain View"
27
- assert_equal res.state, "CA"
28
- assert_equal res.state_code, "CA"
29
- assert_equal res.country_code, "US"
30
- end
21
+ url = "#{@base_url}?resource=#{@ip}"
22
+ res = geocode(@ip, :ripe_geocode)
23
+ assert_url url
24
+ assert_equal res.city, 'Mountain View'
25
+ assert_equal res.state, 'CA'
26
+ assert_equal res.state_code, 'CA'
27
+ assert_equal res.country_code, 'US'
31
28
  end
32
29
 
33
30
  def test_ripe_geocode_au
34
- VCR.use_cassette("ripe_geocode_au") do
35
- url = "http://stat.ripe.net/data/geoloc/data.json?resource=#{@ip_au}"
36
- res = Geokit::Geocoders::RipeGeocoder.geocode(@ip_au)
37
- assert_url url
38
- assert_equal res.city, "Adelaide"
39
- assert_equal res.state, nil
40
- assert_equal res.state_code, nil
41
- assert_equal res.country_code, "AU"
42
- end
31
+ url = "#{@base_url}?resource=#{@ip_au}"
32
+ res = geocode(@ip_au, :ripe_geocode_au)
33
+ assert_url url
34
+ assert_equal res.city, 'Adelaide'
35
+ assert_equal res.state, nil
36
+ assert_equal res.state_code, nil
37
+ assert_equal res.country_code, 'AU'
43
38
  end
44
39
  end
@@ -1,55 +1,55 @@
1
- require File.join(File.dirname(__FILE__), "helper")
2
-
3
- Geokit::Geocoders::UsGeocoder.key = nil
1
+ require File.join(File.dirname(__FILE__), 'helper')
4
2
 
5
3
  class UsGeocoderTest < BaseGeocoderTest #:nodoc: all
6
- GEOCODER_US_FULL = "37.792528,-122.393981,100 Spear St,San Francisco,CA,94105"
4
+ GEOCODER_US_FULL = '37.792528,-122.393981,100 Spear St,San Francisco,CA,94105'
7
5
 
8
6
  def setup
7
+ geocoder_class.key = nil
9
8
  super
10
- @us_full_hash = {city: "San Francisco", state: "CA"}
9
+ @us_full_hash = {city: 'San Francisco', state: 'CA'}
11
10
  @us_full_loc = Geokit::GeoLoc.new(@us_full_hash)
11
+ @base_url = 'http://geocoder.us/service/csv/geocode'
12
12
  end
13
13
 
14
14
  def test_geocoder_us
15
15
  response = MockSuccess.new
16
16
  response.expects(:body).returns(GEOCODER_US_FULL)
17
- url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
18
- Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
19
- verify(Geokit::Geocoders::UsGeocoder.geocode(@address))
17
+ url = "#{@base_url}?address=#{escape(@address)}"
18
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
19
+ verify(geocode(@address))
20
20
  end
21
21
 
22
22
  def test_geocoder_with_geo_loc
23
23
  response = MockSuccess.new
24
24
  response.expects(:body).returns(GEOCODER_US_FULL)
25
- url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
26
- Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
27
- verify(Geokit::Geocoders::UsGeocoder.geocode(@us_full_loc))
25
+ url = "#{@base_url}?address=#{escape(@address)}"
26
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
27
+ verify(geocode(@us_full_loc))
28
28
  end
29
29
 
30
30
  def test_service_unavailable
31
31
  response = MockFailure.new
32
- url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
33
- Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
34
- assert !Geokit::Geocoders::UsGeocoder.geocode(@us_full_loc).success
32
+ url = "#{@base_url}?address=#{escape(@address)}"
33
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
34
+ assert !geocode(@us_full_loc).success
35
35
  end
36
36
 
37
37
  def test_all_method
38
38
  response = MockSuccess.new
39
39
  response.expects(:body).returns(GEOCODER_US_FULL)
40
- url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
41
- Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
42
- res = Geokit::Geocoders::UsGeocoder.geocode(@address)
40
+ url = "#{@base_url}?address=#{escape(@address)}"
41
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
42
+ res = geocode(@address)
43
43
  assert_equal 1, res.all.size
44
44
  end
45
45
 
46
46
  private
47
47
 
48
48
  def verify(location)
49
- assert_equal "CA", location.state
50
- assert_equal "San Francisco", location.city
51
- assert_equal "37.792528,-122.393981", location.ll
49
+ assert_equal 'CA', location.state
50
+ assert_equal 'San Francisco', location.city
51
+ assert_equal '37.792528,-122.393981', location.ll
52
52
  assert location.is_us?
53
- assert_equal "100 Spear St, San Francisco, CA, 94105, US", location.full_address # slightly different from yahoo
53
+ assert_equal '100 Spear St, San Francisco, CA, 94105, US', location.full_address # slightly different from yahoo
54
54
  end
55
55
  end
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require 'webmock/test_unit'
3
+
4
+ require File.join(File.dirname(__FILE__), '../lib/geokit.rb')
5
+
6
+ class UserAgentTest < Test::Unit::TestCase
7
+
8
+ NETHTTPDEFAULT = 'Ruby'
9
+ NETHTTPDEFAULTHEADERS = {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'}
10
+ TYPHOEUSDEFAULT = 'Typhoeus - https://github.com/typhoeus/typhoeus'
11
+ TYPHOEUSDEFAULTHEADERS = {}
12
+ TESTAGENT = 'MyAgent'
13
+ URL = 'http://www.example.com'
14
+
15
+ def test_nethttp_useragent_set_to_testagent
16
+ stub_request(:get, URL).with(:headers => NETHTTPDEFAULTHEADERS.merge('User-Agent' => TESTAGENT))
17
+
18
+ Geokit::Geocoders::useragent = TESTAGENT
19
+ Geokit::NetAdapter::NetHttp.do_get(URL)
20
+ assert_requested :get, URL
21
+ end
22
+
23
+ def test_nethttp_useragent_set_to_default
24
+ stub_request(:get, URL).with(:headers => NETHTTPDEFAULTHEADERS.merge('User-Agent' => NETHTTPDEFAULT))
25
+
26
+ Geokit::Geocoders::useragent = nil
27
+ Geokit::NetAdapter::NetHttp.do_get(URL)
28
+ assert_requested :get, URL
29
+ end
30
+
31
+ def test_typhoeus_set_to_testagent
32
+ stub_request(:get, URL).with(:headers => TYPHOEUSDEFAULTHEADERS.merge('User-Agent' => TESTAGENT))
33
+
34
+ Geokit::Geocoders::useragent = TESTAGENT
35
+ Geokit::NetAdapter::Typhoeus.do_get(URL)
36
+ assert_requested :get, URL
37
+ end
38
+
39
+ def test_typhoeus_set_to_default
40
+ stub_request(:get, URL).with(:headers => TYPHOEUSDEFAULTHEADERS.merge('User-Agent' => TYPHOEUSDEFAULT))
41
+
42
+ Geokit::Geocoders::useragent = nil
43
+ Geokit::NetAdapter::Typhoeus.do_get(URL)
44
+ assert_requested :get, URL
45
+ end
46
+ end
@@ -1,107 +1,95 @@
1
- require File.join(File.dirname(__FILE__), "helper")
2
-
3
- Geokit::Geocoders::YahooGeocoder.key = "dj0yJmk9cXByQVN2WHZmTVhDJmQ9WVdrOVZscG1WVWhOTldrbWNHbzlNakF6TlRJME16UTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0zNg--"
4
- Geokit::Geocoders::YahooGeocoder.secret = "SECRET"
1
+ require File.join(File.dirname(__FILE__), 'helper')
5
2
 
6
3
  class YahooGeocoderTest < BaseGeocoderTest #:nodoc: all
7
4
  def setup
8
5
  super
9
- @yahoo_full_hash = {street_address: "100 Spear St", city: "San Francisco", state: "CA", zip: "94105-1522", country_code: "US"}
10
- @yahoo_city_hash = {city: "San Francisco", state: "CA"}
6
+ @yahoo_full_hash = {street_address: '100 Spear St', city: 'San Francisco', state: 'CA', zip: '94105-1522', country_code: 'US'}
7
+ @yahoo_city_hash = {city: 'San Francisco', state: 'CA'}
11
8
  @yahoo_full_loc = Geokit::GeoLoc.new(@yahoo_full_hash)
12
9
  @yahoo_city_loc = Geokit::GeoLoc.new(@yahoo_city_hash)
10
+
11
+ key = @keys['yahoo']
12
+ geocoder_class.key = key['key']
13
+ geocoder_class.secret = key['secret']
14
+ @base_url = 'https://yboss.yahooapis.com/geo/placefinder'
13
15
  end
14
16
 
15
17
  def assert_yahoo_url(expected_url)
16
- assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, "").gsub("%20", "+")
18
+ assert_equal expected_url, TestHelper.last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
17
19
  end
18
20
 
19
21
  # the testing methods themselves
20
22
  def test_yahoo_full_address
21
- VCR.use_cassette("yahoo_full") do
22
- url = "https://yboss.yahooapis.com/geo/placefinder?flags=J&q=#{Geokit::Inflector.url_escape(@full_address)}"
23
- do_full_address_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@full_address))
23
+ url = "#{@base_url}?flags=J&q=#{escape(@full_address)}"
24
+ do_full_address_assertions(geocode(@full_address, :yahoo_full))
24
25
  assert_yahoo_url url
25
- end
26
26
  end
27
27
 
28
28
  def test_yahoo_full_address_accuracy
29
- VCR.use_cassette("yahoo_full") do
30
- url = "https://yboss.yahooapis.com/geo/placefinder?flags=J&q=#{Geokit::Inflector.url_escape(@full_address)}"
31
- res = Geokit::Geocoders::YahooGeocoder.geocode(@full_address)
29
+ url = "#{@base_url}?flags=J&q=#{escape(@full_address)}"
30
+ res = geocode(@full_address, :yahoo_full)
32
31
  assert_yahoo_url url
33
32
  assert_equal 8, res.accuracy
34
- end
35
33
  end
36
34
 
37
35
  def test_yahoo_full_address_with_geo_loc
38
- VCR.use_cassette("yahoo_full") do
39
- url = "https://yboss.yahooapis.com/geo/placefinder?flags=J&q=#{Geokit::Inflector.url_escape(@full_address)}"
40
- do_full_address_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_full_loc))
36
+ url = "#{@base_url}?flags=J&q=#{escape(@full_address)}"
37
+ do_full_address_assertions(geocode(@yahoo_full_loc, :yahoo_full))
41
38
  assert_yahoo_url url
42
- end
43
39
  end
44
40
 
45
41
  def test_yahoo_city
46
- VCR.use_cassette("yahoo_city") do
47
- url = "https://yboss.yahooapis.com/geo/placefinder?flags=J&q=#{Geokit::Inflector.url_escape(@address)}"
48
- do_city_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@address))
42
+ url = "#{@base_url}?flags=J&q=#{escape(@address)}"
43
+ do_city_assertions(geocode(@address, :yahoo_city))
49
44
  assert_yahoo_url url
50
- end
51
45
  end
52
46
 
53
47
  def test_yahoo_city_accuracy
54
- VCR.use_cassette("yahoo_city") do
55
- url = "https://yboss.yahooapis.com/geo/placefinder?flags=J&q=#{Geokit::Inflector.url_escape(@address)}"
56
- res = Geokit::Geocoders::YahooGeocoder.geocode(@address)
48
+ url = "#{@base_url}?flags=J&q=#{escape(@address)}"
49
+ res = geocode(@address, :yahoo_city)
57
50
  assert_yahoo_url url
58
51
  assert_equal 4, res.accuracy
59
- end
60
52
  end
61
53
 
62
54
  def test_yahoo_city_with_geo_loc
63
- VCR.use_cassette("yahoo_city") do
64
- url = "https://yboss.yahooapis.com/geo/placefinder?flags=J&q=#{Geokit::Inflector.url_escape(@address)}"
65
- do_city_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc))
55
+ url = "#{@base_url}?flags=J&q=#{escape(@address)}"
56
+ do_city_assertions(geocode(@yahoo_city_loc, :yahoo_city))
66
57
  assert_yahoo_url url
67
- end
68
58
  end
69
59
 
70
60
  def test_no_results
71
- no_results_address = "ZZ, ZZ, ZZ"
72
- VCR.use_cassette("yahoo_no_results") do
73
- url = "https://yboss.yahooapis.com/geo/placefinder?flags=J&q=#{Geokit::Inflector.url_escape(no_results_address)}"
74
- result = Geokit::Geocoders::YahooGeocoder.geocode(no_results_address)
61
+ no_results_address = 'ZZ, ZZ, ZZ'
62
+ url = "#{@base_url}?flags=J&q=#{escape(no_results_address)}"
63
+ result = geocode(no_results_address, :yahoo_no_results)
75
64
  assert_yahoo_url url
76
- assert_equal ",", result.ll
77
- end
65
+ assert_equal ',', result.ll
78
66
  end
79
67
 
80
68
  def test_service_unavailable
81
69
  response = MockFailure.new
82
- Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).returns(response)
83
- assert !Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc).success
70
+ geocoder_class.expects(:call_geocoder_service).returns(response)
71
+ assert !geocode(@yahoo_city_loc).success
84
72
  end
85
73
 
86
74
  private
87
75
 
88
76
  # next two methods do the assertions for both address-level and city-level lookups
89
77
  def do_full_address_assertions(res)
90
- assert_equal "CA", res.state
91
- assert_equal "San Francisco", res.city
78
+ assert_equal 'CA', res.state
79
+ assert_equal 'San Francisco', res.city
92
80
  assert_array_in_delta [37.792332, -122.393791], res.to_a
93
81
  assert res.is_us?
94
- assert_equal "100 Spear St, San Francisco, CA, 94105-1578, US", res.full_address
95
- assert_equal "yahoo", res.provider
82
+ assert_equal '100 Spear St, San Francisco, CA, 94105-1578, US', res.full_address
83
+ assert_equal 'yahoo', res.provider
96
84
  end
97
85
 
98
86
  def do_city_assertions(res)
99
- assert_equal "CA", res.state
100
- assert_equal "San Francisco", res.city
87
+ assert_equal 'CA', res.state
88
+ assert_equal 'San Francisco', res.city
101
89
  assert_array_in_delta [37.77713, -122.41964], res.to_a
102
90
  assert res.is_us?
103
- assert_equal "San Francisco, CA, US", res.full_address
91
+ assert_equal 'San Francisco, CA, US', res.full_address
104
92
  assert_nil res.street_address
105
- assert_equal "yahoo", res.provider
93
+ assert_equal 'yahoo', res.provider
106
94
  end
107
95
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
- require File.join(File.dirname(__FILE__), "helper")
2
+ require File.join(File.dirname(__FILE__), 'helper')
3
3
 
4
4
  class YandexGeocoderTest < BaseGeocoderTest #:nodoc: all
5
5
  YANDEX_FULL = <<-EOF.strip
@@ -7,7 +7,7 @@ class YandexGeocoderTest < BaseGeocoderTest #:nodoc: all
7
7
  EOF
8
8
 
9
9
  YANDEX_REGION = <<-EOF.strip
10
- {"response":{"GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"request":"Ростов-на-Дону, ул. Станиславского, д.21","found":"1","results":"10"}},"featureMember":[{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"house","text":"Россия, Ростовская область, город Ростов-на-Дону, улица Станиславского, 21","precision":"exact","AddressDetails":{"Country":{"AddressLine":"Ростовская область, город Ростов-на-Дону, улица Станиславского, 21","CountryNameCode":"RU","CountryName":"Россия","AdministrativeArea":{"AdministrativeAreaName":"Ростовская область","Locality":{"LocalityName":"город Ростов-на-Дону","Thoroughfare":{"ThoroughfareName":"улица Станиславского","Premise":{"PremiseNumber":"21"}}}}}}}},"description":"город Ростов-на-Дону, Ростовская область, Россия","name":"улица Станиславского, 21","boundedBy":{"Envelope":{"lowerCorner":"39.701349 47.214507","upperCorner":"39.705446 47.217298"}},"Point":{"pos":"39.703398 47.215903"}}}]}}}
10
+ {"response":{"GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"request":"Ростов-на-Дону, ул. Станиславского, д.21","found":"2","results":"10"}},"featureMember":[{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"house","text":"Россия, Ростов-на-Дону, улица Станиславского, 21","precision":"exact","AddressDetails":{"Country":{"AddressLine":"Ростов-на-Дону, улица Станиславского, 21","CountryNameCode":"RU","CountryName":"Россия","AdministrativeArea":{"AdministrativeAreaName":"Ростовская область","SubAdministrativeArea":{"SubAdministrativeAreaName":"городской округ Ростов-на-Дону","Locality":{"LocalityName":"Ростов-на-Дону","Thoroughfare":{"ThoroughfareName":"улица Станиславского","Premise":{"PremiseNumber":"21"}}}}}}}}},"description":"Ростов-на-Дону, Россия","name":"улица Станиславского, 21","boundedBy":{"Envelope":{"lowerCorner":"39.695043 47.210284","upperCorner":"39.7115 47.221497"}},"Point":{"pos":"39.703272 47.21589"}}}]}}}
11
11
  EOF
12
12
 
13
13
  YANDEX_CITY = <<-EOF.strip
@@ -22,69 +22,71 @@ class YandexGeocoderTest < BaseGeocoderTest #:nodoc: all
22
22
  super
23
23
  @full_address = "Москва, улица Новый Арбат, дом 24"
24
24
  @address = "город Москва"
25
+ @base_url = 'https://geocode-maps.yandex.ru/1.x'
25
26
  end
26
27
 
27
28
  # the testing methods themselves
28
29
  def test_yandex_full_address
29
30
  response = MockSuccess.new
30
31
  response.expects(:body).returns(YANDEX_FULL)
31
- url = "https://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector.url_escape(@full_address)}&format=json"
32
- Geokit::Geocoders::YandexGeocoder.expects(:call_geocoder_service).with(url).returns(response)
33
- res = Geokit::Geocoders::YandexGeocoder.geocode(@full_address)
32
+ url = "#{@base_url}/?geocode=#{escape(@full_address)}&format=json"
33
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
34
+ res = geocode(@full_address)
34
35
 
35
- assert_equal "yandex", res.provider
36
+ assert_equal 'yandex', res.provider
36
37
  assert_equal "улица Новый Арбат, 24", res.street_address
37
38
  assert_equal "Москва", res.city
38
39
  assert_equal 55.753083, res.lat
39
40
  assert_equal 37.587614, res.lng
40
- assert_equal "RU", res.country_code
41
+ assert_equal 'RU', res.country_code
41
42
  assert res.success
42
43
  end
43
44
 
44
- def test_yandex_full_address_with_region
45
+ def test_yandex_full_address_with_region_and_district
45
46
  region_address = "Ростов-на-Дону, ул. Станиславского, д.21"
46
47
  response = MockSuccess.new
47
48
  response.expects(:body).returns(YANDEX_REGION)
48
- url = "https://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector.url_escape(region_address)}&format=json"
49
- Geokit::Geocoders::YandexGeocoder.expects(:call_geocoder_service).with(url).returns(response)
50
- res = Geokit::Geocoders::YandexGeocoder.geocode(region_address)
49
+ url = "#{@base_url}/?geocode=#{escape(region_address)}&format=json"
50
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
51
+ res = geocode(region_address)
51
52
 
52
- assert_equal "yandex", res.provider
53
+ assert_equal 'yandex', res.provider
53
54
  assert_equal "улица Станиславского, 21", res.street_address
54
- assert_equal "город Ростов на Дону", res.city
55
+ assert_equal "Ростов на Дону", res.city
55
56
  assert_equal "Ростовская область", res.state
56
- assert_equal 47.215903, res.lat
57
- assert_equal 39.703398, res.lng
58
- assert_equal "RU", res.country_code
57
+ assert_equal "городской округ Ростов-на-Дону", res.district
58
+ assert_equal 47.21589, res.lat
59
+ assert_equal 39.703272, res.lng
60
+ assert_equal 'RU', res.country_code
59
61
  assert res.success
60
62
  end
61
63
 
62
64
  def test_yandex_city
63
65
  response = MockSuccess.new
64
66
  response.expects(:body).returns(YANDEX_CITY)
65
- url = "https://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector.url_escape(@address)}&format=json"
66
- Geokit::Geocoders::YandexGeocoder.expects(:call_geocoder_service).with(url).returns(response)
67
- res = Geokit::Geocoders::YandexGeocoder.geocode(@address)
67
+ url = "#{@base_url}/?geocode=#{escape(@address)}&format=json"
68
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
69
+ res = geocode(@address)
68
70
 
69
- assert_equal "yandex", res.provider
70
- assert_equal "city", res.precision
71
+ assert_equal 'yandex', res.provider
72
+ assert_equal 'city', res.precision
71
73
  assert_equal "Москва", res.city
72
74
  assert_equal 55.755773, res.lat
73
75
  assert_equal 37.617761, res.lng
74
- assert_equal "RU", res.country_code
76
+ assert_equal 'RU', res.country_code
75
77
  assert res.success
76
78
  end
77
79
 
78
80
  def test_no_results
79
- no_results_address = "ZZ, ZZ, ZZ"
81
+ no_results_address = 'ZZ, ZZ, ZZ'
80
82
  # no_results_full_hash = {:street_address=>"ZZ", :city=>"ZZ", :state=>"ZZ"}
81
83
  # no_results_full_loc = Geokit::GeoLoc.new(no_results_full_hash)
82
84
 
83
85
  response = MockSuccess.new
84
86
  response.expects(:body).returns(YANDEX_NO_RESULTS)
85
- url = "https://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector.url_escape(no_results_address)}&format=json"
86
- Geokit::Geocoders::YandexGeocoder.expects(:call_geocoder_service).with(url).returns(response)
87
- result = Geokit::Geocoders::YandexGeocoder.geocode(no_results_address)
88
- assert_equal ",", result.ll
87
+ url = "#{@base_url}/?geocode=#{escape(no_results_address)}&format=json"
88
+ geocoder_class.expects(:call_geocoder_service).with(url).returns(response)
89
+ result = geocode(no_results_address)
90
+ assert_equal ',', result.ll
89
91
  end
90
92
  end