Newer
Older
require "geocoder/configuration"
require "geocoder/models/active_record" if defined?(::ActiveRecord)
require "geocoder/models/mongoid" if defined?(::Mongoid)
require "geocoder/models/mongo_mapper" if defined?(::MongoMapper)
module Geocoder
extend self
##
# Search for information about an address or a set of coordinates.
def search(query)
blank_query?(query) ? [] : lookup(query).search(query)
# Look up the coordinates of the given street or IP address.
if (results = search(address)).size > 0
results.first.coordinates
# Look up the address of the given coordinates ([lat,lon])
# or IP address (string).
if (results = search(query)).size > 0
##
# The working Cache object, or +nil+ if none configured.
#
def cache
if @cache.nil? and store = Configuration.cache
@cache = Cache.new(store, Configuration.cache_prefix)
end
@cache
end
##
# Array of valid Lookup names.
#
def valid_lookups
street_lookups + ip_lookups
end
##
# All street address lookups, default first.
#
def street_lookups
[:google, :google_premier, :yahoo, :bing, :geocoder_ca, :yandex, :nominatim]
end
##
# All IP address lookups, default first.
#
def ip_lookups
[:freegeoip]
private # -----------------------------------------------------------------
# Get a Lookup object (which communicates with the remote geocoding API).
# Takes a search query and returns an IP or street address Lookup
# depending on the query contents.
def lookup(query)
if ip_address?(query)
get_lookup(Configuration.lookup || street_lookups.first)
James McCarthy
committed
@lookups = {} unless defined?(@lookups)
@lookups[name] = spawn_lookup(name) unless @lookups.include?(name)
def spawn_lookup(name)
if valid_lookups.include?(name)
name = name.to_s
require "geocoder/lookups/#{name}"
klass = name.split("_").map{ |i| i[0...1].upcase + i[1..-1] }.join
James McCarthy
committed
Geocoder::Lookup.const_get(klass).new
valids = valid_lookups.map(&:inspect).join(", ")
raise ConfigurationError, "Please specify a valid lookup for Geocoder " +
"(#{name.inspect} is not one of: #{valids})."
##
# Does the given value look like an IP address?
#
# Does not check for actual validity, just the appearance of four
!!value.to_s.match(/^(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
##
# Is the given search query blank? (ie, should we not bother searching?)
#
def blank_query?(value)
# load Railtie if Rails exists
if defined?(Rails)
require "geocoder/railtie"
Geocoder::Railtie.insert
end