distance_calculator 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1ba1144e72774f8f420ca0039b99051e2e292bbe9293fb5a49ebd395a17c3316
4
+ data.tar.gz: 128b7e53e2ce99a42dd36fc3df881957020f9135fa98d3c99081055c3758669a
5
+ SHA512:
6
+ metadata.gz: 25dbb8a08fb29f596c891e6e09ff3dcd5b5248d1a53d67c6d07199c67c3ab360eeec1b5c04930b93035a3b31984cbfdd49fefbda14a205928d8c19d0389b4c8b
7
+ data.tar.gz: 832515e1e7c2be9450456e2de94a002ec2a221835b85c0d38ab04c1310fddff37cac929931ae739b3ed8748f014cdbef154ecbc26fd0664c0c1bbd7a7b26480d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in distance_calculator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 YaroslavTupitskyi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ ## Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ ```ruby
6
+ gem 'distance_calculator'
7
+ ```
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install distance_calculator
16
+
17
+ ## License
18
+
19
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'distance_calculator'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'distance_calculator'
8
+ spec.version = DistanceCalculator::VERSION
9
+ spec.authors = ['YaroslavTupitskyi']
10
+ spec.email = ['yarik@active-bridge.com']
11
+
12
+ spec.summary = 'DistanceCalculator'
13
+ spec.description = 'DistanceCalculator'
14
+ spec.homepage = 'https://github.com/YaroslavTupitskyi/DistanceCalculator'
15
+ spec.license = 'MIT'
16
+ spec.platform = Gem::Platform::RUBY
17
+
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 2.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
@@ -0,0 +1,13 @@
1
+ require 'distance_calculator_with_math'
2
+
3
+ class DistanceCalculator
4
+ VERSION = '0.0.1'.freeze
5
+
6
+ def initialize(coords)
7
+ @coords = coords
8
+ end
9
+
10
+ def calculate_distance_with_math
11
+ DistanceCalculatorWithMath.call(@coords)
12
+ end
13
+ end
@@ -0,0 +1,58 @@
1
+ module DistanceCalculatorWithMath
2
+ extend self
3
+
4
+ RAD_PER_DEG = (Math::PI / 180).freeze
5
+ R = 6371
6
+ LATITUDE_REGEXP = /^(\+|-)?(90(\.0{1,})?|([0-9]|[1-8][0-9])(\.[0-9]{1,})?)$/.freeze
7
+ LONGITUDE_REGEXP = /^(\+|-)?(?:180(\.0{1,})?|([0-9]|[1-9][0-9]|1[0-7][0-9])(\.[0-9]{1,})?)$/.freeze
8
+
9
+ def call(coords)
10
+ coords.each_cons(2).map { |coord1, coord2| [distance(coord1, coord2).round(2)] }
11
+ end
12
+
13
+ private
14
+
15
+ def distance(coord1, coord2)
16
+ check_locations([coord1, coord2])
17
+ coord1 = convert_to_radians(coord1)
18
+ coord2 = convert_to_radians(coord2)
19
+ calculate_haversin(coord1, coord2)
20
+ end
21
+
22
+ def check_locations(locations = [])
23
+ locations.each { |l| validate_location(*l) }
24
+ end
25
+
26
+ def validate_location(lat, lng)
27
+ return if lat =~ LATITUDE_REGEXP && lng =~ LONGITUDE_REGEXP
28
+
29
+ raise ArgumentError, error_message(lat, lng)
30
+ end
31
+
32
+ def deg_to_rad(deg)
33
+ deg * RAD_PER_DEG
34
+ end
35
+
36
+ def convert_to_radians(arr)
37
+ arr.map { |e| deg_to_rad(e.to_f) }
38
+ end
39
+
40
+ def calculate_haversin(coord1, coord2)
41
+ lat1, lng1 = coord1
42
+ lat2, lng2 = coord2
43
+
44
+ h = haversin(lat2 - lat1) +
45
+ Math.cos(lat1) * Math.cos(lat2) *
46
+ haversin(lng2 - lng1)
47
+
48
+ 2 * R * Math.asin(Math.sqrt(h))
49
+ end
50
+
51
+ def haversin(rad)
52
+ Math.sin(rad / 2)**2
53
+ end
54
+
55
+ def error_message(lat, lng)
56
+ "Latitude or Longitude is incorrect. Please, check parameters: [#{lat}, #{lng}]"
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: distance_calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - YaroslavTupitskyi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: DistanceCalculator
56
+ email:
57
+ - yarik@active-bridge.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - distance_calculator.gemspec
70
+ - lib/distance_calculator.rb
71
+ - lib/distance_calculator_with_math.rb
72
+ homepage: https://github.com/YaroslavTupitskyi/DistanceCalculator
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.7
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: DistanceCalculator
96
+ test_files: []