geocoder 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of geocoder might be problematic. Click here for more details.

@@ -20,9 +20,10 @@ module Geocoder
20
20
  # Calculate the distance from the object to an arbitrary point.
21
21
  # See Geocoder::Calculations.distance_between for ways of specifying
22
22
  # the point. Also takes a symbol specifying the units
23
- # (:mi or :km; default is :mi).
23
+ # (:mi or :km; can be specified in Geocoder configuration).
24
24
  #
25
- def distance_to(point, units = :mi)
25
+ def distance_to(point, units = nil)
26
+ units ||= self.class.geocoder_options[:units]
26
27
  return nil unless geocoded?
27
28
  Geocoder::Calculations.distance_between(
28
29
  to_coordinates, point, :units => units)
@@ -36,6 +37,7 @@ module Geocoder
36
37
  # ways of specifying the point.
37
38
  #
38
39
  def bearing_to(point, options = {})
40
+ options[:method] ||= self.class.geocoder_options[:method]
39
41
  return nil unless geocoded?
40
42
  Geocoder::Calculations.bearing_between(
41
43
  to_coordinates, point, options)
@@ -47,6 +49,7 @@ module Geocoder
47
49
  # ways of specifying the point.
48
50
  #
49
51
  def bearing_from(point, options = {})
52
+ options[:method] ||= self.class.geocoder_options[:method]
50
53
  return nil unless geocoded?
51
54
  Geocoder::Calculations.bearing_between(
52
55
  point, to_coordinates, options)
@@ -78,7 +81,6 @@ module Geocoder
78
81
  fail
79
82
  end
80
83
 
81
-
82
84
  private # --------------------------------------------------------------
83
85
 
84
86
  ##
@@ -114,3 +116,4 @@ module Geocoder
114
116
  end
115
117
  end
116
118
  end
119
+
@@ -20,6 +20,7 @@ module Geocoder::Store
20
20
 
21
21
  radius = args.size > 0 ? args.shift : 20
22
22
  options = args.size > 0 ? args.shift : {}
23
+ options[:units] ||= geocoder_options[:units]
23
24
 
24
25
  # Use BSON::OrderedHash if Ruby's hashes are unordered.
25
26
  # Conditions must be in order required by indexes (see mongo gem).
@@ -30,7 +31,7 @@ module Geocoder::Store
30
31
  conds[field] = empty.clone
31
32
  conds[field]["$nearSphere"] = coords.reverse
32
33
  conds[field]["$maxDistance"] = \
33
- Geocoder::Calculations.distance_to_radians(radius, options[:units] || :mi)
34
+ Geocoder::Calculations.distance_to_radians(radius, options[:units])
34
35
 
35
36
  if obj = options[:exclude]
36
37
  conds[:_id.ne] = obj.id
@@ -81,3 +82,4 @@ module Geocoder::Store
81
82
  end
82
83
  end
83
84
  end
85
+
@@ -1,3 +1,3 @@
1
1
  module Geocoder
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
@@ -3,10 +3,23 @@ namespace :geocode do
3
3
  task :all => :environment do
4
4
  class_name = ENV['CLASS'] || ENV['class']
5
5
  raise "Please specify a CLASS (model)" unless class_name
6
- klass = Object.const_get(class_name)
6
+ klass = class_from_string(class_name)
7
7
 
8
8
  klass.not_geocoded.each do |obj|
9
9
  obj.geocode; obj.save
10
10
  end
11
11
  end
12
12
  end
13
+
14
+ ##
15
+ # Get a class object from the string given in the shell environment.
16
+ # Similar to ActiveSupport's +constantize+ method.
17
+ #
18
+ def class_from_string(class_name)
19
+ parts = class_name.split("::")
20
+ constant = Object
21
+ parts.each do |part|
22
+ constant = constant.const_get(part)
23
+ end
24
+ constant
25
+ end
@@ -2,7 +2,12 @@
2
2
  require 'test_helper'
3
3
 
4
4
  class CalculationsTest < Test::Unit::TestCase
