Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created September 3, 2012 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/3609934 to your computer and use it in GitHub Desktop.
Save peterhellberg/3609934 to your computer and use it in GitHub Desktop.
Simple GeoIP service class using freegeoip.net and the Ruby Standard Library
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
@peterhellberg
Copy link
Author

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.

@mislav
Copy link

mislav commented Sep 13, 2012

Albeit with no error checking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment