periplus 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -0
- data/VERSION +1 -1
- data/lib/periplus/bing_response.rb +25 -0
- data/lib/periplus/location.rb +19 -0
- data/lib/periplus/request.rb +136 -0
- data/lib/periplus/route.rb +20 -0
- data/lib/periplus.rb +22 -106
- data/periplus.gemspec +12 -2
- data/spec/bing_response.spec +36 -0
- data/spec/location_spec.rb +28 -0
- data/spec/periplus_spec.rb +29 -0
- data/spec/request_spec.rb +29 -0
- data/spec/route_spec.rb +0 -32
- metadata +31 -13
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
+
activesupport (3.1.0.rc4)
|
5
|
+
multi_json (~> 1.0)
|
4
6
|
crack (0.1.8)
|
5
7
|
diff-lcs (1.1.2)
|
6
8
|
git (1.2.5)
|
@@ -10,6 +12,7 @@ GEM
|
|
10
12
|
bundler (~> 1.0)
|
11
13
|
git (>= 1.2.5)
|
12
14
|
rake
|
15
|
+
multi_json (1.0.3)
|
13
16
|
rake (0.9.2)
|
14
17
|
rcov (0.9.9)
|
15
18
|
rspec (2.4.0)
|
@@ -25,6 +28,7 @@ PLATFORMS
|
|
25
28
|
ruby
|
26
29
|
|
27
30
|
DEPENDENCIES
|
31
|
+
activesupport
|
28
32
|
bundler (~> 1.0.0)
|
29
33
|
httparty
|
30
34
|
jeweler (~> 1.6.2)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Periplus
|
2
|
+
class BingResponse
|
3
|
+
def initialize(httparty_response)
|
4
|
+
@response = httparty_response
|
5
|
+
|
6
|
+
if @response.response.kind_of? Net::HTTPClientError
|
7
|
+
http_code = @response.response.code
|
8
|
+
http_message = @response.response.message
|
9
|
+
raise "An error has occurred communicating with the Bing Maps service. HTTP Status: #{http_code} (#{http_message})"
|
10
|
+
end
|
11
|
+
|
12
|
+
parse
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse
|
16
|
+
resource_sets = @response.parsed_response["resourceSets"]
|
17
|
+
raise "Not found." if resource_sets == nil or resource_sets.length == 0
|
18
|
+
|
19
|
+
resources = resource_sets.first["resources"]
|
20
|
+
raise "Not found." if resources == nil or resources.length == 0
|
21
|
+
|
22
|
+
@primary_resource = resources.first
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Periplus
|
2
|
+
class Location < BingResponse
|
3
|
+
attr_accessor :latitude
|
4
|
+
attr_accessor :longitude
|
5
|
+
attr_accessor :address
|
6
|
+
attr_accessor :confidence
|
7
|
+
attr_accessor :entity_type
|
8
|
+
|
9
|
+
def parse
|
10
|
+
super()
|
11
|
+
|
12
|
+
point = @primary_resource["point"]
|
13
|
+
@latitude, @longitude = point["coordinates"]
|
14
|
+
@address= @primary_resource["address"]
|
15
|
+
@confidence = @primary_resource["confidence"].downcase.to_sym
|
16
|
+
@entity_type = @primary_resource["entityType"].downcase.to_sym
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/route")
|
2
|
+
require "uri"
|
3
|
+
|
4
|
+
module Periplus
|
5
|
+
class Request
|
6
|
+
def initialize(api_key)
|
7
|
+
@api_key = api_key
|
8
|
+
end
|
9
|
+
|
10
|
+
BING_URL = "http://dev.virtualearth.net/REST/v1/"
|
11
|
+
ROUTE_PATH = "Routes"
|
12
|
+
LOCATION_PATH = "Locations"
|
13
|
+
ROUTE_IMAGE_PATH = "Imagery/Map/Road/Routes/Driving"
|
14
|
+
QUERY_IMAGE_PATH = "Imagery/Map/Road/"
|
15
|
+
|
16
|
+
def route_details_url(waypoints, options = {})
|
17
|
+
options = default_options(options).merge(hashify_waypoints(waypoints))
|
18
|
+
.merge(:o => "json")
|
19
|
+
|
20
|
+
"#{BING_URL}#{ROUTE_PATH}?#{options.to_params}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def route_map_url(waypoints, options = {})
|
24
|
+
options = options.merge(hashify_waypoints(waypoints))
|
25
|
+
.merge(:key => @api_key)
|
26
|
+
"#{BING_URL}#{ROUTE_IMAGE_PATH}?#{options.to_params}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def address_map_url(address, options = {})
|
30
|
+
options = default_options(options)
|
31
|
+
"#{BING_URL}#{QUERY_IMAGE_PATH}#{URI.escape(format_waypoint(address))}?#{options.to_params}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def location_details_url(address, options = {})
|
35
|
+
options = default_options(options).merge(:o => "json")
|
36
|
+
.merge(structure_address(address))
|
37
|
+
"#{BING_URL}#{LOCATION_PATH}?#{options.to_params}"
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def default_options(given_options)
|
42
|
+
given_options.merge(:key => @api_key)
|
43
|
+
end
|
44
|
+
|
45
|
+
# turns a list of waypoints into a bing-api-friendly "wp.1", "wp.2", etc...
|
46
|
+
def hashify_waypoints(waypoints)
|
47
|
+
counter = 1
|
48
|
+
waypoints.inject({}) do |hash, waypoint|
|
49
|
+
hash["wp.#{counter}"] = format_waypoint(waypoint)
|
50
|
+
counter = counter + 1
|
51
|
+
hash
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def has_key_or_attribute?(object, key_or_attribute)
|
56
|
+
object.respond_to? key_or_attribute or
|
57
|
+
(object.respond_to? :has_key? and object.has_key? key_or_attribute)
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_by_key_or_attribute(object, key_or_attribute)
|
61
|
+
if object.respond_to? :has_key?
|
62
|
+
if object.has_key? key_or_attribute
|
63
|
+
object[key_or_attribute]
|
64
|
+
end
|
65
|
+
elsif object.respond_to? key_or_attribute
|
66
|
+
object.send key_or_attribute
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
ADDRESS_STRUCTURE = {
|
71
|
+
:street => :addressLine,
|
72
|
+
:address => :addressLine,
|
73
|
+
:city => :locality,
|
74
|
+
:state => :adminDistrict,
|
75
|
+
:province => :adminDistrict,
|
76
|
+
:state_province => :adminDistrict,
|
77
|
+
:country => :countryRegion,
|
78
|
+
:postal_code => :postalCode,
|
79
|
+
:zip_code => :postalCode,
|
80
|
+
:zipcode => :postalCode,
|
81
|
+
:zip => :postalCode
|
82
|
+
}
|
83
|
+
|
84
|
+
def structure_address(address)
|
85
|
+
ADDRESS_STRUCTURE.inject({}) do |structured, key_val|
|
86
|
+
unconverted, converted = key_val
|
87
|
+
|
88
|
+
if has_key_or_attribute? address, converted
|
89
|
+
structured[converted] = get_by_key_or_attribute address, converted
|
90
|
+
elsif has_key_or_attribute? address, unconverted
|
91
|
+
structured[converted] = get_by_key_or_attribute address, unconverted
|
92
|
+
end
|
93
|
+
structured
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
WAYPOINT_FORMAT = [:street,
|
98
|
+
:address,
|
99
|
+
:city,
|
100
|
+
",",
|
101
|
+
:state,
|
102
|
+
:province,
|
103
|
+
:state_province,
|
104
|
+
:country,
|
105
|
+
:postal_code,
|
106
|
+
:zipcode,
|
107
|
+
:zip_code,
|
108
|
+
:zip]
|
109
|
+
|
110
|
+
def format_waypoint(waypoint)
|
111
|
+
return waypoint if waypoint.instance_of? String
|
112
|
+
|
113
|
+
if WAYPOINT_FORMAT
|
114
|
+
.find_all { |el| el.instance_of? Symbol }
|
115
|
+
.any? { |key| has_key_or_attribute?(waypoint, key) }
|
116
|
+
|
117
|
+
# find all matching elements
|
118
|
+
q = WAYPOINT_FORMAT.map do |attr|
|
119
|
+
attr.instance_of?(String) ? attr : get_by_key_or_attribute(waypoint, attr)
|
120
|
+
end.find_all { |el| el }
|
121
|
+
|
122
|
+
q.inject('') do |query, el|
|
123
|
+
# if it's punctuation or the first character, don't put a space before it
|
124
|
+
if el =~ /^[.,!?]$/ or query.length == 0
|
125
|
+
"#{query}#{el}"
|
126
|
+
else
|
127
|
+
"#{query} #{el}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
else
|
131
|
+
# we didn't have any elements matching
|
132
|
+
waypoint.to_s
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/bing_response")
|
2
|
+
|
3
|
+
module Periplus
|
4
|
+
class Route < BingResponse
|
5
|
+
attr_accessor :response
|
6
|
+
attr_accessor :distance
|
7
|
+
attr_accessor :distance_unit
|
8
|
+
attr_accessor :duration
|
9
|
+
attr_accessor :route
|
10
|
+
|
11
|
+
def parse
|
12
|
+
super()
|
13
|
+
|
14
|
+
@distance = @primary_resource["travelDistance"]
|
15
|
+
@distance_unit = @primary_resource["distanceUnit"].downcase.to_sym
|
16
|
+
@duration = @primary_resource["travelDuration"]
|
17
|
+
@route = @primary_resource
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/periplus.rb
CHANGED
@@ -1,116 +1,32 @@
|
|
1
1
|
require "httparty"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/periplus/bing_response")
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + "/periplus/request")
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/periplus/route")
|
5
|
+
require File.expand_path(File.dirname(__FILE__) + "/periplus/location")
|
2
6
|
|
3
7
|
module Periplus
|
4
|
-
|
5
|
-
|
6
|
-
format :json
|
7
|
-
|
8
|
-
def initialize(api_key)
|
9
|
-
@api_key = api_key
|
10
|
-
end
|
11
|
-
|
12
|
-
BING_URL = "http://dev.virtualearth.net/REST/v1/Routes"
|
13
|
-
def route(waypoints, options = {})
|
14
|
-
options = options.merge(hashify_waypoints(waypoints))
|
15
|
-
.merge(:o => "json", :key => @api_key)
|
16
|
-
Route.new (self.class.get BING_URL, :query => options)
|
17
|
-
end
|
8
|
+
include HTTParty
|
9
|
+
format :json
|
18
10
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
waypoints.inject({}) do |hash, waypoint|
|
24
|
-
hash["wp.#{counter}"] = format_waypoint(waypoint)
|
25
|
-
counter = counter + 1
|
26
|
-
hash
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def has_key_or_attribute?(object, key_or_attribute)
|
31
|
-
object.respond_to? key_or_attribute or
|
32
|
-
(object.respond_to? :has_key? and object.has_key? key_or_attribute)
|
33
|
-
end
|
34
|
-
|
35
|
-
def get_by_key_or_attribute(object, key_or_attribute)
|
36
|
-
if object.respond_to? :has_key? and object.has_key? key_or_attribute
|
37
|
-
object[key_or_attribute]
|
38
|
-
elsif object.respond_to? key_or_attribute
|
39
|
-
object.send key_or_attribute
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
WAYPOINT_FORMAT = [:street,
|
44
|
-
:address,
|
45
|
-
:city,
|
46
|
-
",",
|
47
|
-
:state,
|
48
|
-
:province,
|
49
|
-
:state_province,
|
50
|
-
:country,
|
51
|
-
:postal_code]
|
52
|
-
|
53
|
-
def format_waypoint(waypoint)
|
54
|
-
return waypoint if waypoint.instance_of? String
|
55
|
-
|
56
|
-
if WAYPOINT_FORMAT
|
57
|
-
.find_all { |el| el.instance_of? Symbol }
|
58
|
-
.any? { |key| has_key_or_attribute?(waypoint, key) }
|
59
|
-
|
60
|
-
# find all matching elements
|
61
|
-
q = WAYPOINT_FORMAT.map do |attr|
|
62
|
-
attr.instance_of?(String) ? attr : get_by_key_or_attribute(waypoint, attr)
|
63
|
-
end.find_all { |el| el }
|
64
|
-
|
65
|
-
q.inject('') do |query, el|
|
66
|
-
# if it's punctuation or the first character, don't put a space before it
|
67
|
-
if el =~ /^[.,!?]$/ or query.length == 0
|
68
|
-
"#{query}#{el}"
|
69
|
-
else
|
70
|
-
"#{query} #{el}"
|
71
|
-
end
|
72
|
-
end
|
73
|
-
else
|
74
|
-
# we didn't have any elements matching
|
75
|
-
waypoint.to_s
|
76
|
-
end
|
77
|
-
end
|
11
|
+
def self.route_details(key, waypoints, options = {})
|
12
|
+
request = Request.new key
|
13
|
+
url = request.route_details_url waypoints, options
|
14
|
+
Route.new (get url)
|
78
15
|
end
|
79
16
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
attr_accessor :route
|
86
|
-
|
87
|
-
def initialize(httparty_response)
|
88
|
-
@response = httparty_response
|
89
|
-
|
90
|
-
if @response.response.kind_of? Net::HTTPClientError
|
91
|
-
http_code = @response.response.code
|
92
|
-
http_message = @response.response.message
|
93
|
-
raise "An error has occurred communicating with the Bing Maps service. HTTP Status: #{http_code} (#{http_message})"
|
94
|
-
end
|
95
|
-
|
96
|
-
parse_resource_sets
|
97
|
-
end
|
98
|
-
|
99
|
-
def parse_resource_sets
|
100
|
-
resource_sets = @response.parsed_response["resourceSets"]
|
101
|
-
raise "No route found." if resource_sets == nil or resource_sets.length == 0
|
102
|
-
|
103
|
-
resources = resource_sets.first["resources"]
|
104
|
-
raise "No route found." if resources == nil or resources.length == 0
|
105
|
-
|
106
|
-
primary_route = resources.first
|
107
|
-
|
108
|
-
@distance = primary_route["travelDistance"]
|
109
|
-
@distance_unit = primary_route["distanceUnit"].downcase.to_sym
|
17
|
+
def self.location_details(key, address, options = {})
|
18
|
+
request = Request.new key
|
19
|
+
url = request.location_details_url address, options
|
20
|
+
Location.new (get url)
|
21
|
+
end
|
110
22
|
|
111
|
-
|
23
|
+
def self.route_map_url(key, waypoints, options = {})
|
24
|
+
request = Request.new key
|
25
|
+
request.route_map_url waypoints, options
|
26
|
+
end
|
112
27
|
|
113
|
-
|
114
|
-
|
28
|
+
def self.address_map_url(key, address, options = {})
|
29
|
+
request = Request.new key
|
30
|
+
request.address_map_url address, options
|
115
31
|
end
|
116
32
|
end
|
data/periplus.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{periplus}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ryan Crum"]
|
12
|
-
s.date = %q{2011-06-
|
12
|
+
s.date = %q{2011-06-22}
|
13
13
|
s.description = %q{A library that behaves as a client for the Bing Maps REST sdk}
|
14
14
|
s.email = %q{ryan.j.crum@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,7 +26,14 @@ Gem::Specification.new do |s|
|
|
26
26
|
"Rakefile",
|
27
27
|
"VERSION",
|
28
28
|
"lib/periplus.rb",
|
29
|
+
"lib/periplus/bing_response.rb",
|
30
|
+
"lib/periplus/location.rb",
|
31
|
+
"lib/periplus/request.rb",
|
32
|
+
"lib/periplus/route.rb",
|
29
33
|
"periplus.gemspec",
|
34
|
+
"spec/bing_response.spec",
|
35
|
+
"spec/location_spec.rb",
|
36
|
+
"spec/periplus_spec.rb",
|
30
37
|
"spec/request_spec.rb",
|
31
38
|
"spec/route_spec.rb",
|
32
39
|
"spec/spec_helper.rb"
|
@@ -42,12 +49,14 @@ Gem::Specification.new do |s|
|
|
42
49
|
|
43
50
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
51
|
s.add_runtime_dependency(%q<httparty>, [">= 0"])
|
52
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
45
53
|
s.add_development_dependency(%q<rspec>, ["~> 2.4.0"])
|
46
54
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
47
55
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
|
48
56
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
49
57
|
else
|
50
58
|
s.add_dependency(%q<httparty>, [">= 0"])
|
59
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
51
60
|
s.add_dependency(%q<rspec>, ["~> 2.4.0"])
|
52
61
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
62
|
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
@@ -55,6 +64,7 @@ Gem::Specification.new do |s|
|
|
55
64
|
end
|
56
65
|
else
|
57
66
|
s.add_dependency(%q<httparty>, [">= 0"])
|
67
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
58
68
|
s.add_dependency(%q<rspec>, ["~> 2.4.0"])
|
59
69
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
60
70
|
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'periplus'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
describe "BingResponse" do
|
5
|
+
it "throws an exception if the httparty response is HTTP status 4xx" do
|
6
|
+
http_error = Net::HTTPUnauthorized.new "1_1", 401, "Unauthorized"
|
7
|
+
response = mock HTTParty::Response, :response => http_error
|
8
|
+
|
9
|
+
begin
|
10
|
+
Periplus::BingResponse.new(response)
|
11
|
+
rescue Exception => e
|
12
|
+
e.message.should == "An error has occurred communicating with the Bing Maps service. HTTP Status: 401 (Unauthorized)"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "throws an exception if the httparty response contains no routes" do
|
17
|
+
http_ok = Net::HTTPOK.new "1_1", 200, "OK"
|
18
|
+
empty_resource_sets = {
|
19
|
+
"resourceSets" => []
|
20
|
+
}
|
21
|
+
|
22
|
+
empty_single_resource_set = {
|
23
|
+
"resourceSets" => [{"resources" => []}]
|
24
|
+
}
|
25
|
+
|
26
|
+
[empty_resource_sets, empty_single_resource_set].each do |parsed_response|
|
27
|
+
response = mock HTTParty::Response, :response => http_ok, :parsed_response => parsed_response
|
28
|
+
|
29
|
+
begin
|
30
|
+
Periplus::BingResponse.new(response)
|
31
|
+
rescue Exception => e
|
32
|
+
e.message.should == "Not found."
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'periplus'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
describe "Location" do
|
5
|
+
it "stores shortcut attibutes property" do
|
6
|
+
http_ok = Net::HTTPOK.new "1_1", 200, "OK"
|
7
|
+
parsed_response = {
|
8
|
+
"resourceSets" => [{
|
9
|
+
"resources" => [{
|
10
|
+
"point" => {
|
11
|
+
"coordinates" => [5, -5]
|
12
|
+
},
|
13
|
+
"address" => {:my_address => "junk"},
|
14
|
+
"confidence" => "High",
|
15
|
+
"entityType" => "Address"
|
16
|
+
}]
|
17
|
+
}]
|
18
|
+
}
|
19
|
+
|
20
|
+
response = mock HTTParty::Response, :response => http_ok, :parsed_response => parsed_response
|
21
|
+
location = Periplus::Location.new response
|
22
|
+
location.latitude.should == 5
|
23
|
+
location.longitude.should == -5
|
24
|
+
location.address.should == {:my_address => "junk"}
|
25
|
+
location.confidence.should == :high
|
26
|
+
location.entity_type.should == :address
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'periplus'
|
2
|
+
|
3
|
+
describe "Periplus" do
|
4
|
+
it "should get route details" do
|
5
|
+
request = mock Periplus::Request
|
6
|
+
request.should_receive(:route_details_url).and_return("http://awesome")
|
7
|
+
Periplus::Request.should_receive(:new).and_return(request)
|
8
|
+
|
9
|
+
http_ok = Net::HTTPOK.new "1_1", 200, "OK"
|
10
|
+
parsed_response = {
|
11
|
+
"resourceSets" => [{
|
12
|
+
"resources" => [{
|
13
|
+
"travelDistance" => 50,
|
14
|
+
"travelDuration" => 12345,
|
15
|
+
"distanceUnit" => "Mile"
|
16
|
+
}]
|
17
|
+
}]
|
18
|
+
}
|
19
|
+
response = mock HTTParty::Response, :response => http_ok, :parsed_response => parsed_response
|
20
|
+
Periplus.should_receive(:get).and_return(response)
|
21
|
+
|
22
|
+
route = Periplus.route_details "KEY", [], {}
|
23
|
+
route.distance.should == 50
|
24
|
+
route.duration.should == 12345
|
25
|
+
route.distance_unit.should == :mile
|
26
|
+
route.route.should == parsed_response["resourceSets"].first["resources"].first
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/spec/request_spec.rb
CHANGED
@@ -30,6 +30,21 @@ describe "Request" do
|
|
30
30
|
}
|
31
31
|
end
|
32
32
|
|
33
|
+
it "generates a route map url correctly" do
|
34
|
+
req = Periplus::Request.new 'fake_key'
|
35
|
+
|
36
|
+
waypoints = ["New York, NY", "Buffalo, NY", "Louisville, KY"]
|
37
|
+
url = req.route_map_url waypoints
|
38
|
+
url.should == "http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/Routes/Driving?wp.1=New%20York%2C%20NY&wp.2=Buffalo%2C%20NY&wp.3=Louisville%2C%20KY&key=fake_key"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "generates an address map url correctly" do
|
42
|
+
req = Periplus::Request.new 'fake_key'
|
43
|
+
address = "Anchorage, AK"
|
44
|
+
url = req.address_map_url address
|
45
|
+
url.should == "http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/Anchorage,%20AK?key=fake_key"
|
46
|
+
end
|
47
|
+
|
33
48
|
it "formats an address object correctly" do
|
34
49
|
class PeriplusAddress
|
35
50
|
attr_accessor :address
|
@@ -51,4 +66,18 @@ describe "Request" do
|
|
51
66
|
|
52
67
|
formatted.should == "1600 Pennsylvania Ave Washington, DC US 20500"
|
53
68
|
end
|
69
|
+
|
70
|
+
it "builds a location details url correctly" do
|
71
|
+
address = {
|
72
|
+
:street => "1600 Pennsylvania Ave",
|
73
|
+
:city => "Washington",
|
74
|
+
:state => "DC",
|
75
|
+
:country => "US",
|
76
|
+
:zip => 20500
|
77
|
+
}
|
78
|
+
|
79
|
+
req = Periplus::Request.new 'fake key'
|
80
|
+
url = req.location_details_url address
|
81
|
+
url.should == "http://dev.virtualearth.net/REST/v1/Locations?key=fake%20key&o=json&addressLine=1600%20Pennsylvania%20Ave&locality=Washington&adminDistrict=DC&countryRegion=US&postalCode=20500"
|
82
|
+
end
|
54
83
|
end
|
data/spec/route_spec.rb
CHANGED
@@ -2,38 +2,6 @@ require 'periplus'
|
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
3
|
|
4
4
|
describe "Route" do
|
5
|
-
it "throws an exception if the httparty response is HTTP status 4xx" do
|
6
|
-
http_error = Net::HTTPUnauthorized.new "1_1", 401, "Unauthorized"
|
7
|
-
response = mock HTTParty::Response, :response => http_error
|
8
|
-
|
9
|
-
begin
|
10
|
-
Periplus::Route.new(response)
|
11
|
-
rescue Exception => e
|
12
|
-
e.message.should == "An error has occurred communicating with the Bing Maps service. HTTP Status: 401 (Unauthorized)"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
it "throws an exception if the httparty response contains no routes" do
|
17
|
-
http_ok = Net::HTTPOK.new "1_1", 200, "OK"
|
18
|
-
empty_resource_sets = {
|
19
|
-
"resourceSets" => []
|
20
|
-
}
|
21
|
-
|
22
|
-
empty_single_resource_set = {
|
23
|
-
"resourceSets" => [{"resources" => []}]
|
24
|
-
}
|
25
|
-
|
26
|
-
[empty_resource_sets, empty_single_resource_set].each do |parsed_response|
|
27
|
-
response = mock HTTParty::Response, :response => http_ok, :parsed_response => parsed_response
|
28
|
-
|
29
|
-
begin
|
30
|
-
Periplus::Route.new(response)
|
31
|
-
rescue Exception => e
|
32
|
-
e.message.should == "No route found."
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
5
|
it "stores shortcut attributes properly" do
|
38
6
|
http_ok = Net::HTTPOK.new "1_1", 200, "OK"
|
39
7
|
parsed_response = {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: periplus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-06-
|
12
|
+
date: 2011-06-22 00:00:00.000000000 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: httparty
|
17
|
-
requirement: &
|
17
|
+
requirement: &2168771280 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,21 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2168771280
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activesupport
|
28
|
+
requirement: &2168769920 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2168769920
|
26
37
|
- !ruby/object:Gem::Dependency
|
27
38
|
name: rspec
|
28
|
-
requirement: &
|
39
|
+
requirement: &2168769200 !ruby/object:Gem::Requirement
|
29
40
|
none: false
|
30
41
|
requirements:
|
31
42
|
- - ~>
|
@@ -33,10 +44,10 @@ dependencies:
|
|
33
44
|
version: 2.4.0
|
34
45
|
type: :development
|
35
46
|
prerelease: false
|
36
|
-
version_requirements: *
|
47
|
+
version_requirements: *2168769200
|
37
48
|
- !ruby/object:Gem::Dependency
|
38
49
|
name: bundler
|
39
|
-
requirement: &
|
50
|
+
requirement: &2168768480 !ruby/object:Gem::Requirement
|
40
51
|
none: false
|
41
52
|
requirements:
|
42
53
|
- - ~>
|
@@ -44,10 +55,10 @@ dependencies:
|
|
44
55
|
version: 1.0.0
|
45
56
|
type: :development
|
46
57
|
prerelease: false
|
47
|
-
version_requirements: *
|
58
|
+
version_requirements: *2168768480
|
48
59
|
- !ruby/object:Gem::Dependency
|
49
60
|
name: jeweler
|
50
|
-
requirement: &
|
61
|
+
requirement: &2168790180 !ruby/object:Gem::Requirement
|
51
62
|
none: false
|
52
63
|
requirements:
|
53
64
|
- - ~>
|
@@ -55,10 +66,10 @@ dependencies:
|
|
55
66
|
version: 1.6.2
|
56
67
|
type: :development
|
57
68
|
prerelease: false
|
58
|
-
version_requirements: *
|
69
|
+
version_requirements: *2168790180
|
59
70
|
- !ruby/object:Gem::Dependency
|
60
71
|
name: rcov
|
61
|
-
requirement: &
|
72
|
+
requirement: &2168817340 !ruby/object:Gem::Requirement
|
62
73
|
none: false
|
63
74
|
requirements:
|
64
75
|
- - ! '>='
|
@@ -66,7 +77,7 @@ dependencies:
|
|
66
77
|
version: '0'
|
67
78
|
type: :development
|
68
79
|
prerelease: false
|
69
|
-
version_requirements: *
|
80
|
+
version_requirements: *2168817340
|
70
81
|
description: A library that behaves as a client for the Bing Maps REST sdk
|
71
82
|
email: ryan.j.crum@gmail.com
|
72
83
|
executables: []
|
@@ -84,7 +95,14 @@ files:
|
|
84
95
|
- Rakefile
|
85
96
|
- VERSION
|
86
97
|
- lib/periplus.rb
|
98
|
+
- lib/periplus/bing_response.rb
|
99
|
+
- lib/periplus/location.rb
|
100
|
+
- lib/periplus/request.rb
|
101
|
+
- lib/periplus/route.rb
|
87
102
|
- periplus.gemspec
|
103
|
+
- spec/bing_response.spec
|
104
|
+
- spec/location_spec.rb
|
105
|
+
- spec/periplus_spec.rb
|
88
106
|
- spec/request_spec.rb
|
89
107
|
- spec/route_spec.rb
|
90
108
|
- spec/spec_helper.rb
|
@@ -104,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
122
|
version: '0'
|
105
123
|
segments:
|
106
124
|
- 0
|
107
|
-
hash:
|
125
|
+
hash: -1957350740199156852
|
108
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
127
|
none: false
|
110
128
|
requirements:
|