5
-
5
+ def setup
6
+ Geocoder.configure do |config|
7
+ config.units = :mi
8
+ config.distances = :linear
9
+ end
10
+ end
6
11
 
7
12
  # --- degree distance ---
8
13
 
@@ -144,4 +149,34 @@ class CalculationsTest < Test::Unit::TestCase
144
149
  l = Landmark.new(*landmark_params(:msg))
145
150
  assert_equal l.bearing_from([50,-86.1]), l.bearing_to([50,-86.1]) - 180
146
151
  end
152
+
153
+ def test_extract_coordinates
154
+ result = Geocoder::Calculations.extract_coordinates([ nil, nil ])
155
+ assert_equal [ Geocoder::Calculations::NAN ] * 2, result
156
+
157
+ result = Geocoder::Calculations.extract_coordinates([ 1.0 / 3, 2.0 / 3 ])
158
+ assert_in_delta 1.0 / 3, result.first, 1E-5
159
+ assert_in_delta 2.0 / 3, result.last, 1E-5
160
+
161
+ result = Geocoder::Calculations.extract_coordinates(nil)
162
+ assert_equal [ Geocoder::Calculations::NAN ] * 2, result
163
+
164
+ result = Geocoder::Calculations.extract_coordinates('')
165
+ assert_equal [ Geocoder::Calculations::NAN ] * 2, result
166
+
167
+ result = Geocoder::Calculations.extract_coordinates([ 'nix' ])
168
+ assert_equal [ Geocoder::Calculations::NAN ] * 2, result
169
+
170
+ o = Object.new
171
+ result = Geocoder::Calculations.extract_coordinates(o)
172
+ assert_equal [ Geocoder::Calculations::NAN ] * 2, result
173
+
174
+ def o.to_coordinates
175
+ [ 1.0 / 3, 2.0 / 3 ]
176
+ end
177
+ result = Geocoder::Calculations.extract_coordinates(o)
178
+ assert_in_delta 1.0 / 3, result.first, 1E-5
179
+ assert_in_delta 2.0 / 3, result.last, 1E-5
180
+ end
147
181
  end
182
+
@@ -2,6 +2,9 @@
2
2
  require 'test_helper'
3
3
 
4
4
  class ConfigurationTest < Test::Unit::TestCase
5
+ def setup
6
+ Geocoder::Configuration.set_defaults
7
+ end
5
8
 
6
9
  def test_exception_raised_on_bad_lookup_config
7
10
  Geocoder::Configuration.lookup = :stoopid
@@ -10,4 +13,91 @@ class ConfigurationTest < Test::Unit::TestCase
10
13
  end
11
14
  end
12
15
 
