In Files

Parent

Class/Module Index [+]

Quicksearch

GeoIp

Public Class Methods

api_key() click to toggle source
# File lib/geo_ip.rb, line 14
def api_key
  @@api_key
end
api_key=(api_key) click to toggle source
# File lib/geo_ip.rb, line 18
def api_key= api_key
  @@api_key = api_key
end
geolocation(ip, options={}) click to toggle source

Retreive the remote location of a given ip address.

It takes two optional arguments:

  • preceision: can either be :city (default) or :country

  • timezone: can either be false (default) or true

Example:

GeoIp.geolocation('209.85.227.104', {:precision => :city, :timezone => true})
# File lib/geo_ip.rb, line 38
def geolocation ip, options={}
  @precision = options[:precision] || :city
  @timezone  = options[:timezone]  || false
  raise 'API key must be set first: GeoIp.api_key = \YOURKEY\' if self.api_key.nil?
  raise 'Invalid IP address' unless ip.to_s =~ IPV4_REGEXP
  raise 'Invalid precision'  unless [:country, :city].include?(@precision)
  raise 'Invalid timezone'   unless [true, false].include?(@timezone)
  url = "#{SERVICE_URL}#{@precision == :city || @timezone ? CITY_API : COUNTRY_API}?key=#{api_key}&ip=#{ip}&format=json&timezone=#{@timezone}"
  parsed_response = JSON.parse RestClient::Request.execute(:method => :get, :url => url, :timeout => self.timeout)
  convert_keys parsed_response
end
timeout() click to toggle source
# File lib/geo_ip.rb, line 22
def timeout
  @@timeout
end
timeout=(timeout) click to toggle source
# File lib/geo_ip.rb, line 26
def timeout= timeout
  @@timeout = timeout
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.