distance_fox 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cff5a5f458b21ec1d22020843ebf129ff58065d4d69038d1509672f2470181ba
4
- data.tar.gz: 3c86f4fef0c40ce55b9200f6107592b74cc902e836848a2f884634d73491a321
3
+ metadata.gz: d50033e856f2be1c8fa447b618455880a34758d8ccb08eac180830ccba744a09
4
+ data.tar.gz: 603f31d6c3fae909c219888c7b30dc5a0e0065d9d613979e064e2a56612442e4
5
5
  SHA512:
6
- metadata.gz: 013073bb1246908b333425a40b3129b2db85dd9409a285dafa5b483b0b6241dde5a5c7a22f33e6f49ec20b60aa3a2dec3c7512640b60f9330e348138ba9f24bc
7
- data.tar.gz: 9bf7bc6f6b5d51152aa3a143471b322c175a866428d3a62c61ae4f27dad64a216f3252d945e32ac8b7babf3fd5c95e44a12bb12faa4f4a5dfaf69ef5dfb5d53b
6
+ metadata.gz: 7c40c456df7000ebecfe3137beb6f64a835cf8fcded4cbb0d1e3c000f4774fcf392709deed9f041b50b6e1bd96177ea05c8e51c51576561f98b50df331e89cbe
7
+ data.tar.gz: b943e5ab358ab9489e483ef292b9be46b909686f280e9cef48ad3cb7c61afeb1f59d41fcca29ec02562a0b49d0ab1e8b7e91c5b9cf8927da63977bf36fbe0e68
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- distance_fox (0.1.2)
4
+ distance_fox (0.1.3)
5
5
  httparty (~> 0.13.7)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -24,7 +24,7 @@ Set up DistanceFox with your Google Distance Matrix API Key:
24
24
 
25
25
  ```ruby
26
26
  DistanceFox.configure do |config|
27
- config.google_api_key = 'YOUR_API_KEY'
27
+ config.api_key = 'YOUR_API_KEY'
28
28
  end
29
29
  ```
30
30
 
@@ -1,117 +1,119 @@
1
1
  require 'httparty'
2
2
 
3
3
  module DistanceFox
4
- class Calculations
5
- EARTH_RADIUS = 6371.0
4
+ EARTH_RADIUS = 6371.0
6
5
 
7
- # Origin and destination can be:
8
- #
9
- # * an array of coordinates ([lat,lon])
10
- # * an array of coordinates (['lat','lon'])
11
- # * a geocodable address (string)
12
- def self.linear_distance(origin, destination)
13
- lat_origin, lon_origin = extract_coordinates origin
14
- lat_destination, lon_destination = extract_coordinates destination
6
+ # Origin and destination can be:
7
+ #
8
+ # * an array of coordinates ([lat,lon])
9
+ # * an array of coordinates (['lat','lon'])
10
+ # * a geocodable address (string)
11
+ def self.linear_distance(origin, destination)
12
+ lat_origin, lon_origin = extract_coordinates origin
13
+ lat_destination, lon_destination = extract_coordinates destination
15
14
 
16
- delta_lat = (lat_destination - lat_origin) * Math::PI / 180
17
- delta_lon = (lon_destination - lon_origin) * Math::PI / 180
15
+ delta_lat = (lat_destination - lat_origin) * Math::PI / 180
16
+ delta_lon = (lon_destination - lon_origin) * Math::PI / 180
18
17
 
19
- a = Math.sin(delta_lat / 2) *
20
- Math.sin(delta_lat / 2) +
21
- Math.cos(lat_origin * Math::PI / 180) *
22
- Math.cos(lat_destination * Math::PI / 180) *
23
- Math.sin(delta_lon / 2) * Math.sin(delta_lon / 2)
18
+ a = Math.sin(delta_lat / 2) *
19
+ Math.sin(delta_lat / 2) +
20
+ Math.cos(lat_origin * Math::PI / 180) *
21
+ Math.cos(lat_destination * Math::PI / 180) *
22
+ Math.sin(delta_lon / 2) * Math.sin(delta_lon / 2)
24
23
 
