Sha256: ee647038bae10c25fd307e06511a01a9c1dcb7ffa09d8ec2f4df4137052a7064

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Geokit
  module Geocoders
    # Provides geocoding based upon an IP address.  The underlying web service is geoplugin.net
    class RipeGeocoder < BaseIpGeocoder
      self.secure = false # supports HTTPS, but Net::HTTPS doesn't like the server

      private

      def self.do_geocode(ip)
        process :json, ip
      end

      def self.submit_url(ip)
        "#{protocol}://stat.ripe.net/data/geoloc/data.json?resource=#{ip}"
      end

      def self.parse_json(json)
        loc = new_loc
        data = json['data']['locations'][0]

        if data
          loc.lat = data['latitude']
          loc.lng = data['longitude']
          set_address_components(data, loc)
          loc.success = data && (data['status_code'] == 200)
        end
        loc
      end

      def self.set_address_components(data, loc)
        match = data['country'].match(/([A-Z]+)(\(([A-Z]+)\))?/)
        if match[3]
          loc.state_code = match[1]
          loc.country_code = match[3]
        else
          loc.country_code = match[1]
        end

        loc.city = data['city']
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geokit-1.9.0 lib/geokit/geocoders/ripe.rb