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