Object
# File lib/geo_ip.rb, line 18 def api_key= api_key @@api_key = api_key end
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
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
Generated with the Darkfish Rdoc Generator 2.