forked from geokit/geokit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc40b01
commit e59343c
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Geokit | ||
module Geocoders | ||
# Provides geocoding based upon an IP address. The underlying web service is freegeoip.net | ||
class IpstackGeocoder < BaseIpGeocoder | ||
config :api_key | ||
self.secure = true | ||
|
||
private | ||
|
||
def self.do_geocode(ip, _options = nil) | ||
process :json, ip | ||
end | ||
|
||
def self.submit_url(ip) | ||
"#{protocol}://api.ipstack.com/#{ip}?access_key=#{api_key}" | ||
end | ||
|
||
def self.parse_json(result) | ||
loc = new_loc | ||
return loc if result['success'] == false | ||
|
||
loc.city = result['city'] | ||
loc.state_code = result['region_code'] | ||
loc.state_name = result['region_name'] | ||
loc.zip = result['zip'] | ||
loc.lat = result['latitude'] | ||
loc.lng = result['longitude'] | ||
loc.country_code = result['country_code'] | ||
loc.country = result['country_name'] | ||
loc.success = !loc.city.nil? | ||
|
||
loc | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require File.join(File.dirname(__FILE__), 'helper') | ||
|
||
class IpstackGeocoderTest < BaseGeocoderTest #:nodoc: all | ||
def setup | ||
super | ||
@ip = '68.194.36.44' | ||
geocoder_class.api_key = 'some_api_key' | ||
end | ||
|
||
def assert_url(expected_url) | ||
assert_equal expected_url, TestHelper.last_url | ||
end | ||
|
||
def test_free_geo_ip_geocode | ||
url = "http://api.ipstack.com/#{@ip}?access_key=some_api_key" | ||
res = geocode(@ip, :ipstack_geocode) | ||
|
||
assert_url url | ||
assert_equal res.city, 'The Bronx' | ||
assert_equal res.state, 'NY' | ||
assert_equal res.state_name, 'New York' | ||
assert_equal res.zip, '10466' | ||
assert_equal res.country_code, 'US' | ||
assert_equal res.country, 'United States' | ||
assert_equal res.success?, true | ||
end | ||
end |