16
+ # --- class method configuration ---
17
+ def test_configurated_by_class_method
18
+ Geocoder::Configuration.units = :mi
19
+ distance = Geocoder::Calculations.distance_between([0,0], [0,1]).round
20
+ assert_not_equal 111, distance
21
+ assert_equal 69, distance
22
+
23
+ Geocoder::Configuration.units = :km
24
+ distance = Geocoder::Calculations.distance_between([0,0], [0,1]).round
25
+ assert_equal 111, distance
26
+ assert_not_equal 69, distance
27
+
28
+ Geocoder::Configuration.distances = :spherical
29
+ angle = Geocoder::Calculations.bearing_between([50,-85], [40.750354, -73.993371]).round
30
+ assert_equal 136, angle
31
+ assert_not_equal 130, angle
32
+
33
+ Geocoder::Configuration.distances = :linear
34
+ angle = Geocoder::Calculations.bearing_between([50,-85], [40.750354, -73.993371]).round
35
+ assert_not_equal 136, angle
36
+ assert_equal 130, angle
37
+ end
38
+
39
+ # --- Geocoder#configure distances configuration ---
40
+ def test_geocoder_configuration
41
+ # DSL
42
+ Geocoder.configure do |config|
43
+ config.units = :mi
44
+ config.distances = :linear
45
+ end
46
+
47
+ assert_equal Geocoder::Configuration.units, :mi
48
+ distance = Geocoder::Calculations.distance_between([0,0], [0,1]).round
49
+ assert_not_equal 111, distance
50
+ assert_equal 69, distance
51
+
52
+ assert_equal Geocoder::Configuration.distances, :linear
53
+ angle = Geocoder::Calculations.bearing_between([50,-85], [40.750354, -73.993371]).round
54
+ assert_not_equal 136, angle
55
+ assert_equal 130, angle
56
+
57
+ # Direct
58
+ Geocoder.configure.units = :km
59
+ Geocoder.configure.distances = :spherical
60
+
61
+ assert_equal Geocoder::Configuration.units, :km
62
+ distance = Geocoder::Calculations.distance_between([0,0], [0,1]).round
63
+ assert_equal 111, distance
64
+ assert_not_equal 69, distance
65
+
66
+ assert_equal Geocoder::Configuration.distances, :spherical
67
+ angle = Geocoder::Calculations.bearing_between([50,-85], [40.750354, -73.993371]).round
68
+ assert_equal 136, angle
69
+ assert_not_equal 130, angle
70
+ end
71
+
72
+ # Geocoder per-model configuration
73
+ def test_model_configuration
74
+ Landmark.reverse_geocoded_by :latitude, :longitude, :method => :spherical, :units => :km
75
+ assert_equal :km, Landmark.geocoder_options[:units]
76
+ assert_equal :spherical, Landmark.geocoder_options[:method]
77
+
78
+ v = Landmark.new(*landmark_params(:msg))
79
+ v.latitude = 0
80
+ v.longitude = 0
81
+ assert_equal 111, v.distance_to([0,1]).round
82
+ v.latitude = 40.750354
83
+ v.longitude = -73.993371
84
+ assert_equal 136, v.bearing_from([50,-85]).round
85
+ end
86
+
87
+ def test_configuration_chain
88
+ v = Landmark.new(*landmark_params(:msg))
89
+ v.latitude = 0
90
+ v.longitude = 0
91
+
92
+ # method option > global configuration
93
+ Geocoder.configure.units = :km
94
+ assert_equal 69, v.distance_to([0,1], :mi).round
95
+
96
+ # per-model configuration > global configuration
97
+ Landmark.reverse_geocoded_by :latitude, :longitude, :method => :spherical, :units => :mi
98
+ assert_equal 69, v.distance_to([0,1]).round
99
+
100
+ # method option > per-model configuration
101
+ assert_equal 111, v.distance_to([0,1], :km).round
102
+ end
13
103
  end
@@ -29,3 +29,4 @@ class CustomBlockTest < Test::Unit::TestCase
29
29
  assert_nil e.address
30
30
  end
31
31
  end
32
+
@@ -6,8 +6,10 @@ class InputHandlingTest < Test::Unit::TestCase
6
6
  def test_ip_address_detection
7
7
  assert Geocoder.send(:ip_address?, "232.65.123.94")
8
8
  assert Geocoder.send(:ip_address?, "666.65.123.94") # technically invalid
9
+ assert Geocoder.send(:ip_address?, "::ffff:12.34.56.78")
9
10
  assert !Geocoder.send(:ip_address?, "232.65.123.94.43")
10
11
  assert !Geocoder.send(:ip_address?, "232.65.123")
12
+ assert !Geocoder.send(:ip_address?, "::ffff:123.456.789")
11
13
  end
12
14
 
13
15
  def test_blank_query_detection
data/test/lookup_test.rb CHANGED
@@ -27,4 +27,11 @@ class LookupTest < Test::Unit::TestCase
27
27
  g = Geocoder::Lookup::Yahoo.new
28
28
  assert_match "appid=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
29
29
  end
30
+
31
+ def test_geocoder_ca_showpostal
32
+ Geocoder::Configuration.api_key = "MY_KEY"
33
+ g = Geocoder::Lookup::GeocoderCa.new
34
+ assert_match "showpostal=1", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
35
+ end
36
+
30
37
  end