25
- c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
24
+ c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
26
25
 
27
- d = EARTH_RADIUS * c
28
- d.round(3)
29
- end
26
+ d = EARTH_RADIUS * c
27
+ d.round(3)
28
+ end
30
29
 
31
- ##
32
- # Linear Distance to nearest location from multiple locations
33
- # Origin can be:
34
- # * an array of float coordinates ([lat,lon])
35
- # * an array of string coordinates (['lat','lon'])
36
- #
37
- # Destinations can be:
38
- # * a hash of arrays ({['city' => 'City', 'coordinates' => ['lat','lon']]})
39
- def self.nearest_linear_distance(origin, destinations)
40
- origins_with_distance = []
41
- origin_coordinates = extract_coordinates origin
42
- destinations.each do |destination|
43
- distance = linear_distance(origin_coordinates, destination['coordinates'])
44
- origins_with_distance.push(
45
- 'destination' => destination['city'],
46
- 'distance' => distance,
47
- 'coordinates' => {
48
- 'origin' => origin_coordinates,
49
- 'destination' => destination['coordinates']
50
- }
51
- )
52
- end
53
- origins_with_distance.min_by { |key| key['distance'] }
30
+ ##
31
+ # Linear Distance to nearest location from multiple locations
32
+ # Origin can be:
33
+ # * an array of float coordinates ([lat,lon])
34
+ # * an array of string coordinates (['lat','lon'])
35
+ #
36
+ # Destinations can be:
37
+ # * a hash of arrays ({['city' => 'City', 'coordinates' => ['lat','lon']]})
38
+ def self.nearest_linear_distance(origin, destinations)
39
+ origins_with_distance = []
40
+ origin_coordinates = extract_coordinates origin
41
+ destinations.each do |destination|
42
+ distance = linear_distance(origin_coordinates, destination['coordinates'])
43
+ origins_with_distance.push(
44
+ 'destination' => destination['city'],
45
+ 'distance' => distance,
46
+ 'coordinates' => {
47
+ 'origin' => origin_coordinates,
48
+ 'destination' => destination['coordinates']
49
+ }
50
+ )
54
51
  end
52
+ origins_with_distance.min_by { |key| key['distance'] }
53
+ end
55
54
 
56
- ##
57
- # Origin and destination can be:
58
- #
59
- # * an array of coordinates ([lat,lon])
60
- # * an array of coordinates (['lat','lon'])
61
- # * a geocodable address (string)
62
- def self.driving_distance(origin, destination)
63
- origin = origin.to_s
64
- destination = destination.to_s
65
- google_distance_query = "https://maps.googleapis.com/maps/api/distancematrix/json?mode=driving&origins=#{origin}&destinations=#{destination}&key=#{DistanceFox.configuration.api_key}"
66
- google_distance_object = HTTParty.get(URI.escape(google_distance_query))
67
- google_distance_object['rows'].first['elements'].first['distance']['value'].to_f / 1000
68
- end
55
+ ##
56
+ # Origin and destination can be:
57
+ #
58
+ # * an array of coordinates ([lat,lon])
59
+ # * an array of coordinates (['lat','lon'])
60
+ # * a geocodable address (string)
61
+ def self.driving_distance(origin, destination)
62
+ origin = extract_coordinates(origin).join(',')
63
+ destination = extract_coordinates(destination).join(',')
64
+ google_distance_query = "https://maps.googleapis.com/maps/api/distancematrix/json?mode=driving&origins=#{origin}&destinations=#{destination}&key=#{DistanceFox.configuration.api_key}"
65
+ google_distance_response = HTTParty.get(URI.escape(google_distance_query))
66
+ google_distance_response['rows'][0]['elements'][0]['distance']['value'].to_f / 1000
67
+ end
69
68
 
