Sha256: 0a509f3e2bccbddadf36c7747d0e08672e068ffd00c982551d9b1851698c5a8a

Contents?: true

Size: 1.09 KB

Versions: 9

Compression:

Stored size: 1.09 KB

Contents

# encoding: UTF-8
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

9 entries across 9 versions & 1 rubygems

Version Path
graticule-2.7.2 lib/graticule/geocoder/host_ip.rb
graticule-2.7.1 lib/graticule/geocoder/host_ip.rb
graticule-2.7.0 lib/graticule/geocoder/host_ip.rb
graticule-2.6.0 lib/graticule/geocoder/host_ip.rb
graticule-2.5.0 lib/graticule/geocoder/host_ip.rb
graticule-2.4.0 lib/graticule/geocoder/host_ip.rb
graticule-2.3.0 lib/graticule/geocoder/host_ip.rb
graticule-2.2.0 lib/graticule/geocoder/host_ip.rb
graticule-2.1.0 lib/graticule/geocoder/host_ip.rb