Skip to content

Commit

Permalink
Added configuration for defaults and units in environment.rb. Added t…
Browse files Browse the repository at this point in the history
…ests for unicode in ip geocoding as well as :include support and overriding default_units.

git-svn-id: http://geokit.rubyforge.org/svn/trunk@24 9265c765-0211-4c68-b2df-6d1bd6e20c4d
  • Loading branch information
bill_eisenhauer committed Mar 13, 2007
1 parent b68d984 commit 7a1c470
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
4 changes: 4 additions & 0 deletions assets/api_keys_template
@@ -1,3 +1,7 @@
# These defaults are used in GeoKit::Mappable.distance_to and in acts_as_mappable
GeoKit::DEFAULT_UNITS = :miles
GeoKit::DEFAULT_FORMULA = :sphere

# This is your yahoo application key for the Yahoo Geocoder. # This is your yahoo application key for the Yahoo Geocoder.
# See http://developer.yahoo.com/faq/index.html#appid # See http://developer.yahoo.com/faq/index.html#appid
# and http://developer.yahoo.com/maps/rest/V1/geocode.html # and http://developer.yahoo.com/maps/rest/V1/geocode.html
Expand Down
1 change: 1 addition & 0 deletions init.rb
@@ -1,3 +1,4 @@
require File.dirname(__FILE__) + '/lib/geo_kit/defaults'
require File.dirname(__FILE__) + '/lib/geo_kit/acts_as_mappable' require File.dirname(__FILE__) + '/lib/geo_kit/acts_as_mappable'
require File.dirname(__FILE__) + '/lib/geo_kit/ip_geocode_lookup' require File.dirname(__FILE__) + '/lib/geo_kit/ip_geocode_lookup'
require File.dirname(__FILE__) + '/lib/geo_kit/geocoders' require File.dirname(__FILE__) + '/lib/geo_kit/geocoders'
Expand Down
7 changes: 4 additions & 3 deletions lib/geo_kit/acts_as_mappable.rb
Expand Up @@ -28,7 +28,8 @@ def self.included(base) # :nodoc:
module ClassMethods # :nodoc: module ClassMethods # :nodoc:
# Class method to bring distance query support into ActiveRecord models. By default # Class method to bring distance query support into ActiveRecord models. By default
# uses :miles for distance units and performs calculations based upon the Haversine # uses :miles for distance units and performs calculations based upon the Haversine
# (sphere) 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, # 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. # :lat_column_name, :lng_column_name, and :distance_column_name hash keys.
def acts_as_mappable(options = {}) def acts_as_mappable(options = {})
Expand All @@ -41,8 +42,8 @@ def acts_as_mappable(options = {})
# Handle class variables. # Handle class variables.
cattr_accessor :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name 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.distance_column_name = options[:distance_column_name] || 'distance'
self.default_units = options[:default_units] || :miles self.default_units = options[:default_units] || GeoKit::DEFAULT_UNITS
self.default_formula = options[:default_formula] || :sphere self.default_formula = options[:default_formula] || GeoKit::DEFAULT_FORMULA
self.lat_column_name = options[:lat_column_name] || 'lat' self.lat_column_name = options[:lat_column_name] || 'lat'
self.lng_column_name = options[:lng_column_name] || 'lng' self.lng_column_name = options[:lng_column_name] || 'lng'
end end
Expand Down
8 changes: 4 additions & 4 deletions lib/geo_kit/mappable.rb
Expand Up @@ -26,13 +26,13 @@ def self.included(receiver) # :nodoc:
module ClassMethods #:nodoc: module ClassMethods #:nodoc:
# Returns the distance between two points. The from and to parameters are # Returns the distance between two points. The from and to parameters are
# required to have lat and lng attributes. Valid options are: # required to have lat and lng attributes. Valid options are:
# :units - valid values are :miles or :kms (:miles is the default) # :units - valid values are :miles or :kms (GeoKit::DEFAULT_UNITS is the default)
# :formula - valid values are :flat or :sphere (:sphere is the default) # :formula - valid values are :flat or :sphere (GeoKit::DEFAULT_FORMULA is the default)
def distance_between(from, to, options={}) def distance_between(from, to, options={})
from_point = from && from.is_a?(String) ? geocode(from) : from from_point = from && from.is_a?(String) ? geocode(from) : from
to_point = to && to.is_a?(String) ? geocode(to) : to to_point = to && to.is_a?(String) ? geocode(to) : to
units = options[:units] || :miles units = options[:units] || GeoKit::DEFAULT_UNITS
formula = options[:formula] || :sphere formula = options[:formula] || GeoKit::DEFAULT_FORMULA
case formula case formula
when :sphere when :sphere
units_sphere_multiplier(units) * units_sphere_multiplier(units) *
Expand Down
14 changes: 14 additions & 0 deletions test/acts_as_mappable_test.rb
Expand Up @@ -66,6 +66,20 @@ def setup
@custom_loc_e = custom_locations(:e) @custom_loc_e = custom_locations(:e)
end end


def test_override_default_units_the_hard_way
Location.default_units = :kms
locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
assert_equal 5, locations.size
Location.default_units = :miles
end

def test_include
locations = Location.find(:all, :origin => @loc_a, :include => :company, :conditions => "company_id = 1")
assert !locations.empty?
assert_equal 1, locations[0].company.id
assert_equal 'Starbucks', locations[0].company.name
end

def test_distance_between_geocoded def test_distance_between_geocoded
GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a) GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a) GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a)
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/companies.yml
@@ -1,5 +1,7 @@
starbucks: starbucks:
id: 1 id: 1
name: Starbucks


barnes_and_noble: barnes_and_noble:
id: 2 id: 2
name: Barnes & Noble
22 changes: 22 additions & 0 deletions test/ipgeocoder_test.rb
Expand Up @@ -16,6 +16,13 @@ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
Longitude: -88.4588 Longitude: -88.4588
EOF EOF


UNICODED=<<-EOF
Country: SWEDEN (SE)
City: Borås
Latitude: 57.7167
Longitude: 12.9167
EOF

def setup def setup
super super
@success.provider = "hostip" @success.provider = "hostip"
Expand All @@ -36,6 +43,21 @@ def test_successful_lookup
assert location.success assert location.success
end end


def test_unicoded_lookup
success = MockSuccess.new
success.expects(:body).returns(UNICODED)
Net::HTTP.expects(:get_response).with('api.hostip.info', '/get_html.php?ip=12.215.42.19&position=true').returns(success)
location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
assert_not_nil location
assert_equal 57.7167, location.lat
assert_equal 12.9167, location.lng
assert_equal "Borås", location.city
assert_nil location.state
assert_equal "SE", location.country_code
assert_equal "hostip", location.provider
assert location.success
end

def test_failed_lookup def test_failed_lookup
failure = MockSuccess.new failure = MockSuccess.new
failure.expects(:body).returns(FAILURE) failure.expects(:body).returns(FAILURE)
Expand Down

0 comments on commit 7a1c470

Please sign in to comment.