drifter 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -81,16 +81,6 @@ and sent as a query string parameter e.g. Yahoo's service returns a timezone if
81
81
  >> paris.data["timezone"]
82
82
  => "Europe/paris"
83
83
 
84
- Finally, Drifter::Location objects have a distance_to() method
85
-
86
- >> london.distance_to(bh)
87
- => 5438.60013996461
88
-
89
- Distances are returned in miles by default. You can change this per request or change the default
90
-
91
- >> Drifter.default_units = :km
92
- >> london.distance_to(bh, :units => :miles)
93
-
94
84
  Drifter.geocode() always returns an array if the request was processed successfully by the
95
85
  geocoding service. An empty array indicates that the service returned no results.
96
86
 
@@ -1,12 +1,10 @@
1
1
  require 'rubygems'
2
- require 'drifter/distance/haversine'
3
2
  require 'drifter/geocoders'
4
3
  require 'drifter/location'
5
4
 
6
5
  module Drifter
7
6
 
8
7
  @@default_geocoder = :google
9
- @@default_units = :miles
10
8
  @@last_error = nil
11
9
 
12
10
 
@@ -24,19 +22,6 @@ module Drifter
24
22
  end
25
23
 
26
24
 
27
- # Returns the default units for distance calculations
28
- def self.default_units
29
- @@default_units
30
- end
31
-
32
-
33
- # Sets the default units for distance calculations.
34
- # Supported values are :miles and :kms
35
- def self.default_units=(value)
36
- @@default_units=value
37
- end
38
-
39
-
40
25
  # Helper method to extract the lat and lng from an array or from any object
41
26
  # that responds to lat() and lng(). Returns nil if neither of those apply
42
27
  def self.extract_latlng(loc)
@@ -1,4 +1,3 @@
1
- require 'drifter/location/locatable'
2
1
  module Drifter
3
2
  # Drifter.geocode() returns an array of Drifter::Location objects
4
3
  # Depending on the geocoder used, Location objects are populated
@@ -11,7 +10,6 @@ module Drifter
11
10
  # Additional data returned by the geocoder can be accessed via the
12
11
  # data() method
13
12
  class Location
14
- include Drifter::Location::Locatable
15
13
 
16
14
  attr_accessor :raw_data
17
15
  attr_accessor :raw_data_format
@@ -1,3 +1,3 @@
1
1
  module Drifter
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drifter
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 23
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
- - 1
8
8
  - 2
9
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Ahmed Adam
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-01-24 00:00:00 +00:00
18
+ date: 2011-03-08 00:00:00 +00:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - ~>
27
28
  - !ruby/object:Gem::Version
29
+ hash: 11
28
30
  segments:
29
31
  - 1
30
32
  - 4
@@ -40,6 +42,7 @@ dependencies:
40
42
  requirements:
41
43
  - - ">="
42
44
  - !ruby/object:Gem::Version
45
+ hash: 3
43
46
  segments:
44
47
  - 0
45
48
  version: "0"
@@ -60,17 +63,14 @@ files:
60
63
  - Rakefile
61
64
  - drifter.gemspec
62
65
  - lib/drifter.rb
63
- - lib/drifter/distance/haversine.rb
64
66
  - lib/drifter/geocoders.rb
65
67
  - lib/drifter/geocoders/base.rb
66
68
  - lib/drifter/geocoders/google.rb
67
69
  - lib/drifter/geocoders/hostip.rb
68
70
  - lib/drifter/geocoders/yahoo.rb
69
71
  - lib/drifter/location.rb
70
- - lib/drifter/location/locatable.rb
71
72
  - lib/drifter/version.rb
72
73
  - test/google_geocoder_test.rb
73
- - test/locatable_test.rb
74
74
  - test/location_test.rb
75
75
  - test/responses/google_error
76
76
  - test/responses/google_many_results
@@ -96,6 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
+ hash: 3
99
100
  segments:
100
101
  - 0
101
102
  version: "0"
@@ -104,13 +105,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  requirements:
105
106
  - - ">="
106
107
  - !ruby/object:Gem::Version
108
+ hash: 3
107
109
  segments:
108
110
  - 0
109
111
  version: "0"
110
112
  requirements: []
111
113
 
112
114
  rubyforge_project: drifter
113
- rubygems_version: 1.3.7
115
+ rubygems_version: 1.4.2
114
116
  signing_key:
115
117
  specification_version: 3
116
118
  summary: Simple geocoding library for ruby
