Sha256: e49bc17e75d9c88f5fa8c765f579a9021355adc745161f0bf5863345c9375777

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

module Geokit
  module Geocoders
    # Provides geocoding based upon an IP address.  The underlying web service is geoplugin.net
    class GeoPluginGeocoder < Geocoder
      private

      def self.do_geocode(ip, options = {})
        return GeoLoc.new unless /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
        response = self.call_geocoder_service("http://www.geoplugin.net/xml.gp?ip=#{ip}")
        return response.is_a?(Net::HTTPSuccess) ? parse_xml(response.body) : GeoLoc.new
      rescue
        logger.error "Caught an error during GeoPluginGeocoder geocoding call: "+$!
        return GeoLoc.new
      end

      def self.parse_xml(xml)
        xml = REXML::Document.new(xml)
        geo = GeoLoc.new
        geo.provider='geoPlugin'
        geo.city = xml.elements['//geoplugin_city'].text
        geo.state = xml.elements['//geoplugin_region'].text
        geo.country_code = xml.elements['//geoplugin_countryCode'].text
        geo.lat = xml.elements['//geoplugin_latitude'].text.to_f
        geo.lng = xml.elements['//geoplugin_longitude'].text.to_f
        geo.success = !!geo.city && !geo.city.empty?
        return geo
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
geokit-1.7.1 lib/geokit/services/geo_plugin.rb
geokit-1.7.0 lib/geokit/services/geo_plugin.rb
geokit-1.6.7 lib/geokit/services/geo_plugin.rb
geokit-1.6.6 lib/geokit/services/geo_plugin.rb
geokit-1.7.0.rc1 lib/geokit/services/geo_plugin.rb