bing_route 0.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 +7 -0
- data/lib/bing_route.rb +80 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dcdcaafec41f5cc9a2784aecdb5be0a1b1e83805
|
4
|
+
data.tar.gz: 83947b2f9ad6e43cd4457b96ad6f71aca2e6491a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c13d756e89d91b5e942d95a44fa598aa87832938dda4e5943666b0169a9b983d73dc686552deadd8ae49c650b7f66d7cb32e15a8f00d08c80c63253b23a2757
|
7
|
+
data.tar.gz: 7513417c51120aa69ca74a06b2c70b711a8f649b8de1a1ca9a8a620bf9f63a4f8c97d0d861804f96ea774c54e6ee02e4dddc9df3b9ad2ba8698b762e3aeab7d0
|
data/lib/bing_route.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'cgi'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
BASE_URL = "http://dev.virtualearth.net/REST/V1/Routes?"
|
7
|
+
BING_MAPS_KEY = ENV['BING_MAPS_KEY']
|
8
|
+
|
9
|
+
class BingRoute
|
10
|
+
|
11
|
+
@@default_options = {
|
12
|
+
:optmz => :timeWithTraffic,
|
13
|
+
:du => :mi,
|
14
|
+
:ra => :routeSummariesOnly,
|
15
|
+
:key => BING_MAPS_KEY,
|
16
|
+
}
|
17
|
+
|
18
|
+
def initialize(origin, destination, opts=@@default_options)
|
19
|
+
waypoints = "wp.0=#{origin.to_query}&wp.1=#{destination.to_query}"
|
20
|
+
url = "#{BASE_URL}#{waypoints}&#{opts.to_query_string}"
|
21
|
+
|
22
|
+
begin
|
23
|
+
doc = open(url)
|
24
|
+
rescue OpenURI::HTTPError => e
|
25
|
+
puts "Whoops we got #{e.message}"
|
26
|
+
@status = e.message
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
|
30
|
+
directions = JSON.parse(doc.read)
|
31
|
+
@status = directions["statusDescription"]
|
32
|
+
@distance = directions["resourceSets"][0]["resources"][0]["travelDistance"]
|
33
|
+
@time_in_seconds = directions["resourceSets"][0]["resources"][0]["travelDuration"]
|
34
|
+
@traffic_time_in_seconds = directions["resourceSets"][0]["resources"][0]["travelDurationTraffic"]
|
35
|
+
end
|
36
|
+
|
37
|
+
def status
|
38
|
+
@status
|
39
|
+
end
|
40
|
+
|
41
|
+
def distance
|
42
|
+
return 0 if @status != "OK"
|
43
|
+
@distance.round
|
44
|
+
end
|
45
|
+
|
46
|
+
def drive_time
|
47
|
+
return 0 if @status != "OK"
|
48
|
+
convert_to_minutes(@time_in_seconds)
|
49
|
+
end
|
50
|
+
|
51
|
+
def drive_time_in_traffic
|
52
|
+
return 0 if @status != "OK"
|
53
|
+
convert_to_minutes(@traffic_time_in_seconds)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def convert_to_minutes(seconds)
|
59
|
+
(seconds.to_f / 60).round
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
class Hash
|
65
|
+
|
66
|
+
def to_query_string
|
67
|
+
collect do |k, v|
|
68
|
+
"#{k}=#{CGI::escape(v.to_s)}"
|
69
|
+
end * '&'
|
70
|
+
end unless method_defined? :to_query_string
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
class String
|
75
|
+
|
76
|
+
def to_query
|
77
|
+
"#{CGI::escape(self)}"
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bing_route
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Silvia
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: N/A
|
14
|
+
email: james.e.silvia@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/bing_route.rb
|
20
|
+
homepage: http://rubygems.org/gems/bing_route
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: bing_route!
|
44
|
+
test_files: []
|