Module: Drifter
- Defined in:
- 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
Defined Under Namespace
Modules: Geocoders Classes: Location
Constant Summary collapse
- VERSION =
"0.2.0"
- @@default_geocoder =
:google
- @@last_error =
nil
Class Method Summary collapse
-
.default_geocoder ⇒ Object
returns the default geocoder.
-
.default_geocoder=(value) ⇒ Object
Sets the default geocoder.
-
.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().
-
.extract_latlng!(loc) ⇒ Object
Same as Drifter,extract_latlng() but raises ArgumentError on failure.
-
.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.
-
.last_error ⇒ Object
Returns a Hash containing error code and status from a failed geocoding request.
Class Method Details
.default_geocoder ⇒ Object
returns the default geocoder
12 13 14 |
# File 'lib/drifter.rb', line 12 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=()
20 21 22 |
# File 'lib/drifter.rb', line 20 def self.default_geocoder=(value) @@default_geocoder = 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
27 28 29 30 31 |
# File 'lib/drifter.rb', line 27 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
35 36 37 38 39 |
# File 'lib/drifter.rb', line 35 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
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/drifter.rb', line 57 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_error ⇒ Object
Returns a Hash containing error code and status from a failed geocoding request
73 74 75 |
# File 'lib/drifter.rb', line 73 def self.last_error @@last_error end |