class Geokit::Geocoders::IpGeocoder
Provides geocoding based upon an IP address. The underlying web service is a hostip.info which sources their data through a combination of publicly available information as well as community contributions.
Private Class Methods
do_geocode(ip, _=nil)
click to toggle source
Given an IP address, returns a GeoLoc
instance which contains latitude, longitude, city, and country code. Sets the success attribute to false if the ip parameter does not match an ip address.
# File lib/geokit/geocoders/ip.rb, line 12 def self.do_geocode(ip, _=nil) return GeoLoc.new unless valid_ip?(ip) url = submit_url(ip) res = call_geocoder_service(url) return GeoLoc.new unless net_adapter.success?(res) ensure_utf8_encoding(res) body = res.body body = body.encode('UTF-8') if body.respond_to? :encode parse :yaml, body end
ensure_utf8_encoding(res)
click to toggle source
Forces UTF-8 encoding on the body Rails expects string input to be UTF-8 hostip.info specifies the charset encoding in the headers thus extract encoding from headers and tell Rails about it by forcing it
# File lib/geokit/geocoders/ip.rb, line 50 def self.ensure_utf8_encoding(res) if (enc_string = extract_charset(res)) res.body.force_encoding(enc_string.upcase) if res.body.respond_to?(:force_encoding) res.body.encode('UTF-8') end end
extract_charset(res)
click to toggle source
Extracts charset out of the response headers
# File lib/geokit/geocoders/ip.rb, line 58 def self.extract_charset(res) if (content_type = res['content-type']) capture = content_type.match(/charset=(.+)/) capture && capture[1] end end
submit_url(ip)
click to toggle source
# File lib/geokit/geocoders/ip.rb, line 23 def self.submit_url(ip) "http://api.hostip.info/get_html.php?ip=#{ip}&position=true" end