Created
September 3, 2012 15:02
-
-
Save peterhellberg/3609934 to your computer and use it in GitHub Desktop.
Simple GeoIP service class using freegeoip.net and the Ruby Standard Library
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
require 'net/http' | |
require 'ostruct' | |
require 'json' | |
class GeoipService | |
def initialize(http = nil) | |
@http = http || Net::HTTP.new('freegeoip.net') | |
end | |
def call(host) | |
OpenStruct.new JSON.parse @http.get("/json/#{host}").body | |
end | |
end |
Albeit with no error checking.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The original Gist was using Faraday and Hashie, but it is quite possible to achieve the same result (albeit with no error checking) using only the Ruby Standard Library.