Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Tests and fix state/country in Ripe Geocoder

  • Loading branch information...
commit d57376a6fbc55912925ad6312e057ba1b5ad58a5 1 parent ec026f1
Michael Noack mnoack authored
37 fixtures/vcr_cassettes/fcc_reverse_geocode.yml
View
@@ -0,0 +1,37 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://data.fcc.gov/api/block/find?format=json&latitude=34.05&longitude=-118.25
+ body:
+ encoding: US-ASCII
+ string: ''
+ headers:
+ Accept:
+ - ! '*/*'
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 200
+ message: OK
+ headers:
+ Server:
+ - Sun-Java-System-Web-Server/7.0
+ Date:
+ - Fri, 13 Dec 2013 13:40:26 GMT
+ Content-Type:
+ - application/json
+ Via:
+ - 1.1 https-data
+ Proxy-Agent:
+ - Sun-Java-System-Web-Server/7.0
+ Transfer-Encoding:
+ - chunked
+ body:
+ encoding: US-ASCII
+ string: ! '{"Block":{"FIPS":"060372073012003"},"County":{"FIPS":"06037","name":"Los
+ Angeles"},"State":{"FIPS":"06","code":"CA","name":"California"},"status":"OK","executionTime":"11"}'
+ http_version:
+ recorded_at: Fri, 13 Dec 2013 13:46:20 GMT
+recorded_with: VCR 2.7.0
36 fixtures/vcr_cassettes/free_geo_ip_geocode.yml
View
@@ -0,0 +1,36 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://freegeoip.net/xml/74.125.237.209
+ body:
+ encoding: US-ASCII
+ string: ''
+ headers:
+ Accept:
+ - ! '*/*'
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 200
+ message: OK
+ headers:
+ Access-Control-Allow-Origin:
+ - ! '*'
+ Content-Type:
+ - application/xml
+ Date:
+ - Fri, 13 Dec 2013 13:02:44 GMT
+ Content-Length:
+ - '398'
+ body:
+ encoding: US-ASCII
+ string: ! "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n <Ip>74.125.237.209</Ip>\n
+ <CountryCode>US</CountryCode>\n <CountryName>United States</CountryName>\n
+ <RegionCode>CA</RegionCode>\n <RegionName>California</RegionName>\n <City>Mountain
+ View</City>\n <ZipCode>94043</ZipCode>\n <Latitude>37.4192</Latitude>\n <Longitude>-122.0574</Longitude>\n
+ <MetroCode>807</MetroCode>\n <AreaCode>650</AreaCode>\n</Response>\n"
+ http_version:
+ recorded_at: Fri, 13 Dec 2013 13:03:38 GMT
+recorded_with: VCR 2.7.0
38 fixtures/vcr_cassettes/geo_plugin_geocode.yml
View
@@ -0,0 +1,38 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://www.geoplugin.net/xml.gp?ip=74.125.237.209
+ body:
+ encoding: US-ASCII
+ string: ''
+ headers:
+ Accept:
+ - ! '*/*'
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 200
+ message: OK
+ headers:
+ Server:
+ - geoPlugin/3.2.6
+ Date:
+ - Fri, 13 Dec 2013 13:52:11 GMT
+ Content-Type:
+ - application/xml
+ Content-Length:
+ - '1164'
+ Connection:
+ - close
+ body:
+ encoding: US-ASCII
+ string: ! "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<geoPlugin>\n\t<geoplugin_request>74.125.237.209</geoplugin_request>\n\t<geoplugin_status>200</geoplugin_status>\n\t<geoplugin_credit>Some
+ of the returned data includes GeoLite data created by MaxMind, available from
+ &lt;a href=\\'http://www.maxmind.com\\'&gt;http://www.maxmind.com&lt;/a&gt;.</geoplugin_credit>\n\t<geoplugin_city>Mountain
+ View</geoplugin_city>\n\t<geoplugin_region>CA</geoplugin_region>\n\t<geoplugin_areaCode>650</geoplugin_areaCode>\n\t<geoplugin_dmaCode>807</geoplugin_dmaCode>\n\t<geoplugin_countryCode>US</geoplugin_countryCode>\n\t<geoplugin_countryName>United
+ States</geoplugin_countryName>\n\t<geoplugin_continentCode>NA</geoplugin_continentCode>\n\t<geoplugin_latitude>37.419201</geoplugin_latitude>\n\t<geoplugin_longitude>-122.057404</geoplugin_longitude>\n\t<geoplugin_regionCode>CA</geoplugin_regionCode>\n\t<geoplugin_regionName>California</geoplugin_regionName>\n\t<geoplugin_currencyCode>USD</geoplugin_currencyCode>\n\t<geoplugin_currencySymbol>&amp;#36;</geoplugin_currencySymbol>\n\t<geoplugin_currencySymbol_UTF8>$</geoplugin_currencySymbol_UTF8>\n\t<geoplugin_currencyConverter>1</geoplugin_currencyConverter>\n</geoPlugin>\n"
+ http_version:
+ recorded_at: Fri, 13 Dec 2013 13:52:08 GMT
+recorded_with: VCR 2.7.0
304 fixtures/vcr_cassettes/geonames_geocode.yml
View
@@ -0,0 +1,304 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://ws.geonames.org/postalCodeSearch?maxRows=10&placename=Adelaide
+ body:
+ encoding: US-ASCII
+ string: ''
+ headers:
+ Accept:
+ - ! '*/*'
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 200
+ message: OK
+ headers:
+ Date:
+ - Fri, 13 Dec 2013 13:34:30 GMT
+ Server:
+ - Apache/2.2.17 (Linux/SUSE)
+ Cache-Control:
+ - no-cache
+ Access-Control-Allow-Origin:
+ - ! '*'
+ Transfer-Encoding:
+ - chunked
+ Content-Type:
+ - text/xml;charset=UTF-8
+ body:
+ encoding: US-ASCII
+ string: ! '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+
+ <geonames>
+
+ <totalResultsCount>178</totalResultsCount>
+
+ <code>
+
+ <postalcode>5000</postalcode>
+
+ <name>Adelaide</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-34.92686</lat>
+
+ <lng>138.59995</lng>
+
+ <adminCode1>SA</adminCode1>
+
+ <adminName1>South Australia</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>SA CITY SUBS</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>5001</postalcode>
+
+ <name>Adelaide</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-34.93333</lat>
+
+ <lng>138.6</lng>
+
+ <adminCode1>SA</adminCode1>
+
+ <adminName1>South Australia</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>SA CITY SUBS</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>5006</postalcode>
+
+ <name>North Adelaide</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-34.90844</lat>
+
+ <lng>138.59525</lng>
+
+ <adminCode1>SA</adminCode1>
+
+ <adminName1>South Australia</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>SA CITY SUBS</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>5760</postalcode>
+
+ <name>Adelaide</name>
+
+ <countryCode>ZA</countryCode>
+
+ <lat>-32.7</lat>
+
+ <lng>26.3</lng>
+
+ <adminCode1/>
+
+ <adminName1/>
+
+ <adminCode2/>
+
+ <adminName2/>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>0846</postalcode>
+
+ <name>Adelaide River</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-13.25</lat>
+
+ <lng>131.1</lng>
+
+ <adminCode1>NT</adminCode1>
+
+ <adminName1>Northern Territory</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>DARWIN</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>4000</postalcode>
+
+ <name>Brisbane Adelaide Street</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-27.46501</lat>
+
+ <lng>153.02339</lng>
+
+ <adminCode1>QLD</adminCode1>
+
+ <adminName1>Queensland</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>BRIS CITY CNTRY</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>4703</postalcode>
+
+ <name>Adelaide Park</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-23.11468</lat>
+
+ <lng>150.69673</lng>
+
+ <adminCode1>QLD</adminCode1>
+
+ <adminName1>Queensland</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>CENTRAL QLD</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>5005</postalcode>
+
+ <name>Adelaide University</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-34.91701</lat>
+
+ <lng>138.57708</lng>
+
+ <adminCode1>SA</adminCode1>
+
+ <adminName1>South Australia</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>SA CITY SUBS</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>5015</postalcode>
+
+ <name>Port Adelaide</name>
+
+ <countryCode>AU</countryCode>
+
+ <lat>-34.81551</lat>
+
+ <lng>138.52127</lng>
+
+ <adminCode1>SA</adminCode1>
+
+ <adminName1>South Australia</adminName1>
+
+ <adminCode2/>
+
+ <adminName2>SA CITY SUBS</adminName2>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ <code>
+
+ <postalcode>M5H</postalcode>
+
+ <name>Downtown Toronto (Richmond / Adelaide / King)</name>
+
+ <countryCode>CA</countryCode>
+
+ <lat>43.6496</lat>
+
+ <lng>-79.38334</lng>
+
+ <adminCode1>ON</adminCode1>
+
+ <adminName1>Ontario</adminName1>
+
+ <adminCode2/>
+
+ <adminName2/>
+
+ <adminCode3/>
+
+ <adminName3/>
+
+ </code>
+
+ </geonames>
+
+'
+ http_version:
+ recorded_at: Fri, 13 Dec 2013 13:34:28 GMT
+recorded_with: VCR 2.7.0
66 fixtures/vcr_cassettes/ripe_geocode.yml
View
@@ -0,0 +1,66 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://stat.ripe.net/data/geoloc/data.json?resource=74.125.237.209
+ body:
+ encoding: US-ASCII
+ string: ""
+ headers:
+ Accept:
+ - "*/*"
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 200
+ message: OK
+ headers:
+ Vary:
+ - Cookie, Accept-Encoding
+ Content-Type:
+ - application/json; charset=utf-8
+ Set-Cookie:
+ - stat-session=36d43b5353a80b7f74c583a0bda4ea17; expires=Fri, 27-Dec-2013 15:12:05 GMT; Max-Age=1209600; Path=/
+ Transfer-Encoding:
+ - chunked
+ Date:
+ - Fri, 13 Dec 2013 15:12:05 GMT
+ Server:
+ - lighttpd/1.4.32
+ body:
+ encoding: US-ASCII
+ string: |-
+ {
+ "cached": true,
+ "data": {
+ "locations": [
+ {
+ "city": "Mountain View",
+ "country": "CA(US)",
+ "covered_percentage": 100,
+ "latitude": 37.419199999999996,
+ "longitude": -122.0574,
+ "prefixes": [
+ "74.125.192.0/18"
+ ]
+ }
+ ],
+ "query_time": "2013-11-01T00:00:00",
+ "resource": "74.125.237.209",
+ "unknown_percentage": 0.0
+ },
+ "data_call_status": "supported",
+ "messages": [],
+ "process_time": 3,
+ "query_id": "ed345b04-6408-11e3-9667-782bcb346712",
+ "see_also": [],
+ "server_id": "stat-app2",
+ "status": "ok",
+ "status_code": 200,
+ "time": "2013-12-13T15:12:05.342893",
+ "version": "2.0"
+ }
+ http_version:
+ recorded_at: Fri, 13 Dec 2013 15:12:02 GMT
+recorded_with: VCR 2.8.0
66 fixtures/vcr_cassettes/ripe_geocode_au.yml
View
@@ -0,0 +1,66 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://stat.ripe.net/data/geoloc/data.json?resource=118.210.24.54
+ body:
+ encoding: US-ASCII
+ string: ""
+ headers:
+ Accept:
+ - "*/*"
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 200
+ message: OK
+ headers:
+ Vary:
+ - Cookie, Accept-Encoding
+ Content-Type:
+ - application/json; charset=utf-8
+ Set-Cookie:
+ - stat-session=8dcf7a7e881c4ec64aa1a7dfcc1335c9; expires=Fri, 27-Dec-2013 15:14:20 GMT; Max-Age=1209600; Path=/
+ Transfer-Encoding:
+ - chunked
+ Date:
+ - Fri, 13 Dec 2013 15:14:20 GMT
+ Server:
+ - lighttpd/1.4.32
+ body:
+ encoding: US-ASCII
+ string: |-
+ {
+ "cached": true,
+ "data": {
+ "locations": [
+ {
+ "city": "Adelaide",
+ "country": "AU",
+ "covered_percentage": 100,
+ "latitude": -34.928699999999999,
+ "longitude": 138.5986,
+ "prefixes": [
+ "118.210.16.0/20"
+ ]
+ }
+ ],
+ "query_time": "2013-11-01T00:00:00",
+ "resource": "118.210.24.54",
+ "unknown_percentage": 0.0
+ },
+ "data_call_status": "supported",
+ "messages": [],
+ "process_time": 3,
+ "query_id": "3dfcd85e-6409-11e3-a402-782bcb346712",
+ "see_also": [],
+ "server_id": "stat-app2",
+ "status": "ok",
+ "status_code": 200,
+ "time": "2013-12-13T15:14:20.874304",
+ "version": "2.0"
+ }
+ http_version:
+ recorded_at: Fri, 13 Dec 2013 15:14:18 GMT
+recorded_with: VCR 2.8.0
1  lib/geokit/geocoders.rb
View
@@ -154,6 +154,7 @@ def self.parse(format, body, *args)
# -------------------------------------------------------------------------------------------
# "Regular" Address geocoders
# -------------------------------------------------------------------------------------------
+ require File.join(File.dirname(__FILE__), 'geocoders/base_ip')
Dir[File.join(File.dirname(__FILE__), "/geocoders/*.rb")].each {|f| require f}
require File.join(File.dirname(__FILE__), 'multi_geocoder')
9 lib/geokit/geocoders/ripe.rb
View
@@ -16,9 +16,16 @@ def self.parse_json(json)
geo = GeoLoc.new
data = json['data']['locations'][0]
+ match = data['country'].match /([A-Z]+)(\(([A-Z]+)\))?/
+ if match[3]
+ geo.state = match[1]
+ geo.country_code = match[3]
+ else
+ geo.country_code = match[1]
+ end
+
geo.provider='RIPE'
geo.city = data['city']
- geo.country_code = data['country']
geo.lat = data['latitude']
geo.lng = data['longitude']
geo.success = (data['status_code'] == 200)
2  test/helper.rb
View
@@ -11,7 +11,7 @@
require 'geoip'
if ENV['COVERAGE']
- COVERAGE_THRESHOLD = 84
+ COVERAGE_THRESHOLD = 90
require 'simplecov'
require 'simplecov-rcov'
require 'coveralls'
23 test/test_fcc_geocoder.rb
View
@@ -0,0 +1,23 @@
+require File.join(File.dirname(__FILE__), 'helper')
+
+class FCCGeocoderTest < BaseGeocoderTest #:nodoc: all
+ def setup
+ super
+ @la = Geokit::LatLng.new(34.05, -118.25)
+ end
+
+ def assert_url(expected_url)
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
+ end
+
+ def test_fcc_reverse_geocode
+ VCR.use_cassette('fcc_reverse_geocode') do
+ url = "http://data.fcc.gov/api/block/find?format=json&latitude=34.05&longitude=-118.25"
+ res = Geokit::Geocoders::FCCGeocoder.reverse_geocode(@la)
+ assert_url url
+ assert_equal res.country_code, 'US'
+ assert_equal res.state, 'CA'
+ assert_equal res.district, 'Los Angeles'
+ end
+ end
+end
23 test/test_free_geo_ip_geocoder.rb
View
@@ -0,0 +1,23 @@
+require File.join(File.dirname(__FILE__), 'helper')
+
+class FreeGeoIpGeocoderTest < BaseGeocoderTest #:nodoc: all
+ def setup
+ super
+ @ip = '74.125.237.209'
+ end
+
+ def assert_url(expected_url)
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
+ end
+
+ def test_free_geo_ip_geocode
+ VCR.use_cassette('free_geo_ip_geocode') do
+ url = "http://freegeoip.net/xml/#{@ip}"
+ res = Geokit::Geocoders::FreeGeoIpGeocoder.geocode(@ip)
+ assert_url url
+ assert_equal res.city, 'Mountain View'
+ assert_equal res.state, 'CA'
+ assert_equal res.country_code, 'US'
+ end
+ end
+end
23 test/test_geo_plugin_geocoder.rb
View
@@ -0,0 +1,23 @@
+require File.join(File.dirname(__FILE__), 'helper')
+
+class GeoPluginGeocoderTest < BaseGeocoderTest #:nodoc: all
+ def setup
+ super
+ @ip = '74.125.237.209'
+ end
+
+ def assert_url(expected_url)
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
+ end
+
+ def test_geo_plugin_geocode
+ VCR.use_cassette('geo_plugin_geocode') do
+ url = "http://www.geoplugin.net/xml.gp?ip=#{@ip}"
+ res = Geokit::Geocoders::GeoPluginGeocoder.geocode(@ip)
+ assert_url url
+ assert_equal res.city, 'Mountain View'
+ assert_equal res.state, 'CA'
+ assert_equal res.country_code, 'US'
+ end
+ end
+end
23 test/test_geonames_geocoder.rb
View
@@ -0,0 +1,23 @@
+require File.join(File.dirname(__FILE__), 'helper')
+
+class GeonamesGeocoderTest < BaseGeocoderTest #:nodoc: all
+ def setup
+ super
+ @city = 'Adelaide'
+ end
+
+ def assert_url(expected_url)
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
+ end
+
+ def test_geonames_geocode
+ VCR.use_cassette('geonames_geocode') do
+ url = "http://ws.geonames.org/postalCodeSearch?placename=#{@city}&maxRows=10"
+ res = Geokit::Geocoders::GeonamesGeocoder.geocode(@city)
+ assert_url url
+ assert_equal res.country_code, 'AU'
+ assert_equal res.state, 'South Australia'
+ assert_equal res.city, 'Adelaide'
+ end
+ end
+end
35 test/test_ripe_geocoder.rb
View
@@ -0,0 +1,35 @@
+require File.join(File.dirname(__FILE__), 'helper')
+
+class RipeGeocoderTest < BaseGeocoderTest #:nodoc: all
+ def setup
+ super
+ @ip = '74.125.237.209'
+ @ip_au = '118.210.24.54'
+ end
+
+ def assert_url(expected_url)
+ assert_equal expected_url, TestHelper.get_last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
+ end
+
+ def test_ripe_geocode
+ VCR.use_cassette('ripe_geocode') do
+ url = "http://stat.ripe.net/data/geoloc/data.json?resource=#{@ip}"
+ res = Geokit::Geocoders::RipeGeocoder.geocode(@ip)
+ assert_url url
+ assert_equal res.city, 'Mountain View'
+ assert_equal res.state, 'CA'
+ assert_equal res.country_code, 'US'
+ end
+ end
+
+ def test_ripe_geocode_au
+ VCR.use_cassette('ripe_geocode_au') do
+ url = "http://stat.ripe.net/data/geoloc/data.json?resource=#{@ip_au}"
+ res = Geokit::Geocoders::RipeGeocoder.geocode(@ip_au)
+ assert_url url
+ assert_equal res.city, 'Adelaide'
+ assert_equal res.state, nil
+ assert_equal res.country_code, 'AU'
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.