diff --git a/assets/api_keys_template b/assets/api_keys_template index f462ea7..439fffc 100644 --- a/assets/api_keys_template +++ b/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. # See http://developer.yahoo.com/faq/index.html#appid # and http://developer.yahoo.com/maps/rest/V1/geocode.html diff --git a/init.rb b/init.rb index 2b90c7e..e324a0d 100644 --- a/init.rb +++ b/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/ip_geocode_lookup' require File.dirname(__FILE__) + '/lib/geo_kit/geocoders' diff --git a/lib/geo_kit/acts_as_mappable.rb b/lib/geo_kit/acts_as_mappable.rb index f7738ac..13282e6 100644 --- a/lib/geo_kit/acts_as_mappable.rb +++ b/lib/geo_kit/acts_as_mappable.rb @@ -28,7 +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. 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 = {}) @@ -41,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] || :miles - self.default_formula = options[:default_formula] || :sphere + 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 diff --git a/lib/geo_kit/mappable.rb b/lib/geo_kit/mappable.rb index 51ff514..13455ca 100644 --- a/lib/geo_kit/mappable.rb +++ b/lib/geo_kit/mappable.rb @@ -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 (:miles is the default) - # :formula - valid values are :flat or :sphere (:sphere 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] || :miles - formula = options[:formula] || :sphere + units = options[:units] || GeoKit::DEFAULT_UNITS + formula = options[:formula] || GeoKit::DEFAULT_FORMULA case formula when :sphere units_sphere_multiplier(units) * diff --git a/test/acts_as_mappable_test.rb b/test/acts_as_mappable_test.rb index a13bcc4..9710612 100644 --- a/test/acts_as_mappable_test.rb +++ b/test/acts_as_mappable_test.rb @@ -66,6 +66,20 @@ def setup @custom_loc_e = custom_locations(:e) 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 GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a) GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a) diff --git a/test/fixtures/companies.yml b/test/fixtures/companies.yml index 153e2f2..d48a2e8 100644 --- a/test/fixtures/companies.yml +++ b/test/fixtures/companies.yml @@ -1,5 +1,7 @@ starbucks: id: 1 + name: Starbucks barnes_and_noble: - id: 2 \ No newline at end of file + id: 2 + name: Barnes & Noble \ No newline at end of file diff --git a/test/ipgeocoder_test.rb b/test/ipgeocoder_test.rb index cb046a3..89ea91c 100644 --- a/test/ipgeocoder_test.rb +++ b/test/ipgeocoder_test.rb @@ -16,6 +16,13 @@ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all Longitude: -88.4588 EOF + UNICODED=<<-EOF + Country: SWEDEN (SE) + City: Borås + Latitude: 57.7167 + Longitude: 12.9167 + EOF + def setup super @success.provider = "hostip" @@ -36,6 +43,21 @@ def test_successful_lookup assert location.success 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 failure = MockSuccess.new failure.expects(:body).returns(FAILURE)