andre-geokit 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -4,6 +4,14 @@ require 'rubygems'
4
4
  require 'hoe'
5
5
  require './lib/geokit.rb'
6
6
 
7
+ # undefined method `empty?' for nil:NilClass
8
+ # /Library/Ruby/Site/1.8/rubygems/specification.rb:886:in `validate'
9
+ class NilClass
10
+ def empty?
11
+ true
12
+ end
13
+ end
14
+
7
15
  project=Hoe.new('geokit', Geokit::VERSION) do |p|
8
16
  #p.rubyforge_name = 'geokit' # if different than lowercase project name
9
17
  p.developer('Andre Lewis', 'andre@earthcode.com')
@@ -1,5 +1,5 @@
1
1
  module Geokit
2
- VERSION = '1.3.1'
2
+ VERSION = '1.3.2'
3
3
  # These defaults are used in Geokit::Mappable.distance_to and in acts_as_mappable
4
4
  @@default_units = :miles
5
5
  @@default_formula = :sphere
@@ -564,7 +564,7 @@ module Geokit
564
564
  # The failover approach is crucial for production-grade apps, but is rarely used.
565
565
  # 98% of your geocoding calls will be successful with the first call
566
566
  def self.do_geocode(address)
567
- geocode_ip = !!/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(address)
567
+ geocode_ip = /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/.match(address)
568
568
  provider_order = geocode_ip ? Geokit::Geocoders::ip_provider_order : Geokit::Geocoders::provider_order
569
569
 
