Permalink
Browse files

Applying defaults patch to eliminate startup errors. Extending patch …

…to include Geocoder settings.

git-svn-id: http://geokit.rubyforge.org/svn/trunk@30 9265c765-0211-4c68-b2df-6d1bd6e20c4d
  • Loading branch information...
bill_eisenhauer
bill_eisenhauer committed May 22, 2007
1 parent 075d435 commit a7e2d05daec78d41fba15a2ab2505334161a96b0
View
2 README
@@ -22,7 +22,7 @@ package.
## A NOTE ON TERMINOLOGY
-Throughout the code and API of this, latitude and longitude are refered to as lat
+Throughout the code and API of this, latitude and longitude are referred to as lat
and lng. We've found over the long term the abbreviation saves lots of typing time.
## DISTANCE CALCULATIONS AND QUERIES
View
@@ -1,36 +1,36 @@
# These defaults are used in GeoKit::Mappable.distance_to and in acts_as_mappable
-GeoKit::DEFAULT_UNITS = :miles
-GeoKit::DEFAULT_FORMULA = :sphere
+GeoKit::default_units = :miles
+GeoKit::default_formula = :sphere
# This is your yahoo application key for the Yahoo Geocoder.
# See http://developer.yahoo.com/faq/index.html#appid
# and http://developer.yahoo.com/maps/rest/V1/geocode.html
-GeoKit::Geocoders::YAHOO='REPLACE_WITH_YOUR_YAHOO_KEY'
+GeoKit::Geocoders::yahoo='REPLACE_WITH_YOUR_YAHOO_KEY'
# This is your Google Maps geocoder key.
# See http://www.google.com/apis/maps/signup.html
# and http://www.google.com/apis/maps/documentation/#Geocoding_Examples
-GeoKit::Geocoders::GOOGLE='REPLACE_WITH_YOUR_GOOGLE_KEY'
+GeoKit::Geocoders::google='REPLACE_WITH_YOUR_GOOGLE_KEY'
# This is your username and password for geocoder.us.
# To use the free service, the value can be set to nil or false. For
# usage tied to an account, the value should be set to username:password.
# See http://geocoder.us
# and http://geocoder.us/user/signup
-GeoKit::Geocoders::GEOCODER_US=false
+GeoKit::Geocoders::geocoder_us=false
# This is your authorization key for geocoder.ca.
# To use the free service, the value can be set to nil or false. For
# usage tied to an account, set the value to the key obtained from
# Geocoder.ca.
# See http://geocoder.ca
# and http://geocoder.ca/?register=1
-GeoKit::Geocoders::GEOCODER_CA=false
+GeoKit::Geocoders::geocoder_ca=false
# This is the order in which the geocoders are called in a failover scenario
# If you only want to use a single geocoder, put a single symbol in the array.
# Valid symbols are :google, :yahoo, :us, and :ca.
# Be aware that there are Terms of Use restrictions on how you can use the
# various geocoders. Make sure you read up on relevant Terms of Use for each
# geocoder you are going to use.
-GeoKit::Geocoders::PROVIDER_ORDER=[:google,:us]
+GeoKit::Geocoders::provider_order=[:google,:us]
@@ -28,8 +28,8 @@ def self.included(base) # :nodoc:
module ClassMethods # :nodoc:
# Class method to bring distance query support into ActiveRecord models. By default
# uses :miles for distance units and performs calculations based upon the Haversine
- # (sphere) formula. These can be changed by setting GeoKit::DEFAULT_UNITS and
- # GeoKit::DEFAULT_FORMULA. Also, by default, uses lat, lng, and distance for respective
+ # (sphere) formula. These can be changed by setting GeoKit::default_units and
+ # GeoKit::default_formula. Also, by default, uses lat, lng, and distance for respective
# column names. All of these can be overridden using the :default_units, :default_formula,
# :lat_column_name, :lng_column_name, and :distance_column_name hash keys.
def acts_as_mappable(options = {})
@@ -42,8 +42,8 @@ def acts_as_mappable(options = {})
# Handle class variables.
cattr_accessor :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name
self.distance_column_name = options[:distance_column_name] || 'distance'
- self.default_units = options[:default_units] || GeoKit::DEFAULT_UNITS
- self.default_formula = options[:default_formula] || GeoKit::DEFAULT_FORMULA
+ self.default_units = options[:default_units] || GeoKit::default_units
+ self.default_formula = options[:default_formula] || GeoKit::default_formula
self.lat_column_name = options[:lat_column_name] || 'lat'
self.lng_column_name = options[:lng_column_name] || 'lng'
end
View
@@ -1,5 +1,21 @@
module GeoKit
# These defaults are used in GeoKit::Mappable.distance_to and in acts_as_mappable
- DEFAULT_UNITS = :miles
- DEFAULT_FORMULA = :sphere
-end
+ @@default_units = :miles
+ @@default_formula = :sphere
+
+ [:default_units, :default_formula].each do |sym|
+ class_eval <<-EOS, __FILE__, __LINE__
+ def self.#{sym}
+ if defined?(#{sym.to_s.upcase})
+ #{sym.to_s.upcase}
+ else
+ @@#{sym}
+ end
+ end
+
+ def self.#{sym}=(obj)
+ @@#{sym} = obj
+ end
+ EOS
+ end
+end
View
@@ -15,6 +15,28 @@ module GeoKit
# Some configuration is required for these geocoders and can be located in the environment
# configuration files.
module Geocoders
+ @@yahoo = 'REPLACE_WITH_YOUR_YAHOO_KEY'
+ @@google = 'REPLACE_WITH_YOUR_GOOGLE_KEY'
+ @@geocoder_us = false
+ @@geocoder_ca = false
+ @@provider_order = [:google,:us]
+
+ [:yahoo, :google, :geocoder_us, :geocoder_ca, :provider_order].each do |sym|
+ class_eval <<-EOS, __FILE__, __LINE__
+ def self.#{sym}
+ if defined?(#{sym.to_s.upcase})
+ #{sym.to_s.upcase}
+ else
+ @@#{sym}
+ end
+ end
+
+ def self.#{sym}=(obj)
+ @@#{sym} = obj
+ end
+ EOS
+ end
+
# Error which is thrown in the event a geocoding error occurs.
class GeocodeError < StandardError; end
@@ -89,7 +111,7 @@ def self.construct_request(location)
url += add_ampersand(url) + "city=#{CGI.escape(location.city)}" if location.city
url += add_ampersand(url) + "prov=#{location.state}" if location.state
url += add_ampersand(url) + "postal=#{location.zip}" if location.zip
- url += add_ampersand(url) + "auth=#{GeoKit::Geocoders::GEOCODER_CA}" if GeoKit::Geocoders::GEOCODER_CA
+ url += add_ampersand(url) + "auth=#{GeoKit::Geocoders::geocoder_ca}" if GeoKit::Geocoders::geocoder_ca
url += add_ampersand(url) + "geoit=xml"
'http://geocoder.ca/?' + url
end
@@ -108,7 +130,7 @@ class GoogleGeocoder < Geocoder
# Template method which does the geocode lookup.
def self.do_geocode(address)
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
- res = Net::HTTP.get_response(URI.parse("http://maps.google.com/maps/geo?q=#{CGI.escape(address_str)}&output=xml&key=#{GeoKit::Geocoders::GOOGLE}&oe=utf-8"))
+ res = Net::HTTP.get_response(URI.parse("http://maps.google.com/maps/geo?q=#{CGI.escape(address_str)}&output=xml&key=#{GeoKit::Geocoders::google}&oe=utf-8"))
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml=res.body
logger.debug "Google geocoding. Address: #{address}. Result: #{xml}"
@@ -200,7 +222,7 @@ class UsGeocoder < Geocoder
# For now, the geocoder_method will only geocode full addresses -- not zips or cities in isolation
def self.do_geocode(address)
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
- url = "http://"+(GeoKit::Geocoders::GEOCODER_US || '')+"geocoder.us/service/csv/geocode?address=#{CGI.escape(address_str)}"
+ url = "http://"+(GeoKit::Geocoders::geocoder_us || '')+"geocoder.us/service/csv/geocode?address=#{CGI.escape(address_str)}"
res = Net::HTTP.get_response(URI.parse(url))
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
data = res.body
@@ -232,7 +254,7 @@ class YahooGeocoder < Geocoder
# Template method which does the geocode lookup.
def self.do_geocode(address)
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
- url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=#{GeoKit::Geocoders::YAHOO}&location=#{CGI.escape(address_str)}"
+ url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=#{GeoKit::Geocoders::yahoo}&location=#{CGI.escape(address_str)}"
res = Net::HTTP.get_response(URI.parse(url))
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml = res.body
@@ -285,13 +307,13 @@ class MultiGeocoder < Geocoder
# The failover approach is crucial for production-grade apps, but is rarely used.
# 98% of your geocoding calls will be successful with the first call
def self.do_geocode(address)
- GeoKit::Geocoders::PROVIDER_ORDER.each do |provider|
+ GeoKit::Geocoders::provider_order.each do |provider|
begin
klass=GeoKit::Geocoders.const_get "#{provider.to_s.capitalize}Geocoder"
res = klass.send :geocode, address
return res if res.success
rescue
- logger.error("Something has gone very wrong during geocoding, OR you have configured an invalid class name in GeoKit::Geocoders::PROVIDERS. Address: #{address}. Provider: #{provider}")
+ logger.error("Something has gone very wrong during geocoding, OR you have configured an invalid class name in GeoKit::Geocoders::provider_order. Address: #{address}. Provider: #{provider}")
end
end
# If we get here, we failed completely.
View
@@ -26,13 +26,13 @@ def self.included(receiver) # :nodoc:
module ClassMethods #:nodoc:
# Returns the distance between two points. The from and to parameters are
# required to have lat and lng attributes. Valid options are:
- # :units - valid values are :miles or :kms (GeoKit::DEFAULT_UNITS is the default)
- # :formula - valid values are :flat or :sphere (GeoKit::DEFAULT_FORMULA is the default)
+ # :units - valid values are :miles or :kms (GeoKit::default_units is the default)
+ # :formula - valid values are :flat or :sphere (GeoKit::default_formula is the default)
def distance_between(from, to, options={})
from_point = from && from.is_a?(String) ? geocode(from) : from
to_point = to && to.is_a?(String) ? geocode(to) : to
- units = options[:units] || GeoKit::DEFAULT_UNITS
- formula = options[:formula] || GeoKit::DEFAULT_FORMULA
+ units = options[:units] || GeoKit::default_units
+ formula = options[:formula] || GeoKit::default_formula
case formula
when :sphere
units_sphere_multiplier(units) *
@@ -17,7 +17,7 @@
# Establish test tables.
load(File.dirname(__FILE__) + "/schema.rb")
-GeoKit::Geocoders::PROVIDER_ORDER=[:google,:us]
+GeoKit::Geocoders::provider_order=[:google,:us]
# Uses defaults
class Company < ActiveRecord::Base #:nodoc: all
View
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/base_geocoder_test.rb'
-GeoKit::Geocoders::GEOCODER_CA = "SOMEKEYVALUE"
+GeoKit::Geocoders::geocoder_ca = "SOMEKEYVALUE"
class CaGeocoderTest < BaseGeocoderTest #:nodoc: all
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/base_geocoder_test.rb'
-GeoKit::Geocoders::GOOGLE = 'Google'
+GeoKit::Geocoders::google = 'Google'
class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/base_geocoder_test.rb'
-GeoKit::Geocoders::PROVIDER_ORDER=[:google,:yahoo,:us]
+GeoKit::Geocoders::provider_order=[:google,:yahoo,:us]
class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
@@ -35,10 +35,10 @@ def test_failure
end
def test_invalid_provider
- temp=GeoKit::Geocoders::PROVIDER_ORDER
- GeoKit::Geocoders.const_set :PROVIDER_ORDER,[:bogus]
+ temp = GeoKit::Geocoders::provider_order
+ GeoKit::Geocoders.provider_order = [:bogus]
assert_equal @failure, GeoKit::Geocoders::MultiGeocoder.geocode(@address)
- GeoKit::Geocoders.const_set :PROVIDER_ORDER,temp
+ GeoKit::Geocoders.provider_order = temp
end
end
View
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/base_geocoder_test.rb'
-GeoKit::Geocoders::GEOCODER_US = nil
+GeoKit::Geocoders::geocoder_us = nil
class UsGeocoderTest < BaseGeocoderTest #:nodoc: all
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/base_geocoder_test.rb'
-GeoKit::Geocoders::YAHOO = 'Yahoo'
+GeoKit::Geocoders::yahoo = 'Yahoo'
class YahooGeocoderTest < BaseGeocoderTest #:nodoc: all
YAHOO_FULL=<<-EOF.strip

0 comments on commit a7e2d05

Please sign in to comment.