Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added timeout and proxy support. Added count support. Fixed rake-base…

…d test invocation. Fixed exclusive range condition bug. Fixed default ip address for localhost

git-svn-id: http://geokit.rubyforge.org/svn/trunk@32 9265c765-0211-4c68-b2df-6d1bd6e20c4d
  • Loading branch information...
commit e495d3dea2e6a8899fdba4620c45dcfade68620d 1 parent ed6cc61
bill_eisenhauer authored
View
22 README
@@ -80,11 +80,11 @@ different combinations:
Origin as a two-element array of latititude/longitude:
- find(:all, :origin=>[37.792,-122.393])
+ find(:all, :origin => [37.792,-122.393])
Origin as a geocodeable string:
- find(:all, :origin=>'100 Spear st, San Francisco, CA')
+ find(:all, :origin => '100 Spear st, San Francisco, CA')
Origin as an object which responds to lat and lng methods,
or latitude and longitude methods, or whatever methods you have
@@ -97,7 +97,7 @@ formula for the distance field in the conditions clause. This saves
from having to add complicated SQL in the conditions clause. The result
set returns model instances that are less than 5 units away.
- find(:all, :origin, :conditions => "distance < 5")
+ find(:all, :origin => @somewhere, :conditions => "distance < 5")
NOTE: conditions can also be compound as in
:conditions => "distance < 100 and state ='TX'".
@@ -108,10 +108,10 @@ from the :options hash prior to invoking the superclass behavior.
Other convenience methods work intuitively and are as follows:
- find_within(distance, options={})
- find_beyond(distance, options={})
- find_closest(options={})
- find_farthest(options={})
+ find_within(distance, :origin => @somewhere)
+ find_beyond(distance, :origin => @somewhere)
+ find_closest(:origin => @somewhere)
+ find_farthest(:origin => @somewhere)
where the options respect the defaults, but can be overridden if
desired.
@@ -123,6 +123,14 @@ calculations, you can use the following:
Thereafter, you are free to use it in find_by_sql as you wish.
+There are methods available to enable you to get the count based upon
+the find condition that you have provided. These all work similarly to
+the finders. So for instance:
+
+ count(:origin, :conditions => "distance < 5")
+ count_within(distance, :origin => @somewhere)
+ count_beyond(distance, :origin => @somewhere)
+
## IP GEOCODING
You can obtain the location for an IP at any time using the geocoder
View
10 rakefile.rb → Rakefile
@@ -5,18 +5,18 @@
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the acts_as_mappable plugin.'
+desc 'Test the GeoKit plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
-desc 'Generate documentation for the plugin.'
+desc 'Generate documentation for the GeoKit plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'acts_as_mappable-doc'
- rdoc.title = 'GM'
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'GeoKit'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
-end
+end
View
11 about.yml
@@ -1,8 +1,9 @@
+author:
+ name_1: Bill Eisenhauer
+ homepage_1: http://blog.billeisenhauer.com
+ name_2: Andre Lewis
+ homepage_2: http://www.earthcode.com
summary: Geo distance calculations, distance calculation query support, geocoding for physical and ip addresses.
-version: 0.1
-author_1: Bill Eisenhauer
-homepage_1: http://www.billeisenhauer.com
-author_2: Andre Lewis
-homepage_andre: http://www.earthcode.com
+version: 1.0
rails_version: 1.0+
license: MIT
View
24 assets/api_keys_template
@@ -2,22 +2,36 @@
GeoKit::default_units = :miles
GeoKit::default_formula = :sphere
+# This is the timeout value in seconds to be used for calls to the geocoder web
+# services. For no timeout at all, comment out the setting. The timeout unit
+# is in seconds.
+GeoKit::Geocoders::timeout = 3
+
+# These settings are used if web service calls must be routed through a proxy.
+# These setting can be nil if not needed, otherwise, addr and port must be
+# filled in at a minimum. If the proxy requires authentication, the username
+# and password can be provided as well.
+GeoKit::Geocoders::proxy_addr = nil
+GeoKit::Geocoders::proxy_port = nil
+GeoKit::Geocoders::proxy_user = nil
+GeoKit::Geocoders::proxy_pass = nil
+
# 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
@@ -25,7 +39,7 @@ GeoKit::Geocoders::geocoder_us=false
# 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.
@@ -33,4 +47,4 @@ GeoKit::Geocoders::geocoder_ca=false
# 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]
View
16 init.rb
@@ -1,7 +1,13 @@
-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'
-require File.dirname(__FILE__) + '/lib/geo_kit/mappable'
+# Load modules and classes needed to automatically mix in ActiveRecord and
+# ActionController helpers. All other functionality must be explicitly
+# required.
+require 'geo_kit/defaults'
+require 'geo_kit/mappable'
+require 'geo_kit/acts_as_mappable'
+require 'geo_kit/ip_geocode_lookup'
+
+# Automatically mix in distance finder support into ActiveRecord classes.
ActiveRecord::Base.send :include, GeoKit::ActsAsMappable
+
+# Automatically mix in ip geocoding helpers into ActionController classes.
ActionController::Base.send :include, GeoKit::IpGeocodeLookup
View
1  install.rb
@@ -1,3 +1,4 @@
+# Display to the console the contents of the README file.
puts IO.read(File.join(File.dirname(__FILE__), 'README'))
# Append the contents of api_keys_template to the application's environment.rb file
View
63 lib/geo_kit/acts_as_mappable.rb
@@ -16,6 +16,8 @@ module GeoKit
# * find_closest (alias: find_nearest)
# * find_farthest
#
+ # Counter methods are available and work similarly to finders.
+ #
# If raw SQL is desired, the distance_sql method can be used to obtain SQL appropriate
# to use in a find_by_sql call.
module ActsAsMappable
@@ -64,18 +66,19 @@ module SingletonMethods # :nodoc:
# conditions, substitutes the distance sql for the distance column -- this saves
# having to write the gory SQL.
def find(*args)
- options = extract_options_from_args!(args)
- origin = extract_origin_from_options(options)
- units = extract_units_from_options(options)
- formula = extract_formula_from_options(options)
- add_distance_to_select(options, origin, units, formula) if origin
- apply_find_scope(args, options)
- apply_distance_scope(options)
- substitute_distance_in_conditions(options, origin, units, formula) if origin && options.has_key?(:conditions)
- args.push(options)
+ prepare_for_find_or_count(:find, args)
super(*args)
end
+ # Extends the existing count method by:
+ # - If a mappable instance exists in the options and the distance column exists in the
+ # conditions, substitutes the distance sql for the distance column -- this saves
+ # having to write the gory SQL.
+ def count(*args)
+ prepare_for_find_or_count(:count, args)
+ super(*args)
+ end
+
# Finds within a distance radius.
def find_within(distance, options={})
options[:within] = distance
@@ -107,6 +110,26 @@ def find_farthest(options={})
find(:farthest, options)
end
+ # counts within a distance radius.
+ def count_within(distance, options={})
+ options[:within] = distance
+ count(options)
+ end
+ alias count_inside count_within
+
+ # Counts beyond a distance radius.
+ def count_beyond(distance, options={})
+ options[:beyond] = distance
+ count(options)
+ end
+ alias count_outside count_beyond
+
+ # Counts according to a range. Accepts inclusive or exclusive ranges.
+ def count_by_range(range, options={})
+ options[:range] = range
+ count(options)
+ end
+
# Returns the distance calculation to be used as a display column or a condition. This
# is provide for anyone wanting access to the raw SQL.
def distance_sql(origin, units=default_units, formula=default_formula)
@@ -121,6 +144,26 @@ def distance_sql(origin, units=default_units, formula=default_formula)
private
+ # Prepares either a find or a count action by parsing through the options and
+ # conditionally adding to the select clause for finders.
+ def prepare_for_find_or_count(action, args)
+ options = extract_options_from_args!(args)
+ # Obtain items affecting distance condition.
+ origin = extract_origin_from_options(options)
+ units = extract_units_from_options(options)
+ formula = extract_formula_from_options(options)
+ # Apply select adjustments based upon action.
+ add_distance_to_select(options, origin, units, formula) if origin && action == :find
+ # Apply distance scoping and perform substitutions.
+ apply_distance_scope(options)
+ substitute_distance_in_conditions(options, origin, units, formula) if origin && options.has_key?(:conditions)
+ # Order by scoping for find action.
+ apply_find_scope(args, options) if action == :find
+ # Restore options minus the extra options that we used for the
+ # GeoKit API.
+ args.push(options)
+ end
+
# Looks for mapping-specific tokens and makes appropriate translations so that the
# original finder has its expected arguments. Resets the the scope argument to
# :first and ensures the limit is set to one.
@@ -142,7 +185,7 @@ def apply_find_scope(args, options)
def apply_distance_scope(options)
distance_condition = "#{distance_column_name} <= #{options[:within]}" if options.has_key?(:within)
distance_condition = "#{distance_column_name} > #{options[:beyond]}" if options.has_key?(:beyond)
- distance_condition = "#{distance_column_name} >= #{options[:range].first} AND #{distance_column_name} <= #{options[:range].last}" if options.has_key?(:range)
+ distance_condition = "#{distance_column_name} >= #{options[:range].first} AND #{distance_column_name} <#{'=' unless options[:range].exclude_end?} #{options[:range].last}" if options.has_key?(:range)
[:within, :beyond, :range].each { |option| options.delete(option) } if distance_condition
if distance_condition && options.has_key?(:conditions)
original_conditions = options[:conditions]
View
37 lib/geo_kit/geocoders.rb
@@ -1,6 +1,7 @@
require 'net/http'
require 'rexml/document'
require 'yaml'
+require 'timeout'
module GeoKit
# Contains a set of geocoders which can be used independently if desired. The list contains:
@@ -15,13 +16,19 @@ module GeoKit
# Some configuration is required for these geocoders and can be located in the environment
# configuration files.
module Geocoders
+ @@proxy_addr = nil
+ @@proxy_port = nil
+ @@proxy_user = nil
+ @@proxy_pass = nil
+ @@timeout = nil
@@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|
+ [:yahoo, :google, :geocoder_us, :geocoder_ca, :provider_order, :timeout,
+ :proxy_addr, :proxy_port, :proxy_user, :proxy_pass].each do |sym|
class_eval <<-EOS, __FILE__, __LINE__
def self.#{sym}
if defined?(#{sym.to_s.upcase})
@@ -50,6 +57,14 @@ def self.geocode(address)
res = do_geocode(address)
return res.success ? res : GeoLoc.new
end
+
+ # Call the geocoder service using the timeout if configured.
+ def self.call_geocoder_service(url)
+ timeout(GeoKit::Geocoders::timeout) { return self.do_get(url) } if GeoKit::Geocoders::timeout
+ return self.do_get(url)
+ rescue TimeoutError
+ return nil
+ end
protected
@@ -57,6 +72,12 @@ def self.logger() RAILS_DEFAULT_LOGGER; end
private
+ # Wraps the geocoder call around a proxy if necessary.
+ def self.do_get(url)
+ return Net::HTTP::Proxy(GeoKit::Geocoders::proxy_addr, GeoKit::Geocoders::proxy_port,
+ GeoKit::Geocoders::proxy_user, GeoKit::Geocoders::proxy_pass).get_response(URI.parse(url))
+ end
+
# Adds subclass' geocode method making it conveniently available through
# the base class.
def self.inherited(clazz)
@@ -88,7 +109,7 @@ class CaGeocoder < Geocoder
def self.do_geocode(address)
raise ArgumentError('Geocoder.ca requires a GeoLoc argument') unless address.is_a?(GeoLoc)
url = construct_request(address)
- res = Net::HTTP.get_response(URI.parse(url))
+ res = self.call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml = res.body
logger.debug "Geocoder.ca geocoding. Address: #{address}. Result: #{xml}"
@@ -130,7 +151,8 @@ 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 = self.call_geocoder_service("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}"
@@ -183,7 +205,8 @@ class IpGeocoder < Geocoder
# parameter does not match an ip address.
def self.do_geocode(ip)
return GeoLoc.new unless /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
- response = Net::HTTP.get_response('api.hostip.info', "/get_html.php?ip=#{ip}&position=true")
+ url = "http://api.hostip.info/get_html.php?ip=#{ip}&position=true"
+ response = self.call_geocoder_service(url)
response.is_a?(Net::HTTPSuccess) ? parse_body(response.body) : GeoLoc.new
rescue
logger.error "Caught an error during HostIp geocoding call: "+$!
@@ -223,7 +246,7 @@ class UsGeocoder < Geocoder
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)}"
- res = Net::HTTP.get_response(URI.parse(url))
+ res = self.call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
data = res.body
logger.debug "Geocoder.us geocoding. Address: #{address}. Result: #{data}"
@@ -255,7 +278,7 @@ class YahooGeocoder < Geocoder
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)}"
- res = Net::HTTP.get_response(URI.parse(url))
+ res = self.call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml = res.body
doc = REXML::Document.new(xml)
@@ -309,7 +332,7 @@ class MultiGeocoder < Geocoder
def self.do_geocode(address)
GeoKit::Geocoders::provider_order.each do |provider|
begin
- klass=GeoKit::Geocoders.const_get "#{provider.to_s.capitalize}Geocoder"
+ klass = GeoKit::Geocoders.const_get "#{provider.to_s.capitalize}Geocoder"
res = klass.send :geocode, address
return res if res.success
rescue
View
8 lib/geo_kit/ip_geocode_lookup.rb
@@ -37,12 +37,10 @@ def retrieve_location_from_cookie_or_service
return location.success ? location : nil
end
- # Either returns the real ip address or a fake one in the case where
- # doing live testing via localhost.
+ # Returns the real ip address, though this could be the localhost ip
+ # address. No special handling here anymore.
def get_ip_address
-
- address = request.remote_ip
- address == '127.0.0.1' ? '12.215.42.19' : address
+ request.remote_ip
end
end
end
View
2  lib/geo_kit/mappable.rb
@@ -1,3 +1,5 @@
+require 'geo_kit/defaults'
+
module GeoKit
# Contains class and instance methods providing distance calcuation services. This
# module is meant to be mixed into classes containing lat and lng attributes where
View
106 test/acts_as_mappable_test.rb
@@ -1,21 +1,6 @@
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-require 'test/unit'
-require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
-require 'breakpoint'
-require 'active_record/fixtures'
-require 'action_controller/test_process'
require 'rubygems'
require 'mocha'
-
-# Config database connection.
-config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
-ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
-#ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'postgresql'])
-
-
-# Establish test tables.
-load(File.dirname(__FILE__) + "/schema.rb")
+require File.join(File.dirname(__FILE__), 'test_helper')
GeoKit::Geocoders::provider_order=[:google,:us]
@@ -30,6 +15,7 @@ class Location < ActiveRecord::Base #:nodoc: all
acts_as_mappable
end
+# Uses deviations from conventions.
class CustomLocation < ActiveRecord::Base #:nodoc: all
belongs_to :company
acts_as_mappable :distance_column_name => 'dist',
@@ -47,7 +33,11 @@ class ActsAsMappableTest < Test::Unit::TestCase #:nodoc: all
LOCATION_A_IP = "217.10.83.5"
- self.fixture_path = File.dirname(__FILE__) + '/fixtures'
+ #self.fixture_path = File.dirname(__FILE__) + '/fixtures'
+ #self.fixture_path = RAILS_ROOT + '/test/fixtures/'
+ #puts "Rails Path #{RAILS_ROOT}"
+ #puts "Fixture Path: #{self.fixture_path}"
+ #self.fixture_path = ' /Users/bill_eisenhauer/Projects/geokit_test/test/fixtures/'
fixtures :companies, :locations, :custom_locations
def setup
@@ -70,6 +60,8 @@ 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
+ locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
+ assert_equal 5, locations
Location.default_units = :miles
end
@@ -111,66 +103,99 @@ def test_distance_column_in_select
def test_find_with_distance_condition
locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
assert_equal 5, locations.size
+ locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
+ assert_equal 5, locations
end
def test_find_with_distance_condition_with_units_override
locations = Location.find(:all, :origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
assert_equal 5, locations.size
+ locations = Location.count(:origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
+ assert_equal 5, locations
end
def test_find_with_distance_condition_with_formula_override
locations = Location.find(:all, :origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
assert_equal 6, locations.size
+ locations = Location.count(:origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
+ assert_equal 6, locations
end
def test_find_within
locations = Location.find_within(3.97, :origin => @loc_a)
- assert_equal 5, locations.size
+ assert_equal 5, locations.size
+ locations = Location.count_within(3.97, :origin => @loc_a)
+ assert_equal 5, locations
end
def test_find_within_with_token
locations = Location.find(:all, :within => 3.97, :origin => @loc_a)
assert_equal 5, locations.size
+ locations = Location.count(:within => 3.97, :origin => @loc_a)
+ assert_equal 5, locations
end
def test_find_within_with_coordinates
locations = Location.find_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
assert_equal 5, locations.size
+ locations = Location.count_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
+ assert_equal 5, locations
end
def test_find_with_compound_condition
locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
assert_equal 2, locations.size
+ locations = Location.count(:origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
+ assert_equal 2, locations
end
def test_find_with_secure_compound_condition
locations = Location.find(:all, :origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
assert_equal 2, locations.size
+ locations = Location.count(:origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
+ assert_equal 2, locations
end
def test_find_beyond
locations = Location.find_beyond(3.95, :origin => @loc_a)
assert_equal 1, locations.size
+ locations = Location.count_beyond(3.95, :origin => @loc_a)
+ assert_equal 1, locations
end
def test_find_beyond_with_token
locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
assert_equal 1, locations.size
+ locations = Location.count(:beyond => 3.95, :origin => @loc_a)
+ assert_equal 1, locations
end
def test_find_beyond_with_coordinates
locations = Location.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
assert_equal 1, locations.size
+ locations = Location.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
+ assert_equal 1, locations
end
def test_find_range_with_token
locations = Location.find(:all, :range => 0..10, :origin => @loc_a)
assert_equal 6, locations.size
+ locations = Location.count(:range => 0..10, :origin => @loc_a)
+ assert_equal 6, locations
end
def test_find_range_with_token_with_conditions
locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
assert_equal 2, locations.size
+ locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
+ assert_equal 2, locations
+ end
+
+ def test_find_range_with_token_excluding_end
+ locations = Location.find(:all, :range => 0...10, :origin => @loc_a)
+ assert_equal 6, locations.size
+ locations = Location.count(:range => 0...10, :origin => @loc_a)
+ assert_equal 6, locations
end
def test_find_nearest
@@ -207,21 +232,29 @@ def test_scoped_distance_column_in_select
def test_scoped_find_with_distance_condition
locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
assert_equal 4, locations.size
+ locations = @starbucks.locations.count(:origin => @loc_a, :conditions => "distance < 3.97")
+ assert_equal 4, locations
end
def test_scoped_find_within
locations = @starbucks.locations.find_within(3.97, :origin => @loc_a)
assert_equal 4, locations.size
+ locations = @starbucks.locations.count_within(3.97, :origin => @loc_a)
+ assert_equal 4, locations
end
def test_scoped_find_with_compound_condition
locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
assert_equal 2, locations.size
+ locations = @starbucks.locations.count( :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
+ assert_equal 2, locations
end
def test_scoped_find_beyond
locations = @starbucks.locations.find_beyond(3.95, :origin => @loc_a)
- assert_equal 1, locations.size
+ assert_equal 1, locations.size
+ locations = @starbucks.locations.count_beyond(3.95, :origin => @loc_a)
+ assert_equal 1, locations
end
def test_scoped_find_nearest
@@ -244,30 +277,45 @@ def test_ip_geocoded_find_with_distance_condition
GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 3.97")
assert_equal 5, locations.size
+ GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
+ locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 3.97")
+ assert_equal 5, locations
end
def test_ip_geocoded_find_within
GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
locations = Location.find_within(3.97, :origin => LOCATION_A_IP)
assert_equal 5, locations.size
+ GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
+ locations = Location.count_within(3.97, :origin => LOCATION_A_IP)
+ assert_equal 5, locations
end
def test_ip_geocoded_find_with_compound_condition
GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
assert_equal 2, locations.size
+ GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
+ locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
+ assert_equal 2, locations
end
def test_ip_geocoded_find_with_secure_compound_condition
GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
assert_equal 2, locations.size
+ GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
+ locations = Location.count(:origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
+ assert_equal 2, locations
end
def test_ip_geocoded_find_beyond
GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
locations = Location.find_beyond(3.95, :origin => LOCATION_A_IP)
assert_equal 1, locations.size
+ GeoKit::Geocoders::IpGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
+ locations = Location.count_beyond(3.95, :origin => LOCATION_A_IP)
+ assert_equal 1, locations
end
def test_ip_geocoded_find_nearest
@@ -296,41 +344,57 @@ def test_address_geocode
def test_find_with_custom_distance_condition
locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 3.97")
assert_equal 5, locations.size
+ locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 3.97")
+ assert_equal 5, locations
end
def test_find_with_custom_distance_condition_using_custom_origin
locations = CustomLocation.find(:all, :origin => @custom_loc_a, :conditions => "dist < 3.97")
assert_equal 5, locations.size
+ locations = CustomLocation.count(:origin => @custom_loc_a, :conditions => "dist < 3.97")
+ assert_equal 5, locations
end
def test_find_within_with_custom
locations = CustomLocation.find_within(3.97, :origin => @loc_a)
assert_equal 5, locations.size
+ locations = CustomLocation.count_within(3.97, :origin => @loc_a)
+ assert_equal 5, locations
end
def test_find_within_with_coordinates_with_custom
locations = CustomLocation.find_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
assert_equal 5, locations.size
+ locations = CustomLocation.count_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
+ assert_equal 5, locations
end
def test_find_with_compound_condition_with_custom
locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
assert_equal 1, locations.size
+ locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
+ assert_equal 1, locations
end
def test_find_with_secure_compound_condition_with_custom
locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
assert_equal 1, locations.size
+ locations = CustomLocation.count(:origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
+ assert_equal 1, locations
end
def test_find_beyond_with_custom
locations = CustomLocation.find_beyond(3.95, :origin => @loc_a)
- assert_equal 1, locations.size
+ assert_equal 1, locations.size
+ locations = CustomLocation.count_beyond(3.95, :origin => @loc_a)
+ assert_equal 1, locations
end
def test_find_beyond_with_coordinates_with_custom
locations = CustomLocation.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
assert_equal 1, locations.size
+ locations = CustomLocation.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
+ assert_equal 1, locations
end
def test_find_nearest_with_custom
@@ -352,5 +416,7 @@ def test_find_farthest_with_coordinates_with_custom
def test_find_with_array_origin
locations = Location.find(:all, :origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
assert_equal 5, locations.size
+ locations = Location.count(:origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
+ assert_equal 5, locations
end
end
View
22 test/base_geocoder_test.rb
@@ -1,9 +1,9 @@
-$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'test/unit'
require 'net/http'
require 'rubygems'
require 'mocha'
-require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
+require File.join(File.dirname(__FILE__), '../../../../config/environment')
+
class MockSuccess < Net::HTTPSuccess #:nodoc: all
def initialize
@@ -17,6 +17,7 @@ def initialize
# Base class for testing geocoders.
class BaseGeocoderTest < Test::Unit::TestCase #:nodoc: all
+
# Defines common test fixtures.
def setup
@address = 'San Francisco, CA'
@@ -27,6 +28,23 @@ def setup
@success.success = true
end
+ def test_timeout_call_web_service
+ GeoKit::Geocoders::Geocoder.class_eval do
+ def self.do_get(url)
+ sleep(2)
+ end
+ end
+ url = "http://www.anything.com"
+ GeoKit::Geocoders::timeout = 1
+ assert_nil GeoKit::Geocoders::Geocoder.call_geocoder_service(url)
+ end
+
+ def test_successful_call_web_service
+ url = "http://www.anything.com"
+ GeoKit::Geocoders::Geocoder.expects(:do_get).with(url).returns("SUCCESS")
+ assert_equal "SUCCESS", GeoKit::Geocoders::Geocoder.call_geocoder_service(url)
+ end
+
def test_find_geocoder_methods
public_methods = GeoKit::Geocoders::Geocoder.public_methods
assert public_methods.include?("yahoo_geocoder")
View
13 test/ca_geocoder_test.rb
@@ -1,10 +1,10 @@
-require File.dirname(__FILE__) + '/base_geocoder_test.rb'
+require File.join(File.dirname(__FILE__), 'base_geocoder_test')
GeoKit::Geocoders::geocoder_ca = "SOMEKEYVALUE"
class CaGeocoderTest < BaseGeocoderTest #:nodoc: all
- SUCCESS=<<-EOF
+ CA_SUCCESS=<<-EOF
<?xml version="1.0" encoding="UTF-8" ?>
<geodata><latt>49.243086</latt><longt>-123.153684</longt></geodata>
EOF
@@ -16,14 +16,17 @@ def setup
def test_geocoder_with_geo_loc_with_account
response = MockSuccess.new
- response.expects(:body).returns(SUCCESS)
- Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml")).returns(response)
+ response.expects(:body).returns(CA_SUCCESS)
+ url = "http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml"
+ GeoKit::Geocoders::CaGeocoder.expects(:call_geocoder_service).with(url).returns(response)
verify(GeoKit::Geocoders::CaGeocoder.geocode(@ca_full_loc))
end
def test_service_unavailable
response = MockFailure.new
- Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml")).returns(response)
+ #Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml")).returns(response)
+ url = "http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml"
+ GeoKit::Geocoders::CaGeocoder.expects(:call_geocoder_service).with(url).returns(response)
assert !GeoKit::Geocoders::CaGeocoder.geocode(@ca_full_loc).success
end
View
2  test/geoloc_test.rb
@@ -1,5 +1,5 @@
require 'test/unit'
-require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
+require File.join(File.dirname(__FILE__), '../../../../config/environment')
class GeoLocTest < Test::Unit::TestCase #:nodoc: all
View
17 test/google_geocoder_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/base_geocoder_test.rb'
+require File.join(File.dirname(__FILE__), 'base_geocoder_test')
GeoKit::Geocoders::google = 'Google'
@@ -24,7 +24,8 @@ def setup
def test_google_full_address
response = MockSuccess.new
response.expects(:body).returns(GOOGLE_FULL)
- Net::HTTP.expects(:get_response).with(URI.parse("http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8")).returns(response)
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
res=GeoKit::Geocoders::GoogleGeocoder.geocode(@address)
assert_equal "CA", res.state
assert_equal "San Francisco", res.city
@@ -37,7 +38,8 @@ def test_google_full_address
def test_google_full_address_with_geo_loc
response = MockSuccess.new
response.expects(:body).returns(GOOGLE_FULL)
- Net::HTTP.expects(:get_response).with(URI.parse("http://maps.google.com/maps/geo?q=#{CGI.escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8")).returns(response)
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8"
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
res=GeoKit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
assert_equal "CA", res.state
assert_equal "San Francisco", res.city
@@ -50,7 +52,8 @@ def test_google_full_address_with_geo_loc
def test_google_city
response = MockSuccess.new
response.expects(:body).returns(GOOGLE_CITY)
- Net::HTTP.expects(:get_response).with(URI.parse("http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8")).returns(response)
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
res=GeoKit::Geocoders::GoogleGeocoder.geocode(@address)
assert_equal "CA", res.state
assert_equal "San Francisco", res.city
@@ -64,7 +67,8 @@ def test_google_city
def test_google_city_with_geo_loc
response = MockSuccess.new
response.expects(:body).returns(GOOGLE_CITY)
- Net::HTTP.expects(:get_response).with(URI.parse("http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8")).returns(response)
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
res=GeoKit::Geocoders::GoogleGeocoder.geocode(@google_city_loc)
assert_equal "CA", res.state
assert_equal "San Francisco", res.city
@@ -77,7 +81,8 @@ def test_google_city_with_geo_loc
def test_service_unavailable
response = MockFailure.new
- Net::HTTP.expects(:get_response).with(URI.parse("http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8")).returns(response)
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
assert !GeoKit::Geocoders::GoogleGeocoder.geocode(@google_city_loc).success
end
end
View
6 test/ip_geocode_lookup_test.rb
@@ -1,9 +1,9 @@
-$:.unshift(File.dirname(__FILE__) + '/../lib')
+require File.join(File.dirname(__FILE__), '../../../../config/environment')
+require 'action_controller/test_process'
require 'test/unit'
require 'rubygems'
require 'mocha'
-require File.join(File.dirname(__FILE__), '../../../../test/test_helper')
-require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
+
class LocationAwareController < ActionController::Base #:nodoc: all
geocode_ip_address
View
26 test/ipgeocoder_test.rb
@@ -1,22 +1,22 @@
-require File.dirname(__FILE__) + '/base_geocoder_test.rb'
+require File.join(File.dirname(__FILE__), 'base_geocoder_test')
class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
- FAILURE=<<-EOF
+ IP_FAILURE=<<-EOF
Country: (Private Address) (XX)
City: (Private Address)
Latitude:
Longitude:
EOF
- SUCCESS=<<-EOF
+ IP_SUCCESS=<<-EOF
Country: UNITED STATES (US)
City: Sugar Grove, IL
Latitude: 41.7696
Longitude: -88.4588
EOF
- UNICODED=<<-EOF
+ IP_UNICODED=<<-EOF
Country: SWEDEN (SE)
City: Borås
Latitude: 57.7167
@@ -30,8 +30,9 @@ def setup
def test_successful_lookup
success = MockSuccess.new
- success.expects(:body).returns(SUCCESS)
- Net::HTTP.expects(:get_response).with('api.hostip.info', '/get_html.php?ip=12.215.42.19&position=true').returns(success)
+ success.expects(:body).returns(IP_SUCCESS)
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
assert_not_nil location
assert_equal 41.7696, location.lat
@@ -45,8 +46,9 @@ def test_successful_lookup
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)
+ success.expects(:body).returns(IP_UNICODED)
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
assert_not_nil location
assert_equal 57.7167, location.lat
@@ -60,8 +62,9 @@ def test_unicoded_lookup
def test_failed_lookup
failure = MockSuccess.new
- failure.expects(:body).returns(FAILURE)
- Net::HTTP.expects(:get_response).with('api.hostip.info', '/get_html.php?ip=0.0.0.0&position=true').returns(failure)
+ failure.expects(:body).returns(IP_FAILURE)
+ url = 'http://api.hostip.info/get_html.php?ip=0.0.0.0&position=true'
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
location = GeoKit::Geocoders::IpGeocoder.geocode("0.0.0.0")
assert_not_nil location
assert !location.success
@@ -75,7 +78,8 @@ def test_invalid_ip
def test_service_unavailable
failure = MockFailure.new
- Net::HTTP.expects(:get_response).with('api.hostip.info', '/get_html.php?ip=0.0.0.0&position=true').returns(failure)
+ url = 'http://api.hostip.info/get_html.php?ip=0.0.0.0&position=true'
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
location = GeoKit::Geocoders::IpGeocoder.geocode("0.0.0.0")
assert_not_nil location
assert !location.success
View
4 test/latlng_test.rb
@@ -1,5 +1,6 @@
+$LOAD_PATH.unshift File.join('..', 'lib')
+require 'geo_kit/mappable'
require 'test/unit'
-require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
class LatLngTest < Test::Unit::TestCase #:nodoc: all
@@ -66,5 +67,4 @@ def test_manually_mixed_in
assert_in_delta 3.97, @point.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
assert_in_delta 6.39, @point.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
end
-
end
View
2  test/multi_geocoder_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/base_geocoder_test.rb'
+require File.join(File.dirname(__FILE__), 'base_geocoder_test')
GeoKit::Geocoders::provider_order=[:google,:yahoo,:us]
View
18 test/test_helper.rb
@@ -0,0 +1,18 @@
+require 'test/unit'
+
+plugin_test_dir = File.dirname(__FILE__)
+
+# Load the Rails environment
+require File.join(plugin_test_dir, '../../../../config/environment')
+require 'active_record/fixtures'
+databases = YAML::load(IO.read(plugin_test_dir + '/database.yml'))
+ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
+
+# A specific database can be used by setting the DB environment variable
+ActiveRecord::Base.establish_connection(databases[ENV['DB'] || 'mysql'])
+
+# Load the test schema into the database
+load(File.join(plugin_test_dir, 'schema.rb'))
+
+# Load fixtures from the plugin
+Test::Unit::TestCase.fixture_path = File.join(plugin_test_dir, 'fixtures/')
View
11 test/us_geocoder_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/base_geocoder_test.rb'
+require File.join(File.dirname(__FILE__), 'base_geocoder_test')
GeoKit::Geocoders::geocoder_us = nil
@@ -15,20 +15,23 @@ def setup
def test_geocoder_us
response = MockSuccess.new
response.expects(:body).returns(GEOCODER_US_FULL)
- Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.us/service/csv/geocode?address=#{CGI.escape(@address)}")).returns(response)
+ url = "http://geocoder.us/service/csv/geocode?address=#{CGI.escape(@address)}"
+ GeoKit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
verify(GeoKit::Geocoders::UsGeocoder.geocode(@address))
end
def test_geocoder_with_geo_loc
response = MockSuccess.new
response.expects(:body).returns(GEOCODER_US_FULL)
- Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.us/service/csv/geocode?address=#{CGI.escape(@address)}")).returns(response)
+ url = "http://geocoder.us/service/csv/geocode?address=#{CGI.escape(@address)}"
+ GeoKit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
verify(GeoKit::Geocoders::UsGeocoder.geocode(@us_full_loc))
end
def test_service_unavailable
response = MockFailure.new
- Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.us/service/csv/geocode?address=#{CGI.escape(@address)}")).returns(response)
+ url = "http://geocoder.us/service/csv/geocode?address=#{CGI.escape(@address)}"
+ GeoKit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
assert !GeoKit::Geocoders::UsGeocoder.geocode(@us_full_loc).success
end
View
17 test/yahoo_geocoder_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/base_geocoder_test.rb'
+require File.join(File.dirname(__FILE__), 'base_geocoder_test')
GeoKit::Geocoders::yahoo = 'Yahoo'
@@ -27,34 +27,39 @@ def setup
def test_yahoo_full_address
response = MockSuccess.new
response.expects(:body).returns(YAHOO_FULL)
- Net::HTTP.expects(:get_response).with(URI.parse("http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}")).returns(response)
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}"
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
do_full_address_assertions(GeoKit::Geocoders::YahooGeocoder.geocode(@address))
end
def test_yahoo_full_address_with_geo_loc
response = MockSuccess.new
response.expects(:body).returns(YAHOO_FULL)
- Net::HTTP.expects(:get_response).with(URI.parse("http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@full_address)}")).returns(response)
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@full_address)}"
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
do_full_address_assertions(GeoKit::Geocoders::YahooGeocoder.geocode(@yahoo_full_loc))
end
def test_yahoo_city
response = MockSuccess.new
response.expects(:body).returns(YAHOO_CITY)
- Net::HTTP.expects(:get_response).with(URI.parse("http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}")).returns(response)
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}"
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
do_city_assertions(GeoKit::Geocoders::YahooGeocoder.geocode(@address))
end
def test_yahoo_city_with_geo_loc
response = MockSuccess.new
response.expects(:body).returns(YAHOO_CITY)
- Net::HTTP.expects(:get_response).with(URI.parse("http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}")).returns(response)
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}"
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
do_city_assertions(GeoKit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc))
end
def test_service_unavailable
response = MockFailure.new
- Net::HTTP.expects(:get_response).with(URI.parse("http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}")).returns(response)
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}"
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
assert !GeoKit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc).success
end
Please sign in to comment.
Something went wrong with that request. Please try again.