module Geocoder

def address(query)


or IP address (string).
Look up the address of the given coordinates ([lat,lon])
#
def address(query)
  if (results = search(query)).size > 0
    results.first.address
  end
end

def blank_query?(value)


Is the given search query blank? (ie, should we not bother searching?)
#
def blank_query?(value)
  !!value.to_s.match(/^\s*$/)
end

def cache


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

def coordinates(address)


Look up the coordinates of the given street or IP address.
#
def coordinates(address)
  if (results = search(address)).size > 0
    results.first.coordinates
  end
end

def get_lookup(name)


Retrieve a Lookup object from the store.
#
def get_lookup(name)
  unless defined?(@lookups)
    @lookups = {}
  end
  unless @lookups.include?(name)
    @lookups[name] = spawn_lookup(name)
  end
  @lookups[name]
end

def ip_address?(value)


dot-delimited 8-bit numbers.
Does not check for actual validity, just the appearance of four

Does the given value look like an IP address?
#
def ip_address?(value)
  !!value.to_s.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
end

def ip_lookups


All IP address lookups, default first.
#
def ip_lookups
  [:freegeoip]
end

def lookup(query)


depending on the query contents.
Takes a search query and returns an IP or street address Lookup
Get a Lookup object (which communicates with the remote geocoding API).
#
def lookup(query)
  if ip_address?(query)
    get_lookup(ip_lookups.first)
  else
    get_lookup(Configuration.lookup || street_lookups.first)
  end
end

def search(query)


Search for information about an address or a set of coordinates.
#
def search(query)
  blank_query?(query) ? [] : lookup(query).search(query)
end

def spawn_lookup(name)


Spawn a Lookup of the given 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
    eval("Geocoder::Lookup::#{klass}.new")
  else
    valids = valid_lookups.map{ |l| ":#{l}" }.join(", ")
    raise ConfigurationError, "Please specify a valid lookup for Geocoder " +
      "(#{name.inspect} is not one of: #{valids})."
  end
end

def street_lookups


All street address lookups, default first.
#
def street_lookups
  [:google, :yahoo, :bing, :geocoder_ca, :yandex]
end

def valid_lookups


Array of valid Lookup names.
#
def valid_lookups
  street_lookups + ip_lookups
end