module Geomancer
Constants
- LAT_LONG_RE
- VERSION
Public Class Methods
configure(engine, opts={})
click to toggle source
Your code goes hereā¦
# File lib/geomancer.rb, line 6 def self.configure(engine, opts={}) @engine = engine @options = opts case @engine when :zip_code then require_relative './geomancer/zip_code' else require 'geocoder' cache = opts.delete(:cache) geocoder_config = { :engine => opts } geocoder_config[:cache] = cache if cache Geocoder.configure(geocoder_config) end end
extract_lat_and_long(address)
click to toggle source
# File lib/geomancer.rb, line 26 def self.extract_lat_and_long address matches = LAT_LONG_RE.match(address) return nil if !matches latitude = matches["latitude"] longitude = matches["longitude"] return nil if !(latitude && longitude) return {:latitude => latitude.to_f, :longitude => longitude.to_f} end
geolocate(address)
click to toggle source
# File lib/geomancer.rb, line 43 def self.geolocate address return Geomancer::ZipCode.geolocate(address) if zip_code_only?(address) lat_and_long = extract_lat_and_long(address) return lat_and_long if lat_and_long if !@engine warn("No configuration provided. Defaulting to zip code geolocator") configure(:zip_code) end case @engine when :zip_code then Geomancer::ZipCode.geolocate(address) else address = address.gsub(/[\r\n]/,", ") # Yahoo doesn't like newlines results = Geocoder.search(address) return nil if results.empty? first_result = results.first {:latitude => first_result.latitude, :longitude => first_result.longitude} end end
zip_code_only?(address)
click to toggle source
# File lib/geomancer.rb, line 39 def self.zip_code_only? address address.match(/^\d\d\d\d\d(?:-\d\d\d\d)?$/) end