Class: Drifter::Geocoders::HostIP
- Defined in:
- lib/drifter/geocoders/hostip.rb
Overview
This class adds support for basic ip address geocoding using the free API from hostip.info
Constant Summary collapse
- BASE_URI =
'http://api.hostip.info/get_html.php'
- IP_PATTERN =
/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
- @@lat_error =
nil
Class Method Summary collapse
-
.base_uri ⇒ Object
nodoc.
-
.geocode(ip, options = {}) ⇒ Object
geocodes the given IP address.
-
.query_uri(params) ⇒ Object
nodoc.
Methods inherited from Base
Class Method Details
.base_uri ⇒ Object
nodoc
15 16 17 |
# File 'lib/drifter/geocoders/hostip.rb', line 15 def self.base_uri BASE_URI end |
.geocode(ip, options = {}) ⇒ Object
geocodes the given IP address.
On Success: returns an array with one Drifter::Location object On Failure: returns an empty array On Error: returns nil. last_error() holds error information
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/drifter/geocoders/hostip.rb', line 25 def self.geocode(ip, ={}) # TODO: tests! # make sure it's an IP address unless ip.to_s =~ IP_PATTERN @@last_error = { :message => ip.to_s + " is not a valid IP address" } return nil end # the position param is needed for lat/lng [:ip] = ip.to_s [:position] = true uri = query_uri() # get the response, should be 5 lines (6 but one is blank) response = fetch(uri) response = response.to_s.split("\n").collect { |line| line.empty?? nil : line } response.compact! unless response.size == 5 @@last_error = { :message => "HostIP returned a response that #{name} doesn't understand" } return nil end # still here so the errors can be cleared @@last_error = nil # however, hostip wont return an error response for bad queries. # It just returns blank values and XX as the country code. Treat that a # a successful request with no results: return [] if response.first =~ /XX/ # now we can start building the object loc = Drifter::Location.new # Country: UNITED KINGDOM (UK) data = response[0].split(': ').last.split(' (') loc.country = data.first loc.country_code = data.last.sub(')', '') # City: London data = response[1].split(': ').last loc.city = data # Latitude: 51.5 data = response[2].split(': ').last loc.lat = data.to_f # Longitude: -0.1167 data = response[3].split(': ').last loc.lng = data.to_f return [loc] end |
.query_uri(params) ⇒ Object
nodoc
82 83 84 85 86 |
# File 'lib/drifter/geocoders/hostip.rb', line 82 def self.query_uri(params) uri = URI.parse(base_uri) uri.query = hash_to_query_string(params) return uri end |