distance_fox 0.1.3 → 0.1.4

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: d50033e856f2be1c8fa447b618455880a34758d8ccb08eac180830ccba744a09
4
- data.tar.gz: 603f31d6c3fae909c219888c7b30dc5a0e0065d9d613979e064e2a56612442e4
3
+ metadata.gz: 2afe2b7e3b59d3047f7b2ce181169f2a4fb3bcc882572ee66856468909f08ee3
4
+ data.tar.gz: 7e1c0b16d8c0d3fa05afc4ff02b2850ea40c231b9cd69c9e571a2094b7affd2a
5
5
  SHA512:
6
- metadata.gz: 7c40c456df7000ebecfe3137beb6f64a835cf8fcded4cbb0d1e3c000f4774fcf392709deed9f041b50b6e1bd96177ea05c8e51c51576561f98b50df331e89cbe
7
- data.tar.gz: b943e5ab358ab9489e483ef292b9be46b909686f280e9cef48ad3cb7c61afeb1f59d41fcca29ec02562a0b49d0ab1e8b7e91c5b9cf8927da63977bf36fbe0e68
6
+ metadata.gz: 7b56bd1db29afdd6e1f681b66c350c530e278ff750652a75d7f75dafbcd8bffba6b343d8109161fecbef15ff794a1951c0f99f5dcfab4e5ac9e35e287d7960f6
7
+ data.tar.gz: 6ce540bc79a13fa41a33f72926652925391f33f22de984a8800dda0d5278a98374fa20df1281052ed1a2a226a512413dfa561f6fd778077855fd699d60c96cce
data/.travis.yml CHANGED
@@ -4,4 +4,4 @@ language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
6
  - 2.5.0
7
- before_install: gem install bundler -v 1.17.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- distance_fox (0.1.3)
4
+ distance_fox (0.1.4)
5
5
  httparty (~> 0.13.7)
6
6
 
7
7
  GEM
@@ -1,119 +1,121 @@
1
1
  require 'httparty'
2
2
 
3
3
  module DistanceFox
4
- EARTH_RADIUS = 6371.0
4
+ class Calculations
5
+ EARTH_RADIUS = 6371.0
5
6
 
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
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
14
15
 
15
- delta_lat = (lat_destination - lat_origin) * Math::PI / 180
16
- delta_lon = (lon_destination - lon_origin) * Math::PI / 180
16
+ delta_lat = (lat_destination - lat_origin) * Math::PI / 180
17
+ delta_lon = (lon_destination - lon_origin) * Math::PI / 180
17
18
 
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)
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)
23
24
 
24
- c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
25
+ c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
25
26
 
26
- d = EARTH_RADIUS * c
27
- d.round(3)
28
- end
27
+ d = EARTH_RADIUS * c
28
+ d.round(3)
29
+ end
29
30
 
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
- )
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'] }
51
54
  end
52
- origins_with_distance.min_by { |key| key['distance'] }
53
- end
54
55
 
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
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 = extract_coordinates(origin).join(',')
64
+ destination = extract_coordinates(destination).join(',')
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_response = HTTParty.get(URI.escape(google_distance_query))
67
+ google_distance_response['rows'][0]['elements'][0]['distance']['value'].to_f / 1000
68
+ end
68
69
 
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
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
84
85
 
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']
86
+ # Address can be:
87
+ # a geocodable address (string)
88
+ def self.geocode(address)
89
+ google_distance_response = HTTParty.get(URI.escape(google_distance_query))
90
+ case google_distance_response["results"]
91
+ when []
92
+ raise "Status: #{google_distance_response['status']}"
93
+ else
94
+ coordinates = google_distance_response['results'][0]['geometry']['location']
95
+ end
96
+ [coordinates['lat'], coordinates['lng']]
94
97
  end
95
- [coordinates['lat'], coordinates['lng']]
96
- end
97
98
 
98
- private
99
+ private
99
100
 
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]
101
+ ##
102
+ # Takes an object which is a [lat,lon] array or a geocodable string.
103
+ # Note that if a string is passed this may be a slow-
104
+ # running method and may return nil.
105
+ def self.extract_coordinates(point)
106
+ case point
107
+ when Array
108
+ if point.size == 2
109
+ lat, lon = point
110
+ if !lat.nil? && lat.respond_to?(:to_f) &&
111
+ !lon.nil? && lon.respond_to?(:to_f)
112
+ return [lat.to_f, lon.to_f]
113
+ end
112
114
  end
115
+ when String
116
+ (point = geocode(point)) && (return point)
113
117
  end
114
- when String
115
- (point = geocode(point)) && (return point)
118
+ raise "#{point} is not geocodable"
116
119
  end
117
- raise "#{point} is not geocodable"
118
120
  end
119
121
  end
@@ -1,3 +1,3 @@
1
1
  module DistanceFox
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - danielwollen