Module: Drifter

Defined in:
lib/drifter/location/locatable.rb,
lib/drifter.rb,
lib/drifter/version.rb,
lib/drifter/location.rb,
lib/drifter/geocoders/base.rb,
lib/drifter/geocoders/yahoo.rb,
lib/drifter/geocoders/google.rb,
lib/drifter/geocoders/hostip.rb,
lib/drifter/distance/haversine.rb

Overview

classes including this module must repond to lat(), lat=(), lng() and lng=()

Defined Under Namespace

Modules: Distance, Geocoders Classes: Location

Constant Summary collapse

VERSION =
"0.1.2"
@@default_geocoder =
:google
@@default_units =
:miles
@@last_error =
nil

Class Method Summary collapse

Class Method Details

.default_geocoderObject

returns the default geocoder



14
15
16
# File 'lib/drifter.rb', line 14

def self.default_geocoder
  @@default_geocoder
end

.default_geocoder=(value) ⇒ Object

Sets the default geocoder. Supported values are :google or :yahoo If using :yahoo, you will also need to set your yahoo appid using Drifter::Geocoders::Yahoo.app_id=()



22
23
24
# File 'lib/drifter.rb', line 22

def self.default_geocoder=(value)
  @@default_geocoder = value
end

.default_unitsObject

Returns the default units for distance calculations



28
29
30
# File 'lib/drifter.rb', line 28

def self.default_units
  @@default_units
end

.default_units=(value) ⇒ Object

Sets the default units for distance calculations. Supported values are :miles and :kms



35
36
37
# File 'lib/drifter.rb', line 35

def self.default_units=(value)
  @@default_units=value
end

.extract_latlng(loc) ⇒ Object

Helper method to extract the lat and lng from an array or from any object that responds to lat() and lng(). Returns nil if neither of those apply



42
43
44
45
46
# File 'lib/drifter.rb', line 42

def self.extract_latlng(loc)
  return loc.first, loc.last if loc.is_a?(Array) && loc.size == 2
  return loc.lat, loc.lng if loc.respond_to?(:lat) && loc.respond_to?(:lng)
  return nil
end

.extract_latlng!(loc) ⇒ Object

Same as Drifter,extract_latlng() but raises ArgumentError on failure

Raises:

  • (ArgumentError)


50
51
52
53
54
# File 'lib/drifter.rb', line 50

def self.extract_latlng!(loc)
  lat, lng = extract_latlng(loc)
  return lat, lng if lat && lng
  raise ArgumentError, "Could not extract lat and lng from #{loc.class.name} object"
end

.geocode(location, params = {}) ⇒ Object

Accepts a string or a set of coordinates and returns an Array of Drifter::Location objects with the results of the geocoding request. If there is an error, this method returns nil and the error can be accessed via Drifter.last_error().

You can over-ride the default geocoder using the params option: Drifter.geocode(“somewhere”, :geocoder => :yahoo)

You can perform reverse geocoding by passing a [lat, lng] array, or an object that responds to lat() and lng(). Any params besides :geocoder are url encoded and sent to the geocoder as query string parameters. This can be used to modify the results of the query. See the README for an example.

if location is a string containing an IP address, the :geocoder value is ignored and the ip is geocoded using the hostip.info web service. This only returns a country, city, lat and lng so you could reverse geocode the result to get more info



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/drifter.rb', line 72

def self.geocode(location, params={})
  geocoder = params.delete(:geocoder) || default_geocoder
  geocoder = :hostip if location.to_s =~ Drifter::Geocoders::HostIP::IP_PATTERN
  geocoder = case geocoder
    when :google then Drifter::Geocoders::Google
    when :yahoo then Drifter::Geocoders::Yahoo
    when :hostip then Drifter::Geocoders::HostIP
    else raise ArgumentError, "Geocoder #{geocoder} not recognised"
  end
  results = geocoder.geocode(location, params)
  @@last_error = geocoder.last_error
  return results
end

.last_errorObject

Returns a Hash containing error code and status from a failed geocoding request



88
89
90
# File 'lib/drifter.rb', line 88

def self.last_error
  @@last_error
end