@@ -1,69 +0,0 @@
1
- module Drifter
2
- module Distance
3
- class Haversine
4
-
5
- EarthRadiusInMiles = 3956
6
- EarthRadiusInKms = 6371
7
- RAD_PER_DEG = 0.017453293 # PI/180
8
-
9
-
10
- # this method is from Landon Cox's haversine.rb (GNU Affero GPL v3):
11
- # http://www.esawdust.com/blog/gps/files/HaversineFormulaInRuby.html:
12
- # http://www.esawdust.com (Landon Cox)
13
- # http://www.esawdust.com/blog/businesscard/businesscard.html
14
- def self.between(point1, point2, options={})
15
- lat1, lon1 = Drifter.extract_latlng!(point1)
16
- lat2, lon2 = Drifter.extract_latlng!(point2)
17
-
18
- dlon = lon2 - lon1
19
- dlat = lat2 - lat1
20
-
21
- dlon_rad = dlon * RAD_PER_DEG
22
- dlat_rad = dlat * RAD_PER_DEG
23
-
24
- lat1_rad = lat1 * RAD_PER_DEG
25
- lon1_rad = lon1 * RAD_PER_DEG
26
-
27
- lat2_rad = lat2 * RAD_PER_DEG
28
- lon2_rad = lon2 * RAD_PER_DEG
29
-
30
- a = (Math.sin(dlat_rad/2))**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * (Math.sin(dlon_rad/2))**2
31
- c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))
32
-
33
- units = options.delete(:units) || Drifter.default_units
34
- return EarthRadiusInKms * c if units == :km
35
- return EarthRadiusInMiles * c
36
- end
37
-
38
-
39
- # haversine sql based on http://code.google.com/apis/maps/articles/phpsqlsearch.html
40
- # you will need to add 'select' and 'AS distance' if required
41
- def self.to_postgresql(options)
42
- origin = options[:origin]
43
- lat, lng = Drifter.extract_latlng!(origin)
44
- lat_column = options[:lat_column] || :lat
45
- lng_column = options[:lng_column] || :lng
46
- units = options[:units] || Drifter.default_units
47
- multiplier = EarthRadiusInMiles
48
- multiplier = EarthRadiusInKms if units == :km
49
-
50
- postgres = <<-EOS
51
- #{multiplier} * ACOS(
52
- COS( RADIANS(#{lat}) ) *
53
- COS( RADIANS( #{lat_column} ) ) *
54
- COS( RADIANS( #{lng_column} ) -
55
- RADIANS(#{lng}) ) +
56
- SIN( RADIANS(#{lat}) ) *
57
- SIN( RADIANS( #{lat_column} ) )
58
- )
59
- EOS
60
- end
61
-
62
- # postgresql code seems to work fine fo sql
63
- def self.to_mysql(options)
64
- to_postgresql(options)
65
- end
66
-
67
- end
68
- end
69
- end
@@ -1,15 +0,0 @@
1
- # classes including this module must repond to lat(), lat=(), lng() and lng=()
2
- module Drifter
3
- class Location
4
- module Locatable
5
-
6
-
7
- # nodoc
8
- def distance_to(loc, options={})
9
- Drifter::Distance::Haversine.between(self, loc, options)
10
- end
11
-
12
-
13
- end
14
- end
15
- end
@@ -1,101 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class Widget
4
- include Drifter::Location::Locatable
5
- attr_accessor :lat, :lng
6
- end
7
-
8
-
9
- class LocatableTest < Test::Unit::TestCase
10
-
11
- def setup
12
- @widget = Widget.new
13
- end
14
-
15
-
16
- # nodoc
17
- def floats_equal?(f1, f2)
18
- f1.to_s == f2.to_s
19
- end
20
-
21
-
22
- # nodoc
23
- def dont_test_location
24
- @widget.lat = 10
25
- @widget.lng = 20
26
- loc = @widget.location
27
- assert_equal 10, loc.lat
28
- assert_equal 20, loc.lng
29
-
30
- @widget.lat = nil
31
- @widget.lng = nil
32
- assert_equal nil, @widget.location.lat
33
- assert_equal nil, @widget.location.lng
34
- end
35
-
36
-
37
- # nodoc
38
- def dont_test_location_setter
39
- loc = Drifter::Location.new
40
- loc.lat = 10
41
- loc.lng = 20
42
- @widget.location = loc
43
- assert_equal 10, @widget.lat
44
- assert_equal 20, @widget.lng
45
-
46
- @widget.location = nil
47
- assert_nil @widget.lat
48
- assert_nil @widget.lng
49
-
50
- @widget.location = [3,4]
51
- assert_equal 3, @widget.lat
52
- assert_equal 4, @widget.lng
53
- end
54
-
55
-
56
- def test_distance_to
57
- assert_raise(ArgumentError) { @widget.distance_to nil }
58
- assert_raise(ArgumentError) { @widget.distance_to "" }
59
- assert_raise(ArgumentError) { @widget.distance_to 1 }
60
-
61
- # london
62
- @widget.lat = 51.5001524
63
- @widget.lng = -0.1262362
64
-
65
- # manchester
66
- lat = 53.4807125
67
- lng = -2.2343765
68
-
69
- manchester = Widget.new
70
- manchester.lat = lat
71
- manchester.lng = lng
72
-
73
- distance_in_miles = 162.941138493485
74
- distance_in_km = 262.411019550554
75
-
76
- # pass an array, default units
77
- distance = @widget.distance_to [lat, lng]
78
- assert floats_equal?(distance_in_miles, distance)
79
-
80
- # location as array, units in miles
81
- distance = @widget.distance_to [lat, lng], :units => :miles
82
- assert floats_equal?(distance_in_miles, distance)
83
-
84
- # location as array, units in km
85
- distance = @widget.distance_to [lat, lng], :units => :km
86
- assert floats_equal?(distance_in_km, distance)
87
-
88
- # location as Locatable object, default units
89
- distance = @widget.distance_to manchester
90
- assert floats_equal?(distance_in_miles, distance)
91
-
92
- # location as Locatable object, units in miles
93
- distance = @widget.distance_to manchester, :units => :miles
94
- assert floats_equal?(distance_in_miles, distance)
95
-
96
- # location as Locatable object, units in km
97
- distance = @widget.distance_to manchester, :units => :km
98
- assert floats_equal?(distance_in_km, distance)
99
- end
100
-
101
- end