geo_rb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '028b35e41da078d2419f0b7a9400fbd98c84c5d06c021ad9c0d8eb5b642b321a'
4
- data.tar.gz: 5cefbfc862f9b3063e879e7427a41c51913330ddc73294d9299940fbe5f6ac24
3
+ metadata.gz: d7ef6fbc41f5f8f01523388fd1475698f01ed4877fcf9d6b3fb98a14b3658145
4
+ data.tar.gz: 557ed9d56a9ca5cee2804685bcb656feb21a076456fc7807eb4fb39f1737e5b8
5
5
  SHA512:
6
- metadata.gz: 739a39e6937c42157b810f34fd32bf93c56c16db5a2e6e6359e7ed68b3469113353f007b21f70c06265132f7c79594ae1628bf51c60563980357c32460507e59
7
- data.tar.gz: 6064574fdfac9cd8c2638a467e13a181d7c6fd03222d96048f4264c2a39fca01d2bb3d812c0fb9b0a63c5e231976364e561be8a04a008dc6cc30af8ceb6a2388
6
+ metadata.gz: 3e8ec814b6c58aea5c0249ec0fea828b1932a12519328dd21b799d917d105715df0208ee6a874af8d23c0ad7433bbba399c87d21b1d90205853c8e78657fd94f
7
+ data.tar.gz: 8f15cd64fd2a5bf8b2ca17f6c1bd824faf3548d173fb519ae94dcaede679c819231bfc68a884e18a728cd36169c4226d82d15d3b9c46ef4d3b2184ebba857e59
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # geo_rb
2
2
 