data/test/mongoid_test.rb CHANGED
@@ -1,12 +1,7 @@
1
1
  # encoding: utf-8
2
- require 'test_helper'
3
-
4
- begin
5
- require 'mongoid'
6
2
  require 'mongoid_test_helper'
7
3
 
8
4
  class MongoidTest < Test::Unit::TestCase
9
-
10
5
  def test_geocoded_check
11
6
  p = Place.new(*venue_params(:msg))
12
7
  p.location = [40.750354, -73.993371]
@@ -24,8 +19,20 @@ class MongoidTest < Test::Unit::TestCase
24
19
  p = Place.near(location)
25
20
  assert_equal p.selector[:location]['$nearSphere'], location.reverse
26
21
  end
27
- end
28
22
 
29
- rescue LoadError => crash
30
- warn 'Mongoid not installed, not tested.'
23
+ def test_model_configuration
24
+ p = Place.new(*venue_params(:msg))
25
+ p.location = [0, 0]
26
+
27
+ Place.geocoded_by :address, :coordinates => :location, :units => :km
28
+ assert_equal 111, p.distance_to([0,1]).round
29
+
30
+ Place.geocoded_by :address, :coordinates => :location, :units => :mi
31
+ assert_equal 69, p.distance_to([0,1]).round
32
+ end
33
+
34
+ def test_index_is_skipped_if_skip_option_flag
35
+ result = PlaceWithoutIndex.index_options.keys.flatten[0] == :coordinates
36
+ assert !result
37
+ end
31
38
  end
@@ -1,5 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
+ require 'test_helper'
4
+ require 'mongoid'
5
+ require 'geocoder/models/mongoid'
3
6
 
4
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -26,3 +29,11 @@ class Place
26
29
  write_attribute :address, address
27
30
  end
28
31
  end
32
+
33
+ class PlaceWithoutIndex
34
+ include Mongoid::Document
35
+ include Geocoder::Model::Mongoid
36
+
37
+ field :location, :type => Array
38
+ geocoded_by :location, :skip_index => true
39
+ end
data/test/test_helper.rb CHANGED
@@ -150,7 +150,7 @@ module Geocoder
150
150
  end
151
151
  end
152
152
 
153
- class Nominatim < Base
153
+ class Nominatim < Base
154
154
  private #-----------------------------------------------------------------
155
155
  def fetch_raw_data(query, reverse = false)
156
156
  raise TimeoutError if query == "timeout"
@@ -272,3 +272,4 @@ class Test::Unit::TestCase
272
272
  all_lookups - [:freegeoip]
273
273
  end
274
274
  end
275
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: geocoder
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.1
5
+ version: 1.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alex Reisner
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-02-16 00:00:00 -05:00
14
- default_executable:
13
+ date: 2012-05-24 00:00:00 Z
15
14
  dependencies: []
16
15
 
17
16
  description: Provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), distance queries for ActiveRecord and Mongoid, result caching, and more. Designed for Rails but works with Sinatra and other Rack frameworks too.
@@ -30,6 +29,8 @@ files:
30
29
  - README.rdoc
31
30
  - Rakefile
32
31
  - bin/geocode
32
+ - lib/generators/geocoder/config/config_generator.rb
33
+ - lib/generators/geocoder/config/templates/initializer.rb
33
34
  - lib/geocoder.rb
34
35
  - lib/geocoder/cache.rb
35
36
  - lib/geocoder/calculations.rb
@@ -105,7 +106,6 @@ files:
105
106
  - test/result_test.rb
106
107
  - test/services_test.rb
107
108
  - test/test_helper.rb
108
- has_rdoc: true
109
109
  homepage: http://www.rubygeocoder.com
110
110
  licenses: []
111
111
 
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  requirements: []
130
130
 
131
131
  rubyforge_project:
132
- rubygems_version: 1.6.1
132
+ rubygems_version: 1.8.24
133
133
  signing_key:
134
134
  specification_version: 3
135
135
  summary: Complete geocoding solution for Ruby.