Skip to content

Commit

Permalink
Add ipstack geocoder
Browse files Browse the repository at this point in the history
  • Loading branch information
dsantosmerino committed May 9, 2018
1 parent dc40b01 commit e59343c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
44 changes: 44 additions & 0 deletions fixtures/vcr_cassettes/ipstack_geocode.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions lib/geokit/geocoders/ipstack.rb
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
27 changes: 27 additions & 0 deletions test/test_ipstack_geocoder.rb
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

0 comments on commit e59343c

Please sign in to comment.