focus
A simple interface to the www.hostip.info API.
HostIP.info
hostip.info is a “Community Geotarget IP Project” and provides a simple API to transform an “IP Address” into a location (including Country, City, and coordinates).
No API key or sign-up is required to use the service.
hostip.info does request that if a lot of databases lookups are being done that you download the entire database from them instead.
dependencies
-
[HTTParty](github.com/jnunemaker/httparty/tree/master)
- version used=0.3.1
-
fakeweb [version used=1.2.0] [NOTE: only needed to run tests]
example
from the command line
# focus 209.85.171.100
from a ruby script
require 'rubygems'
require 'focus'
focus = Focus.new("209.85.171.100")
puts focus.location.coordinates
from a rails application
in config/environment
require 'focus'
in your controller
@focus = Focus.new("209.85.171.100")
in your view
@focus.location.coordinates
from a rails application with caching
Using caching is highly advised if possible. The ip_address to location conversion doesn’t normally change over time (if ever) so why continuously query it?
Assumption: caching is already enabled and working.
in config/environment
require 'focus'
in your controller
ip_address = "209.85.171.100"
@focus = Rails.cache.fetch(Spotlight.key(ip_address), :expires_in => 1.weeks) do
Focus.new(ip_address)
end
in your view
@focus.location.coordinates
credits
Other then the code written by [me](github.com/attack) and the code created by [‘jeweler’](github.com/technicalpickles/jeweler/tree/master) for making and maintaining gems, there is strong influence from [jnunemaker](github.com/jnunemaker) and the gem google-weather.
Copyright
Copyright © 2009 Mark G. See LICENSE for details.