570
570
  provider_order.each do |provider|
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
3
+
4
+ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
5
+
6
+ IP_SUCCESS=<<-EOF
7
+ <?xml version="1.0" encoding="UTF-8"?>
8
+ <geoPlugin>
9
+ <geoplugin_city>Belo Horizonte</geoplugin_city>
10
+ <geoplugin_region>Minas Gerais</geoplugin_region>
11
+ <geoplugin_areaCode>0</geoplugin_areaCode>
12
+ <geoplugin_dmaCode>0</geoplugin_dmaCode>
13
+ <geoplugin_countryCode>BR</geoplugin_countryCode>
14
+ <geoplugin_countryName>Brazil</geoplugin_countryName>
15
+ <geoplugin_continentCode>SA</geoplugin_continentCode>
16
+ <geoplugin_latitude>-19.916700</geoplugin_latitude>
17
+ <geoplugin_longitude>-43.933300</geoplugin_longitude>
18
+ <geoplugin_currencyCode>BRL</geoplugin_currencyCode>
19
+ <geoplugin_currencySymbol>&#82;&#36;</geoplugin_currencySymbol>
20
+ <geoplugin_currencyConverter>2.2575001717</geoplugin_currencyConverter>
21
+ </geoPlugin>
22
+ EOF
23
+
24
+ def setup
25
+ super
26
+ @success.provider = "geoPlugin"
27
+ end
28
+
29
+ def test_successful_lookup
30
+ success = MockSuccess.new
31
+ success.expects(:body).returns(IP_SUCCESS)
32
+ url = 'http://www.geoplugin.net/xml.gp?ip=200.150.38.66'
33
+ GeoKit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(success)
34
+ location = GeoKit::Geocoders::GeoPluginGeocoder.geocode('200.150.38.66')
35
+ assert_not_nil location
36
+ assert_equal -19.916700, location.lat
37
+ assert_equal -43.933300, location.lng
38
+ assert_equal "Belo Horizonte", location.city
39
+ assert_equal "Minas Gerais", location.state
40
+ assert_equal "BR", location.country_code
41
+ assert_equal "geoPlugin", location.provider
42
+ assert location.success?
43
+ end
44
+
45
+ def test_invalid_ip
46
+ location = GeoKit::Geocoders::GeoPluginGeocoder.geocode("pixrum")
47
+ assert_not_nil location
48
+ assert !location.success?
49
+ end
50
+
51
+ def test_service_unavailable
52
+ failure = MockFailure.new
53
+ url = 'http://www.geoplugin.net/xml.gp?ip=10.10.10.10'
54
+ GeoKit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
55
+ location = GeoKit::Geocoders::GeoPluginGeocoder.geocode("10.10.10.10")
56
+ assert_not_nil location
57
+ assert !location.success?
58
+ end
59
+ end
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::google = 'Google'
4
+
5
+ class GoogleReverseGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ GOOGLE_REVERSE_FULL=<<-EOF.strip
8
+ <?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>51.457833,7.016685</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>Porscheplatz 1, 45127 Essen, Deutschland</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName><Thoroughfare><ThoroughfareName>Porscheplatz 1</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4609805" south="51.4546853" east="7.0198324" west="7.0135372" /></ExtendedData><Point><coordinates>7.0166848,51.4578329,0</coordinates></Point></Placemark><Placemark id="p2"><address>Stadtkern, Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4630710" south="51.4506320" east="7.0193200" west="7.0026170" /></ExtendedData><Point><coordinates>7.0124328,51.4568201,0</coordinates></Point></Placemark><Placemark id="p3"><address>45127 Essen, Deutschland</address><AddressDetails Accuracy="5" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4637808" south="51.4503125" east="7.0231080" west="6.9965454" /></ExtendedData><Point><coordinates>7.0104543,51.4556194,0</coordinates></Point></Placemark><Placemark id="p4"><address>Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0147614,51.4580686,0</coordinates></Point></Placemark><Placemark id="p5"><address>Essen, Deutschland</address><AddressDetails Accuracy="3" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0461136,51.4508381,0</coordinates></Point></Placemark><Placemark id="p6"><address>Nordrhein-Westfalen, Deutschland</address><AddressDetails Accuracy="2" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="52.5314170" south="50.3225720" east="9.4615950" west="5.8663566" /></ExtendedData><Point><coordinates>7.6615938,51.4332367,0</coordinates></Point></Placemark><Placemark id="p7"><address>Deutschland</address><AddressDetails Accuracy="1" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName></Country></AddressDetails><ExtendedData><LatLonBox north="55.0568230" south="47.2701270" east="15.0418536" west="5.8663566" /></ExtendedData><Point><coordinates>10.4515260,51.1656910,0</coordinates></Point></Placemark></Response></kml>
9
+ EOF
10
+
11
+
12
+ def test_google_full_address
13
+ response = MockSuccess.new
14
+ response.expects(:body).returns(GOOGLE_REVERSE_FULL)
15
+
16
+
17
+ # http://maps.google.com/maps/geo?output=xml&oe=utf-8&ll=51.4578329,7.0166848&key=asdad
18
+
19
+ # #<Geokit::GeoLoc:0x10ec7ec
20
+ # @city="Essen",
21
+ # @country_code="DE",
22
+ # @full_address="Porscheplatz 1, 45127 Essen, Germany",
23
+ # @lat=51.4578329,
24
+ # @lng=7.0166848,
25
+ # @precision="address",
26
+ # @provider="google",
27
+ # @state="Nordrhein-Westfalen",
28
+ # @street_address="Porscheplatz 1",
29
+ # @success=true,
30
+ # @zip="45127">
31
+ #
32
+
33
+
34
+ @latlng = "51.4578329,7.0166848"
35
+
36
+ url = "http://maps.google.com/maps/geo?ll=#{Geokit::Inflector.url_escape(@latlng)}&output=xml&key=Google&oe=utf-8"
37
+ Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
38
+ res=Geokit::Geocoders::GoogleGeocoder.reverse_geocode(@latlng)
39
+ assert_equal "Nordrhein-Westfalen", res.state
40
+ assert_equal "Essen", res.city
41
+ assert_equal "45127", res.zip
42
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
43
+ assert res.is_us? == false
44
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
45
+ assert_equal "google", res.provider
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'lib/geokit'
3
+
4
+ class InflectorTest < Test::Unit::TestCase #:nodoc: all
5
+
6
+ def test_titleize
7
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar Grove')
8
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar grove')
9
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar Grove')
10
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar grove')
11
+ end
12
+
13
+ def test_titleize_with_unicode
14
+ assert_equal 'Borås', Geokit::Inflector.titleize('Borås')
15
+ assert_equal 'Borås', Geokit::Inflector.titleize('borås')
16
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('Borås (Abc)')
17
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('Borås (abc)')
18
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (Abc)')
19
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (abc)')
20
+ end
21
+
22
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
3
+
4
+ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
5
+
6
+ IP_FAILURE=<<-EOF
7
+ Country: (Private Address) (XX)
8
+ City: (Private Address)
9
+ Latitude:
10
+ Longitude:
11
+ EOF
12
+
13
+ IP_SUCCESS=<<-EOF
14
+ Country: UNITED STATES (US)
15
+ City: Sugar Grove, IL
16
+ Latitude: 41.7696
17
+ Longitude: -88.4588
18
+ EOF
19
+
20
+ IP_UNICODED=<<-EOF
21
+ Country: SWEDEN (SE)
22
+ City: Borås
23
+ Latitude: 57.7167
24
+ Longitude: 12.9167
25
+ EOF
26
+
27
+ def setup
28
+ super
29
+ @success.provider = "hostip"
30
+ end
31
+
32
+ def test_successful_lookup
33
+ success = MockSuccess.new
34
+ success.expects(:body).returns(IP_SUCCESS)
35
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
36
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
37
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
38
+ assert_not_nil location
39
+ assert_equal 41.7696, location.lat
40
+ assert_equal(-88.4588, location.lng)
41
+ assert_equal "Sugar Grove", location.city
42
+ assert_equal "IL", location.state
43
+ assert_equal "US", location.country_code
44
+ assert_equal "hostip", location.provider
45
+ assert location.success?
46
+ end
47
+
48
+ def test_unicoded_lookup
49
+ success = MockSuccess.new
50
+ success.expects(:body).returns(IP_UNICODED)
51
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
52
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
53
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
54
+ assert_not_nil location
55
+ assert_equal 57.7167, location.lat
56
+ assert_equal 12.9167, location.lng
57
+ assert_equal "Bor\303\245s", location.city
58
+ assert_nil location.state
59
+ assert_equal "SE", location.country_code
60
+ assert_equal "hostip", location.provider
61
+ assert location.success?
62
+ end
63
+
64
+ def test_failed_lookup
65
+ failure = MockSuccess.new
66
+ failure.expects(:body).returns(IP_FAILURE)
67
+ url = 'http://api.hostip.info/get_html.php?ip=10.10.10.10&position=true'
68
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
69
+ location = GeoKit::Geocoders::IpGeocoder.geocode("10.10.10.10")
70
+ assert_not_nil location
71
+ assert !location.success?
72
+ end
73
+
74
+ def test_invalid_ip
75
+ location = GeoKit::Geocoders::IpGeocoder.geocode("blah")
76
+ assert_not_nil location
77
+ assert !location.success?
78
+ end
79
+
80
+ def test_service_unavailable
81
+ failure = MockFailure.new
82
+ url = 'http://api.hostip.info/get_html.php?ip=10.10.10.10&position=true'
83
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
84
+ location = GeoKit::Geocoders::IpGeocoder.geocode("10.10.10.10")
85
+ assert_not_nil location
86
+ assert !location.success?
87
+ end
88
+ end
@@ -41,4 +41,14 @@ class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
41
41
  Geokit::Geocoders.provider_order = temp
