Created
October 25, 2014 02:59
-
-
Save abrambailey/4a9ea5aada528489e60e to your computer and use it in GitHub Desktop.
MaxMind GeoIP2 Ruby Helper file
This file contains hidden or 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
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