Sha256: 989e62af1aebce8e799416173ac87b142c9b9c02aaa7b3f32f502a1bb3ca0865

Contents?: true

Size: 1.09 KB

Versions: 2

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 prepare_response(response)
        # add new line so YAML.load doesn't puke
        YAML.load(response + "\n")
      end
      
      def parse_response(response) #:nodoc:
        Location.new.tap 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

2 entries across 2 versions & 1 rubygems

Version Path
graticule-2.0.1 lib/graticule/geocoder/host_ip.rb
graticule-2.0.0 lib/graticule/geocoder/host_ip.rb