70
- ##
71
- # Driving Distance to nearest location from multiple locations
72
- #
73
- # Origin can be:
74
- # * an array of coordinates ([lat,lon])
75
- # * an array of coordinates (['lat','lon'])
76
- # * a geocodable address (string)
77
- #
78
- # Destinations can be:
79
- # * a hash of arrays ({['city' => 'City', 'coordinates' => ['lat','lon']]})
80
- def self.nearest_driving_distance(origin, destinations)
81
- origin = origin.join(',')
82
- destination = nearest_linear_distance(origin, destinations)['coordinates']['destination'].join(',')
83
- driving_distance(origin, destination)
84
- end
69
+ ##
70
+ # Driving Distance to nearest location from multiple locations
71
+ #
72
+ # Origin can be:
73
+ # * an array of coordinates ([lat,lon])
74
+ # * an array of coordinates (['lat','lon'])
75
+ # * a geocodable address (string)
76
+ #
77
+ # Destinations can be:
78
+ # * a hash of arrays ({['city' => 'City', 'coordinates' => ['lat','lon']]})
79
+ def self.nearest_driving_distance(origin, destinations)
80
+ origin = origin.join(',')
81
+ destination = nearest_linear_distance(origin, destinations)['coordinates']['destination'].join(',')
82
+ driving_distance(origin, destination)
83
+ end
85
84
 
86
- # Address can be:
87
- # a geocodable address (string)
88
- def self.geocode(address)
89
- google_distance_query = "https://maps.googleapis.com/maps/api/geocode/json?address=#{address}&key=#{DistanceFox.configuration.api_key}"
90
- google_distance_object = HTTParty.get(URI.escape(google_distance_query))
91
- coordinates = google_distance_object['results'][0]['geometry']['location']
92
- [coordinates['lat'], coordinates['lng']]
85
+ # Address can be:
86
+ # a geocodable address (string)
87
+ def self.geocode(address)
88
+ google_distance_response = HTTParty.get(URI.escape(google_distance_query))
89
+ case google_distance_response["results"]
90
+ when []
91
+ raise "Status: #{google_distance_response['status']}"
92
+ else
93
+ coordinates = google_distance_response['results'][0]['geometry']['location']
93
94
  end
95
+ [coordinates['lat'], coordinates['lng']]
96
+ end
94
97
 
95
- private
98
+ private
96
99
 
97
- ##
98
- # Takes an object which is a [lat,lon] array or a geocodable string.
99
- # Note that if a string is passed this may be a slow-
100
- # running method and may return nil.
101
- def self.extract_coordinates(point)
102
- case point
103
- when Array
104
- if point.size == 2
105
- lat, lon = point
106
- if !lat.nil? && lat.respond_to?(:to_f) &&
107
- !lon.nil? && lon.respond_to?(:to_f)
108
- return [lat.to_f, lon.to_f]
109
- end
100
+ ##
101
+ # Takes an object which is a [lat,lon] array or a geocodable string.
102
+ # Note that if a string is passed this may be a slow-
103
+ # running method and may return nil.
104
+ def self.extract_coordinates(point)
105
+ case point
106
+ when Array
107
+ if point.size == 2
108
+ lat, lon = point
109
+ if !lat.nil? && lat.respond_to?(:to_f) &&
110
+ !lon.nil? && lon.respond_to?(:to_f)
111
+ return [lat.to_f, lon.to_f]
110
112
  end
111
- when String
112
- (point = geocode(point)) && (return point)
113
113
  end
114
- [NAN, NAN]
114
+ when String
115
+ (point = geocode(point)) && (return point)
115
116
  end
117
+ raise "#{point} is not geocodable"
116
118
  end
117
119
  end
@@ -1,3 +1,3 @@
1
1
  module DistanceFox
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distance_fox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - danielwollen