Skip to content

Instantly share code, notes, and snippets.

@abrambailey
Created October 25, 2014 02:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abrambailey/4a9ea5aada528489e60e to your computer and use it in GitHub Desktop.
Save abrambailey/4a9ea5aada528489e60e to your computer and use it in GitHub Desktop.
MaxMind GeoIP2 Ruby Helper file
module GeoHelper
def maxmind_locate
uri = URI("https://geoip.maxmind.com/geoip/v2.1/city/#{request.remote_ip}?pretty")
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https',
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
request = Net::HTTP::Get.new uri.request_uri
#Key from https://www.maxmind.com/en/my_license_key
request.basic_auth 'USER_ID', 'USER_KEY'
response = http.request request # Net::HTTPResponse object
if response.kind_of? Net::HTTPSuccess
location_hash = JSON.parse(response.body)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment