Discal 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/discal.rb +91 -0
  3. metadata +72 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f467358ceb649564d7be7faa02087948f436324e23931b176ad22206d7eeee3c
4
+ data.tar.gz: db88b87451dcf9a2746514731ab5c3368ab2e0e4cf861c4332c4c5c317761a31
5
+ SHA512:
6
+ metadata.gz: 998ab19dc9aca3f7b8cff9b81b5ccbe9b4471748e7d14e8a50fbd6211fbcfce299c8ed6efc9cbcf612f683844b5191cfa5b360211ef1fe3056c2d2661178768b
7
+ data.tar.gz: e0406731f74816b5bd4144cb2d3dbba9e42ff90d3c11c1807a0a5be590aa4cb67e26141d2ba3d3698b32641aca3e49ed755f9bee01aa0ec7194fe022a63e8b46
@@ -0,0 +1,91 @@
1
+ require 'haversine'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ class Discal
7
+ attr_accessor :coords
8
+
9
+ def initialize(coords)
10
+ @coords = coords
11
+ @cache = {}
12
+
13
+ raise ArgumentError, 'Argument is not an Array' unless coords.is_a? Array
14
+ raise RuntimeError, 'Min 2 coordinates' unless coords.count >= 2
15
+ raise RuntimeError, 'Max 100 coordinates' unless coords.count <= 100
16
+ end
17
+
18
+ def calculate_distance_with_math
19
+ loop_coordinates { |coord1, coord2|
20
+ distance_math_function(coord1, coord2)
21
+ }
22
+ end
23
+
24
+ def calculate_distance_with_api
25
+ loop_coordinates { |coord1, coord2|
26
+ api_call(coord1, coord2)
27
+ }
28
+ end
29
+
30
+ protected
31
+
32
+ def loop_coordinates
33
+ results = []
34
+
35
+ coords.each_with_index do |coord1, i|
36
+ for j in (i+1)...(coords.count)
37
+ coord2 = coords[j]
38
+
39
+ cache = get_cache(coord1, coord2)
40
+ if cache
41
+ results << cache
42
+ next
43
+ end
44
+
45
+ result = yield(coord1, coord2)
46
+ results << result
47
+ set_cache(coord1, coord2, result)
48
+ end
49
+ end
50
+
51
+ results
52
+ end
53
+
54
+ private
55
+
56
+ def distance_math_function(coord1, coord2)
57
+ Haversine.distance(coord1[0], coord1[1], coord2[0], coord2[1]).to_kilometers
58
+ end
59
+
60
+ def api_call(coord1, coord2)
61
+ uri = URI.parse(url(coord1, coord2))
62
+ response = Net::HTTP.get_response(uri)
63
+ json = JSON.parse(response.body)#
64
+ json['resourceSets'][0]['resources'][0]['results'][0]['travelDistance']
65
+ rescue SocketError
66
+ raise SocketError, "Error while connecting to the API, please check the coordinates array"
67
+ end
68
+
69
+ def url(coord1, coord2)
70
+ origins = "#{coord1[0]},#{coord1[1]}"
71
+ destinations = "#{coord2[0]},#{coord2[1]}"
72
+ base = 'https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix'
73
+ "#{base}?origins=#{origins}&destinations=#{destinations}&travelMode=driving&distanceUnit=km&key=#{api_key}"
74
+ end
75
+
76
+ def api_key
77
+ File.read('key.txt')
78
+ rescue Errno::ENOENT
79
+ raise Errno::ENOENT, "There is no file key.txt with key for Bing Maps"
80
+ end
81
+
82
+ def get_cache(coord1, coord2)
83
+ r = @cache.dig("#{coord1[0]},#{coord1[1]}", "#{coord2[0]},#{coord2[1]}")
84
+ r ||= @cache.dig("#{coord2[0]},#{coord2[1]}", "#{coord1[0]},#{coord1[1]}")
85
+ end
86
+
87
+ def set_cache(coord1, coord2, value)
88
+ @cache["#{coord1[0]},#{coord1[1]}"] ||= {}
89
+ @cache["#{coord1[0]},#{coord1[1]}"]["#{coord2[0]},#{coord2[1]}"] = value
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Discal
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - 'Pablo Marti [new email: pablo.marti.dev@gmail.com]'
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: haversine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.2
41
+ description: Gets the distance between two points through two functions (math and
42
+ 3rd API)
43
+ email: pablo.marti89@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/discal.rb
49
+ homepage: http://rubygems.org/gems/discal
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.0.1
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Gets the distance between two points
72
+ test_files: []