diff --git a/fixtures/vcr_cassettes/fcc_reverse_geocode.yml b/fixtures/vcr_cassettes/fcc_reverse_geocode.yml new file mode 100644 index 00000000..a684752f --- /dev/null +++ b/fixtures/vcr_cassettes/fcc_reverse_geocode.yml @@ -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 diff --git a/fixtures/vcr_cassettes/free_geo_ip_geocode.yml b/fixtures/vcr_cassettes/free_geo_ip_geocode.yml new file mode 100644 index 00000000..84c67cfc --- /dev/null +++ b/fixtures/vcr_cassettes/free_geo_ip_geocode.yml @@ -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: ! "\n\n 74.125.237.209\n + US\n United States\n + CA\n California\n Mountain + View\n 94043\n 37.4192\n -122.0574\n + 807\n 650\n\n" + http_version: + recorded_at: Fri, 13 Dec 2013 13:03:38 GMT +recorded_with: VCR 2.7.0 diff --git a/fixtures/vcr_cassettes/geo_plugin_geocode.yml b/fixtures/vcr_cassettes/geo_plugin_geocode.yml new file mode 100644 index 00000000..3304f626 --- /dev/null +++ b/fixtures/vcr_cassettes/geo_plugin_geocode.yml @@ -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: ! "\n\n\t74.125.237.209\n\t200\n\tSome + of the returned data includes GeoLite data created by MaxMind, available from + <a href=\\'http://www.maxmind.com\\'>http://www.maxmind.com</a>.\n\tMountain + View\n\tCA\n\t650\n\t807\n\tUS\n\tUnited + States\n\tNA\n\t37.419201\n\t-122.057404\n\tCA\n\tCalifornia\n\tUSD\n\t&#36;\n\t$\n\t1\n\n" + http_version: + recorded_at: Fri, 13 Dec 2013 13:52:08 GMT +recorded_with: VCR 2.7.0 diff --git a/fixtures/vcr_cassettes/geonames_geocode.yml b/fixtures/vcr_cassettes/geonames_geocode.yml new file mode 100644 index 00000000..0ecb3e7c --- /dev/null +++ b/fixtures/vcr_cassettes/geonames_geocode.yml @@ -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: ! ' + + + + 178 + + + + 5000 + + Adelaide + + AU + + -34.92686 + + 138.59995 + + SA + + South Australia + + + + SA CITY SUBS + + + + + + + + + + 5001 + + Adelaide + + AU + + -34.93333 + + 138.6 + + SA + + South Australia + + + + SA CITY SUBS + + + + + + + + + + 5006 + + North Adelaide + + AU + + -34.90844 + + 138.59525 + + SA + + South Australia + + + + SA CITY SUBS + + + + + + + + + + 5760 + + Adelaide + + ZA + + -32.7 + + 26.3 + + + + + + + + + + + + + + + + + + 0846 + + Adelaide River + + AU + + -13.25 + + 131.1 + + NT + + Northern Territory + + + + DARWIN + + + + + + + + + + 4000 + + Brisbane Adelaide Street + + AU + + -27.46501 + + 153.02339 + + QLD + + Queensland + + + + BRIS CITY CNTRY + + + + + + + + + + 4703 + + Adelaide Park + + AU + + -23.11468 + + 150.69673 + + QLD + + Queensland + + + + CENTRAL QLD + + + + + + + + + + 5005 + + Adelaide University + + AU + + -34.91701 + + 138.57708 + + SA + + South Australia + + + + SA CITY SUBS + + + + + + + + + + 5015 + + Port Adelaide + + AU + + -34.81551 + + 138.52127 + + SA + + South Australia + + + + SA CITY SUBS + + + + + + + + + + M5H + + Downtown Toronto (Richmond / Adelaide / King) + + CA + + 43.6496 + + -79.38334 + + ON + + Ontario + + + + + + + + + + + + + +' + http_version: + recorded_at: Fri, 13 Dec 2013 13:34:28 GMT +recorded_with: VCR 2.7.0 diff --git a/fixtures/vcr_cassettes/ripe_geocode.yml b/fixtures/vcr_cassettes/ripe_geocode.yml new file mode 100644 index 00000000..d7355c26 --- /dev/null +++ b/fixtures/vcr_cassettes/ripe_geocode.yml @@ -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 diff --git a/fixtures/vcr_cassettes/ripe_geocode_au.yml b/fixtures/vcr_cassettes/ripe_geocode_au.yml new file mode 100644 index 00000000..9f569324 --- /dev/null +++ b/fixtures/vcr_cassettes/ripe_geocode_au.yml @@ -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 diff --git a/lib/geokit/geocoders.rb b/lib/geokit/geocoders.rb index e807d73d..94d0d0d6 100644 --- a/lib/geokit/geocoders.rb +++ b/lib/geokit/geocoders.rb @@ -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') diff --git a/lib/geokit/geocoders/ripe.rb b/lib/geokit/geocoders/ripe.rb index 647efdfe..a0ce2665 100644 --- a/lib/geokit/geocoders/ripe.rb +++ b/lib/geokit/geocoders/ripe.rb @@ -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) diff --git a/test/helper.rb b/test/helper.rb index ad66d2b4..59768412 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -11,7 +11,7 @@ require 'geoip' if ENV['COVERAGE'] - COVERAGE_THRESHOLD = 84 + COVERAGE_THRESHOLD = 90 require 'simplecov' require 'simplecov-rcov' require 'coveralls' diff --git a/test/test_fcc_geocoder.rb b/test/test_fcc_geocoder.rb new file mode 100644 index 00000000..a87ca084 --- /dev/null +++ b/test/test_fcc_geocoder.rb @@ -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 diff --git a/test/test_free_geo_ip_geocoder.rb b/test/test_free_geo_ip_geocoder.rb new file mode 100644 index 00000000..ad7c5e4a --- /dev/null +++ b/test/test_free_geo_ip_geocoder.rb @@ -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 diff --git a/test/test_geo_plugin_geocoder.rb b/test/test_geo_plugin_geocoder.rb new file mode 100644 index 00000000..f60a6930 --- /dev/null +++ b/test/test_geo_plugin_geocoder.rb @@ -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 diff --git a/test/test_geonames_geocoder.rb b/test/test_geonames_geocoder.rb new file mode 100644 index 00000000..5cc575ec --- /dev/null +++ b/test/test_geonames_geocoder.rb @@ -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 diff --git a/test/test_ripe_geocoder.rb b/test/test_ripe_geocoder.rb new file mode 100644 index 00000000..1d7cc68c --- /dev/null +++ b/test/test_ripe_geocoder.rb @@ -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