Sha256: c0031ac2aae766086eca1bc8415728a4f6f44c11d76b65daa6f20a041c7709f3

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'yaml'

module Graticule #:nodoc:
  module Geocoder #:nodoc:

    class HostIp < Base

      def initialize
        @url = URI.parse 'http://api.hostip.info/get_html.php'
      end

      # Geocode an IP address using http://hostip.info
      def locate(address)
        get :ip => address, :position => true
      end
    
    private
    
      def read_response(response)
        # add new line so YAML.load doesn't puke
        YAML.load(response + "\n")
      end
      
      def parse_response(response) #:nodoc:
        returning Location.new do |location|
          location.latitude = response['Latitude']
          location.longitude = response['Longitude']
          location.locality, location.region = response['City'].split(', ')
          country = response['Country'].match(/\((\w+)\)$/)
          location.country = country[1] if country
        end
      end

      def check_error(response) #:nodoc:
        raise AddressError, 'Unknown' if response['City'] =~ /Unknown City/
        raise AddressError, 'Private Address' if response['City'] =~ /Private Address/
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graticule-0.2.3 lib/graticule/geocoder/host_ip.rb