3
- [![Gem Version](http://img.shields.io/gem/v/geo_rb.svg)][gem]
4
- [![Code Climate](https://codeclimate.com/github/kucho/geo_rb.svg)][codeclimate]
5
-
6
3
  geo_rb makes it easy for Ruby developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders.
7
4
 
8
5
  It is heavily inspired by [geopy](https://github.com/geopy/geopy).
@@ -13,10 +10,15 @@ It is heavily inspired by [geopy](https://github.com/geopy/geopy).
13
10
 
14
11
  ## Usage
15
12
 
16
- ### Address lookup
13
+ ### Distance between two points
17
14
  ```ruby
18
15
  plaza = GeoRb::Location.lookup("Plaza San Martin, Lima, Peru")
19
16
  palacio = GeoRb::Location.lookup("Palacio de Justicia, Lima, Peru")
20
17
  plaza.distance_to(palacio).km # => 0.6649563608870949
21
18
  palacio.distance_to(plaza).km # => 0.6649563608870949
22
19
  ```
20
+
21
+ ### Distance between multiple points
22
+ ```ruby
23
+
24
+ ```
data/geo_rb.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.version = GeoRb::VERSION
10
10
  spec.platform = Gem::Platform::RUBY
11
11
  spec.summary = "Client for geolocation services"
12
- spec.description = "geo_rb makes it easy for Ruby developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources"
12
+ spec.description = "geo_rb makes it easy for Ruby developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders."
13
13
  spec.authors = ["Victor Rodriguez"]
14
14
  spec.email = "victor.rodriguez.guriz@gmail.com"
15
15
  spec.license = "MIT"
@@ -22,7 +22,8 @@ Gem::Specification.new do |spec|
22
22
  "changelog_uri" => "https://github.com/kucho/geo_rb/blob/master/CHANGELOG.md"
23
23
  }
24
24
 
25
- spec.add_runtime_dependency("geographiclib", "~> 0.0.2")
25
+ spec.add_runtime_dependency("concurrent-ruby", "~> 1.1.8")
26
+ spec.add_runtime_dependency("geodesic_wgs84", "~> 1.46.1")
26
27
 
27
28
  spec.add_development_dependency("bundler", "~> 2.2")
28
29
  spec.add_development_dependency("standardrb", "~> 1.0")
@@ -1,22 +1,39 @@
1
- require "geographiclib"
1
+ require "bigdecimal/util"
2
+ require "concurrent"
3
+ require "geodesic_wgs84"
4
+
2
5
 
3
6
  module GeoRb
4
7
  class Distance
5
8
  EARTH_RADIUS = 6_371.0088
6
9
 
7
- attr_reader :kilometers
10
+ attr_reader :kilometers, :meters
8
11
  alias_method :km, :kilometers
12
+ alias_method :m, :meters
13
+
14
+ def self.between(*addresses, adapter: GeoRb::GeoCoders::Nominatim)
15
+ return new if addresses.size == 1
16
+
17
+ requests = addresses.map do |address|
18
+ Concurrent::Promises.future(address) { |a| adapter.new.geocode(a) }
19
+ end
20
+
21
+ locations = Concurrent::Promises.zip(*requests).value!
22
+ new(*locations)
23
+ end
9
24
 
10
25
  def initialize(*locations)
11
- @kilometers = case locations.size
12
- when 1
13
- Float(locations.first) if locations.size == 1
26
+ GeoRb.logger.debug locations
27
+ @meters = case locations.size
28
+ when 0..1
29
+ 0
14
30
  else
15
31
  locations.each_cons(2).reduce(0) do |distance, pair|
16
32
  a, b = sanitize_location(pair.first), sanitize_location(pair.last)
17
33
  distance + measure(a, b)
18
34
  end
19
35
  end
36
+ @kilometers = @meters.to_d / 1_000
20
37
  end
21
38
 
22
39
  def ensure_same_altitude(a, b)
@@ -25,8 +42,8 @@ module GeoRb
25
42
 
26
43
  def measure(a, b)
27
44
  ensure_same_altitude(a, b)
28
- r = GeographicLib::Geodesic::WGS84.inverse(a.latitude, a.longitude, b.latitude, b.longitude)
29
- r[:s12] / 1_000
45
+ r = Wgs84.new.distance(a.latitude, a.longitude, b.latitude, b.longitude).first
46
+ r.to_d
30
47
  end
31
48
 
32
49
  private
@@ -1,3 +1,3 @@
1
1
  module GeoRb
2
- VERSION = Gem::Version.new("0.0.1")
2
+ VERSION = Gem::Version.new("0.0.2")
3
3
  end
@@ -28,7 +28,31 @@ describe GeoRb::Distance do
28
28
  context "when trip between poles" do
29
29
  let(:locations) { [north_pole, south_pole] }
30
30
 
31
- it { should be_within(12).of(earth_circumference / 2) }
31
+ it { should be_within(13).of(earth_circumference / 2) }
32
+ end
33
+ end
34
+
35
+ describe ".between" do
36
+ subject { described_class.between(*locations).km }
37
+
38
+ context "with one valid address" do
39
+ let(:locations) { ["Jockey Plaza, Lima"] }
40
+
41
+ it { is_expected.to eq 0 }
42
+ end
43
+
44
+ context "with two valid addresses" do
45
+ let(:locations) { ["Plaza San Martin, Lima", "Palacio de Justicia, Lima"] }
46
+
47
+ it { is_expected.to eq 0.6650 }
48
+ end
49
+
50
+ context "with mixed addresses" do
51
+ let(:locations) { ["rhu9223u8r2f", "Palacio de Justicia, Lima"] }
52
+
53
+ it "fails" do
54
+ expect { subject }.to raise_error(GeoRb::LocationError)
55
+ end
32
56
  end
33
57
  end
34
58
  end
@@ -0,0 +1,24 @@
1
+ describe GeoRb::Location do
2
+ describe ".lookup" do
3
+ context "with valid location" do
4
+ let(:plaza) { described_class.lookup("Plaza San Martin, Lima") }
5
+
6
+ it "has the expected points" do
7
+ expect(plaza.latitude).to eq(-12.05165965)
8
+ expect(plaza.longitude).to eq(-77.03460482707533)
9
+ end
10
+ end
11
+ end
12
+
13
+ describe "#distance_to" do
14
+ context "from valid location" do
15
+ let(:plaza) { described_class.lookup("Plaza San Martin, Lima, Peru") }
16
+ context "to valid destination" do
17
+ let(:palacio) { described_class.lookup("Palacio de Justicia, Lima, Peru") }
18
+ subject { plaza.distance_to(palacio).km }
19
+
20
+ it { is_expected.to eq 0.6650 }
21
+ end
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Rodriguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-15 00:00:00.000000000 Z
11
+ date: 2021-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: geographiclib
14
+ name: concurrent-ruby
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.2
19
+ version: 1.1.8
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.2
26
+ version: 1.1.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: geodesic_wgs84
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.46.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.46.1
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -67,8 +81,7 @@ dependencies:
67
81
  - !ruby/object:Gem::Version
68
82
  version: '3.10'
69
83
  description: geo_rb makes it easy for Ruby developers to locate the coordinates of
70
- addresses, cities, countries, and landmarks across the globe using third-party geocoders
71
- and other data sources
84
+ addresses, cities, countries, and landmarks across the globe using third-party geocoders.
72
85
  email: victor.rodriguez.guriz@gmail.com
73
86
  executables: []
74
87
  extensions: []
@@ -90,6 +103,7 @@ files:
90
103
  - lib/geo_rb/point.rb
91
104
  - lib/geo_rb/version.rb
92
105
  - spec/geo_rb/distance_spec.rb
106
+ - spec/geo_rb/geo_coders/location_spec.rb
93
107
  - spec/geo_rb/geo_coders/nominatim_spec.rb
94
108
  - spec/spec_helper.rb
95
109
  homepage: https://github.com/kucho/geo_rb
@@ -113,11 +127,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
127
  - !ruby/object:Gem::Version
114
128
  version: '0'
115
129
  requirements: []
116
- rubygems_version: 3.2.3
130
+ rubygems_version: 3.2.15
117
131
  signing_key:
118
132
  specification_version: 4
119
133
  summary: Client for geolocation services
120
134
  test_files:
121
135
  - spec/geo_rb/distance_spec.rb
136
+ - spec/geo_rb/geo_coders/location_spec.rb
122
137
  - spec/geo_rb/geo_coders/nominatim_spec.rb
123
138
  - spec/spec_helper.rb