42
42
  end
43
43
 
44
+ def test_blank_address
45
+ t1, t2 = Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order # will need to reset after
46
+ Geokit::Geocoders.provider_order = [:google]
47
+ Geokit::Geocoders.ip_provider_order = [:geo_plugin]
48
+ Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with("").returns(@failure)
49
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).never
50
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode("")
51
+ Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order = t1, t2 # reset to orig values
52
+ end
53
+
44
54
  end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::ip_provider_order=[:geo_plugin,:ip]
4
+
5
+ class MultiIpGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ def setup
8
+ @ip_address = '10.10.10.10'
9
+ @success = Geokit::GeoLoc.new({:city=>"SAN FRANCISCO", :state=>"CA", :country_code=>"US", :lat=>37.7742, :lng=>-122.417068})
10
+ @success.success = true
11
+ @failure = Geokit::GeoLoc.new
12
+ end
13
+
14
+ def test_successful_first
15
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@success)
16
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
17
+ end
18
+
19
+ def test_failover
20
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
21
+ Geokit::Geocoders::IpGeocoder.expects(:geocode).with(@ip_address).returns(@success)
22
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
23
+ end
24
+
25
+ def test_failure
26
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
27
+ Geokit::Geocoders::IpGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
28
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
29
+ end
30
+
31
+ def test_invalid_provider
32
+ temp = Geokit::Geocoders::ip_provider_order
33
+ Geokit::Geocoders.ip_provider_order = [:bogus]
34
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
35
+ Geokit::Geocoders.ip_provider_order = temp
36
+ end
37
+
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: andre-geokit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andre Lewis and Bill Eisenhauer
@@ -71,8 +71,13 @@ test_files:
71
71
  - test/test_bounds.rb
72
72
  - test/test_ca_geocoder.rb
73
73
  - test/test_geoloc.rb
74
+ - test/test_geoplugin_geocoder.rb
74
75
  - test/test_google_geocoder.rb
76
+ - test/test_google_reverse_geocoder.rb
77
+ - test/test_inflector.rb
78
+ - test/test_ipgeocoder.rb
75
79
  - test/test_latlng.rb
76
80
  - test/test_multi_geocoder.rb
81
+ - test/test_multi_ip_geocoder.rb
77
82
  - test/test_us_geocoder.rb
78
83
  - test/test_yahoo_geocoder.rb