bing 0.1.1 → 0.2.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.
- data/History.txt +13 -0
- data/Manifest.txt +3 -2
- data/lib/bing/core_ext.rb +41 -28
- data/lib/bing/errors.rb +42 -39
- data/lib/bing/location.rb +14 -20
- data/lib/bing/request.rb +3 -3
- data/lib/bing/rest_resource.rb +51 -0
- data/lib/bing/route/itinerary.rb +1 -1
- data/lib/bing/route.rb +10 -16
- data/lib/bing.rb +5 -8
- data/test/test_bing_location.rb +19 -13
- data/test/{test_request.rb → test_bing_request.rb} +3 -3
- data/test/test_bing_rest_resource.rb +66 -0
- data/test/test_bing_route.rb +22 -16
- data/test/test_bing_route_itinerary.rb +1 -1
- data/test/test_core_ext.rb +2 -2
- metadata +10 -8
- data/lib/bing/formatting_helper.rb +0 -21
data/History.txt
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
=== 0.2.0 / 2011-08-05
|
2
|
+
* 4 minor enhancement(s):
|
3
|
+
|
4
|
+
* Added ability for user to specify api version.
|
5
|
+
* Namespacing better, no more mixins, and added rest resource superclass.
|
6
|
+
* Only add core extensions if they are not already defined.
|
7
|
+
* Refined the rest resource and added more coverage.
|
8
|
+
|
9
|
+
* 2 unknown:
|
10
|
+
|
11
|
+
* Added tests for bing rest resource and various comments.
|
12
|
+
* Namespaced request object in favor of mixin.
|
13
|
+
|
1
14
|
=== 0.1.1 / 2011-07-25
|
2
15
|
* 5 unknown:
|
3
16
|
|
data/Manifest.txt
CHANGED
@@ -7,15 +7,16 @@ Rakefile
|
|
7
7
|
lib/bing.rb
|
8
8
|
lib/bing/core_ext.rb
|
9
9
|
lib/bing/errors.rb
|
10
|
-
lib/bing/formatting_helper.rb
|
11
10
|
lib/bing/location.rb
|
12
11
|
lib/bing/request.rb
|
12
|
+
lib/bing/rest_resource.rb
|
13
13
|
lib/bing/route.rb
|
14
14
|
lib/bing/route/itinerary.rb
|
15
15
|
test/helper.rb
|
16
16
|
test/test_bing.rb
|
17
17
|
test/test_bing_location.rb
|
18
|
+
test/test_bing_request.rb
|
19
|
+
test/test_bing_rest_resource.rb
|
18
20
|
test/test_bing_route.rb
|
19
21
|
test/test_bing_route_itinerary.rb
|
20
22
|
test/test_core_ext.rb
|
21
|
-
test/test_request.rb
|
data/lib/bing/core_ext.rb
CHANGED
@@ -1,46 +1,59 @@
|
|
1
|
-
# core extensions to ruby
|
2
|
-
class Object
|
1
|
+
# core extensions to ruby.
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
unless respond_to? :blank?
|
4
|
+
class Object
|
5
|
+
def blank?
|
6
|
+
respond_to?(:empty?) ? empty? : !self
|
7
|
+
end
|
6
8
|
end
|
9
|
+
end
|
10
|
+
|
7
11
|
|
8
|
-
|
9
|
-
|
12
|
+
unless respond_to? :to_param
|
13
|
+
class Object
|
14
|
+
def to_param
|
15
|
+
to_s
|
16
|
+
end
|
10
17
|
end
|
18
|
+
end
|
11
19
|
|
12
|
-
def to_query key
|
13
|
-
require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
|
14
|
-
_key = CGI.escape(key.to_s.camelize(:lower)).gsub(/%(5B|5D)/n) {
|
15
|
-
[$1].pack('H*')
|
16
|
-
}
|
17
|
-
value = CGI.escape(to_param.to_s)
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
unless respond_to? :to_lower_camelized_query
|
22
|
+
class Object
|
23
|
+
def to_lower_camelized_query key
|
24
|
+
require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
|
25
|
+
_key = CGI.escape(key.to_s.camelize(:lower)).gsub(/%(5B|5D)/n) {
|
26
|
+
[$1].pack('H*')
|
27
|
+
}
|
28
|
+
value = CGI.escape(to_param.to_s)
|
21
29
|
|
30
|
+
"#{_key}=#{value}"
|
31
|
+
end
|
32
|
+
end
|
22
33
|
end
|
23
34
|
|
24
|
-
class Hash
|
25
35
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
36
|
+
unless {}.respond_to? :to_lower_camelized_param
|
37
|
+
class Hash
|
38
|
+
def to_lower_camelized_param namespace = nil
|
39
|
+
collect do |key, value|
|
40
|
+
value.to_lower_camelized_query(namespace ? "#{namespace}[#{key}]" : key)
|
41
|
+
end.sort * '&'
|
42
|
+
end
|
30
43
|
end
|
31
|
-
|
32
44
|
end
|
33
45
|
|
34
|
-
class String
|
35
46
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
47
|
+
unless ''.respond_to? :camelize
|
48
|
+
class String
|
49
|
+
def camelize first_letter = :upper
|
50
|
+
case first_letter
|
51
|
+
when :upper then
|
52
|
+
self.gsub(/\/(.?)/) {"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/) {$1.upcase}
|
53
|
+
when :lower then
|
54
|
+
self[0].chr.downcase + camelize()[1..-1]
|
55
|
+
end
|
42
56
|
end
|
43
57
|
end
|
44
|
-
|
45
58
|
end
|
46
59
|
|
data/lib/bing/errors.rb
CHANGED
@@ -1,55 +1,58 @@
|
|
1
|
-
|
2
|
-
# DO NOT use these classes. They are too generic.
|
1
|
+
module Bing
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
end
|
3
|
+
##
|
4
|
+
# DO NOT use these classes. They are too generic.
|
7
5
|
|
8
|
-
class
|
9
|
-
|
6
|
+
class ServiceError < StandardError
|
7
|
+
def status() 500 end
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
end
|
10
|
+
class ResourceMissing < StandardError
|
11
|
+
def status() 502 end
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
def message() 'Resource is empty or nil.' end
|
14
|
+
end
|
16
15
|
|
17
|
-
|
16
|
+
##
|
17
|
+
# Raised when we get an invalid response from an underlying server
|
18
18
|
|
19
|
-
|
20
|
-
error = new "parse error#{uri ? " from #{uri}" : nil} - #{text.inspect}"
|
21
|
-
error.text = text
|
22
|
-
error.uri = uri
|
23
|
-
error
|
24
|
-
end
|
19
|
+
class BadGateway < ServiceError
|
25
20
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
21
|
+
def self.parse_error text, uri = nil
|
22
|
+
error = new "parse error#{uri ? " from #{uri}" : nil} - #{text.inspect}"
|
23
|
+
error.text = text
|
24
|
+
error.uri = uri
|
25
|
+
error
|
26
|
+
end
|
33
27
|
|
34
|
-
|
35
|
-
|
28
|
+
def self.bad_response code, uri, server_message=nil
|
29
|
+
server_message = " with message #{server_message}" if server_message
|
30
|
+
error = new "#{uri} returned #{code}#{server_message}"
|
31
|
+
error.code = code
|
32
|
+
error.uri = uri
|
33
|
+
error
|
34
|
+
end
|
36
35
|
|
37
|
-
|
36
|
+
##
|
37
|
+
# Status code from underlying server
|
38
38
|
|
39
|
-
|
40
|
-
# Text we were unable to parse
|
39
|
+
attr_accessor :code
|
41
40
|
|
42
|
-
|
41
|
+
##
|
42
|
+
# Text we were unable to parse
|
43
43
|
|
44
|
-
|
45
|
-
# URI for this request
|
44
|
+
attr_accessor :text
|
46
45
|
|
47
|
-
|
46
|
+
##
|
47
|
+
# URI for this request
|
48
48
|
|
49
|
-
|
50
|
-
end
|
49
|
+
attr_accessor :uri
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
def status() 502 end
|
52
|
+
end
|
53
|
+
|
54
|
+
class ItineraryResourceMissing < ResourceMissing; end
|
55
|
+
class LocationResourceMissing < ResourceMissing; end
|
56
|
+
class RouteResourceMissing < ResourceMissing; end
|
55
57
|
|
58
|
+
end
|
data/lib/bing/location.rb
CHANGED
@@ -1,36 +1,30 @@
|
|
1
|
-
require 'bing'
|
1
|
+
require 'bing/rest_resource'
|
2
2
|
|
3
3
|
##
|
4
4
|
# Responsible for obtaining a location based off of data passed in.
|
5
|
-
# Most common method is to do:
|
6
|
-
#
|
7
|
-
# Bing::Location.find :query => '123 Address City State'
|
8
5
|
|
9
|
-
class Bing::Location
|
10
|
-
|
11
|
-
##
|
12
|
-
# Path to location resource.
|
13
|
-
|
14
|
-
PATH = '/REST/v1/Locations'
|
6
|
+
class Bing::Location < Bing::RestResource
|
15
7
|
|
16
8
|
##
|
17
9
|
# Will find locations based off of +query+ and return an array of
|
18
|
-
# Bing::Location objects.
|
10
|
+
# Bing::Location objects. Will support any param that bing supports.
|
11
|
+
#
|
12
|
+
# === Example
|
13
|
+
#
|
14
|
+
# Bing::Location.find :query => '123 Address City State'
|
19
15
|
|
20
16
|
def self.find opts
|
21
|
-
|
22
|
-
|
23
|
-
)
|
17
|
+
map_find opts.to_lower_camelized_param
|
18
|
+
end
|
24
19
|
|
25
|
-
|
20
|
+
##
|
21
|
+
# Path to route resource.
|
26
22
|
|
27
|
-
|
28
|
-
|
29
|
-
end.compact
|
23
|
+
def self.path
|
24
|
+
super '/Locations'
|
30
25
|
end
|
31
26
|
|
32
27
|
attr_reader :address
|
33
|
-
attr_reader :bounding_box
|
34
28
|
attr_reader :canonical_description
|
35
29
|
attr_reader :coordinates
|
36
30
|
attr_reader :city
|
@@ -42,7 +36,7 @@ class Bing::Location
|
|
42
36
|
attr_reader :zip
|
43
37
|
|
44
38
|
def initialize resource
|
45
|
-
raise LocationResourceMissing if resource.blank?
|
39
|
+
raise Bing::LocationResourceMissing if resource.blank?
|
46
40
|
|
47
41
|
@confidence = resource['confidence']
|
48
42
|
@entity_type = resource['entityType']
|
data/lib/bing/request.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
##
|
2
2
|
# Responsible for making requests to Bing. Uses persistent HTTP connections.
|
3
3
|
|
4
|
-
|
4
|
+
class Bing::Request
|
5
5
|
|
6
6
|
HTTP = Net::HTTP::Persistent.new
|
7
7
|
USER_AGENT = "Bing Client Version: #{Bing::VERSION}"
|
@@ -10,10 +10,10 @@ module Bing::Request
|
|
10
10
|
##
|
11
11
|
# Perform a get request and ensure that the response.code == 20\d,
|
12
12
|
# otherwise raise a BadGateway.
|
13
|
-
def get uri
|
13
|
+
def self.get uri
|
14
14
|
response = HTTP.request uri
|
15
15
|
|
16
|
-
raise BadGateway.bad_response(response.code, uri) unless
|
16
|
+
raise Bing::BadGateway.bad_response(response.code, uri) unless
|
17
17
|
response.code =~ /20\d/
|
18
18
|
|
19
19
|
response
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'bing'
|
2
|
+
|
3
|
+
class Bing::RestResource
|
4
|
+
|
5
|
+
##
|
6
|
+
# Base Bing Rest route.
|
7
|
+
|
8
|
+
BASE_PATH = "/REST/#{Bing.config[:api_version]}"
|
9
|
+
|
10
|
+
def self._find uri
|
11
|
+
body = JSON.parse Bing::Request.get(uri).body
|
12
|
+
|
13
|
+
body['resourceSets'].first['resources'].map do |resource|
|
14
|
+
new resource
|
15
|
+
end.compact
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.map_uri params
|
19
|
+
Bing.config[:map_uri].merge(
|
20
|
+
"#{path}?key=#{Bing.config[:api_key]}&#{params}"
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.map_find params
|
25
|
+
_find map_uri params
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.path subclass_path = nil
|
29
|
+
"#{BASE_PATH}#{subclass_path}"
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# The map's bounding box.
|
34
|
+
|
35
|
+
attr_reader :bounding_box
|
36
|
+
|
37
|
+
##
|
38
|
+
# Decipher bounding box from bbox in Bing response.
|
39
|
+
|
40
|
+
def bbox box
|
41
|
+
south, west, north, east = *box
|
42
|
+
{
|
43
|
+
:north => north,
|
44
|
+
:east => east,
|
45
|
+
:south => south,
|
46
|
+
:west => west,
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
data/lib/bing/route/itinerary.rb
CHANGED
@@ -8,7 +8,7 @@ class Bing::Route::Itinerary
|
|
8
8
|
attr_reader :travel_mode
|
9
9
|
|
10
10
|
def initialize resource
|
11
|
-
raise ItineraryResourceMissing if resource.blank?
|
11
|
+
raise Bing::ItineraryResourceMissing if resource.blank?
|
12
12
|
|
13
13
|
@distance = resource['travelDistance']
|
14
14
|
@duration = resource['travelDuration']
|
data/lib/bing/route.rb
CHANGED
@@ -1,11 +1,6 @@
|
|
1
|
-
require 'bing'
|
1
|
+
require 'bing/rest_resource'
|
2
2
|
|
3
|
-
class Bing::Route
|
4
|
-
|
5
|
-
##
|
6
|
-
# Path to route resource.
|
7
|
-
|
8
|
-
PATH = '/REST/v1/Routes'
|
3
|
+
class Bing::Route < Bing::RestResource
|
9
4
|
|
10
5
|
##
|
11
6
|
# === Description
|
@@ -35,19 +30,18 @@ class Bing::Route
|
|
35
30
|
|
36
31
|
def self.find opts
|
37
32
|
waypoints = format_waypoints opts.delete :waypoints
|
33
|
+
params = [opts.to_lower_camelized_param, waypoints].join '&'
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
)
|
35
|
+
map_find params
|
36
|
+
end
|
42
37
|
|
43
|
-
|
38
|
+
##
|
39
|
+
# Path to route resource.
|
44
40
|
|
45
|
-
|
46
|
-
|
47
|
-
end.compact
|
41
|
+
def self.path
|
42
|
+
super '/Routes'
|
48
43
|
end
|
49
44
|
|
50
|
-
attr_reader :bounding_box
|
51
45
|
attr_reader :distance_unit
|
52
46
|
attr_reader :duration_unit
|
53
47
|
attr_reader :ending_coordinates
|
@@ -58,7 +52,7 @@ class Bing::Route
|
|
58
52
|
attr_reader :type
|
59
53
|
|
60
54
|
def initialize resource
|
61
|
-
raise RouteResourceMissing if resource.blank?
|
55
|
+
raise Bing::RouteResourceMissing if resource.blank?
|
62
56
|
|
63
57
|
@distance_unit = resource['distanceUnit']
|
64
58
|
@duration_unit = resource['durationUnit']
|
data/lib/bing.rb
CHANGED
@@ -5,12 +5,13 @@ require 'json'
|
|
5
5
|
|
6
6
|
module Bing
|
7
7
|
|
8
|
-
VERSION = '0.
|
8
|
+
VERSION = '0.2.0'
|
9
9
|
|
10
10
|
DEFAULTS = {
|
11
|
-
:api_key
|
12
|
-
:api_uri
|
13
|
-
:
|
11
|
+
:api_key => 'AtsQ7PXwSqL266EUdxMYj3b4-H5A6ubkf8DwH-B4k3rVmmPycUrhmH-lZKHeWXm-',
|
12
|
+
:api_uri => URI.parse('http://api.bing.net'),
|
13
|
+
:api_version => 'v1',
|
14
|
+
:map_uri => URI.parse('http://dev.virtualearth.net'),
|
14
15
|
}
|
15
16
|
|
16
17
|
class << self
|
@@ -30,9 +31,5 @@ end
|
|
30
31
|
|
31
32
|
require 'bing/core_ext'
|
32
33
|
require 'bing/errors'
|
33
|
-
require 'bing/formatting_helper'
|
34
34
|
require 'bing/request'
|
35
35
|
|
36
|
-
include Bing::FormattingHelper
|
37
|
-
# TODO figure out how to allow user defined request object.
|
38
|
-
include Bing::Request
|
data/test/test_bing_location.rb
CHANGED
@@ -2,6 +2,8 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class TestBingLocation < MiniTest::Unit::TestCase
|
4
4
|
|
5
|
+
BL = Bing::Location
|
6
|
+
|
5
7
|
def test_cls_find_successful
|
6
8
|
body = {
|
7
9
|
'resourceSets' => [
|
@@ -11,21 +13,25 @@ class TestBingLocation < MiniTest::Unit::TestCase
|
|
11
13
|
]
|
12
14
|
}.to_json
|
13
15
|
|
14
|
-
mock_map_request 200,
|
16
|
+
mock_map_request 200, BL.path, body
|
15
17
|
|
16
|
-
locs =
|
18
|
+
locs = BL.find :query => '123'
|
17
19
|
|
18
20
|
assert_equal 'full name', locs.first.full_name
|
19
21
|
end
|
20
22
|
|
21
23
|
def test_cls_find_failure
|
22
|
-
mock_map_request 400,
|
24
|
+
mock_map_request 400, BL.path, '{}'
|
23
25
|
|
24
|
-
assert_raises BadGateway do
|
25
|
-
|
26
|
+
assert_raises Bing::BadGateway do
|
27
|
+
BL.find :query => '123'
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
31
|
+
def test_cls_path
|
32
|
+
assert_match %r[Locations], BL.path
|
33
|
+
end
|
34
|
+
|
29
35
|
def test_initialize_with_coordinates
|
30
36
|
resource = {
|
31
37
|
'point' => {
|
@@ -33,7 +39,7 @@ class TestBingLocation < MiniTest::Unit::TestCase
|
|
33
39
|
}
|
34
40
|
}
|
35
41
|
|
36
|
-
bl =
|
42
|
+
bl = BL.new resource
|
37
43
|
|
38
44
|
assert_equal [123, 456], bl.coordinates
|
39
45
|
end
|
@@ -49,7 +55,7 @@ class TestBingLocation < MiniTest::Unit::TestCase
|
|
49
55
|
}
|
50
56
|
}
|
51
57
|
|
52
|
-
bl =
|
58
|
+
bl = BL.new resource
|
53
59
|
|
54
60
|
assert_equal 'address', bl.address
|
55
61
|
assert_equal 'city', bl.city
|
@@ -61,7 +67,7 @@ class TestBingLocation < MiniTest::Unit::TestCase
|
|
61
67
|
def test_initialize_with_confidence
|
62
68
|
resource = { 'confidence' => 'High' }
|
63
69
|
|
64
|
-
bl =
|
70
|
+
bl = BL.new resource
|
65
71
|
|
66
72
|
assert_equal 'High', bl.confidence
|
67
73
|
end
|
@@ -69,7 +75,7 @@ class TestBingLocation < MiniTest::Unit::TestCase
|
|
69
75
|
def test_initialize_with_bbox
|
70
76
|
resource = { 'bbox' => %w[south west north east] }
|
71
77
|
|
72
|
-
bl =
|
78
|
+
bl = BL.new resource
|
73
79
|
|
74
80
|
assert_equal 'north', bl.bounding_box[:north]
|
75
81
|
assert_equal 'east', bl.bounding_box[:east]
|
@@ -80,7 +86,7 @@ class TestBingLocation < MiniTest::Unit::TestCase
|
|
80
86
|
def test_initialize_with_entity_type
|
81
87
|
resource = { 'entityType' => 'Postal' }
|
82
88
|
|
83
|
-
bl =
|
89
|
+
bl = BL.new resource
|
84
90
|
|
85
91
|
assert_equal 'Postal', bl.entity_type
|
86
92
|
end
|
@@ -88,14 +94,14 @@ class TestBingLocation < MiniTest::Unit::TestCase
|
|
88
94
|
def test_initialize_with_full_name
|
89
95
|
resource = { 'name' => '123 street, ca' }
|
90
96
|
|
91
|
-
bl =
|
97
|
+
bl = BL.new resource
|
92
98
|
|
93
99
|
assert_equal '123 street, ca', bl.full_name
|
94
100
|
end
|
95
101
|
|
96
102
|
def test_initialize_without_resource_raises
|
97
|
-
assert_raises LocationResourceMissing do
|
98
|
-
|
103
|
+
assert_raises Bing::LocationResourceMissing do
|
104
|
+
BL.new nil
|
99
105
|
end
|
100
106
|
end
|
101
107
|
|
@@ -9,15 +9,15 @@ class TestRequest < MiniTest::Unit::TestCase
|
|
9
9
|
def test_get_failure
|
10
10
|
stub_request(:any, "http://example.com").to_return(:status => 500)
|
11
11
|
|
12
|
-
assert_raises BadGateway do
|
13
|
-
get @uri
|
12
|
+
assert_raises Bing::BadGateway do
|
13
|
+
Bing::Request.get @uri
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_get_success
|
18
18
|
stub_request(:any, "http://example.com").to_return(:status => 200)
|
19
19
|
|
20
|
-
response = get @uri
|
20
|
+
response = Bing::Request.get @uri
|
21
21
|
|
22
22
|
assert_equal '200', response.code
|
23
23
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Bing::RestResource
|
4
|
+
def initialize *args
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestBingRestResource < MiniTest::Unit::TestCase
|
9
|
+
|
10
|
+
BRR = Bing::RestResource # so cold
|
11
|
+
|
12
|
+
def test_base_path
|
13
|
+
assert_equal '/REST/v1', BRR::BASE_PATH
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_cls__find
|
17
|
+
stub_request(:any, /.*/).to_return(:status => 200, :body => body)
|
18
|
+
|
19
|
+
assert_resource BRR._find URI.parse 'http://example.com'
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_map_uri
|
23
|
+
assert_match %r[.*virtualearth.*REST.*key.*dunkey],
|
24
|
+
BRR.map_uri('dunkey').to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_map_find
|
28
|
+
mock_map_request 200, BRR.path, body
|
29
|
+
|
30
|
+
assert_resource BRR.map_find 'dunkey'
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_path
|
34
|
+
assert_equal "#{BRR::BASE_PATH}", BRR.path
|
35
|
+
assert_equal "#{BRR::BASE_PATH}/chunky", BRR.path('/chunky')
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_bbox
|
39
|
+
box = %w[south west north east]
|
40
|
+
bbox = BRR.new.bbox box
|
41
|
+
|
42
|
+
assert_equal 'north', bbox[:north]
|
43
|
+
assert_equal 'east', bbox[:east]
|
44
|
+
assert_equal 'south', bbox[:south]
|
45
|
+
assert_equal 'west', bbox[:west]
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def assert_resource found
|
51
|
+
assert_kind_of Array, found
|
52
|
+
assert_equal BRR.new.class, found.first.class
|
53
|
+
end
|
54
|
+
|
55
|
+
def body
|
56
|
+
{
|
57
|
+
'resourceSets' => [
|
58
|
+
'resources' => [
|
59
|
+
{'name' => 'full name'}
|
60
|
+
]
|
61
|
+
]
|
62
|
+
}.to_json
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
data/test/test_bing_route.rb
CHANGED
@@ -8,6 +8,8 @@ end
|
|
8
8
|
|
9
9
|
class TestBingRoute < MiniTest::Unit::TestCase
|
10
10
|
|
11
|
+
BR = Bing::Route
|
12
|
+
|
11
13
|
def test_cls_find
|
12
14
|
body = {
|
13
15
|
'resourceSets' => [
|
@@ -17,29 +19,33 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
17
19
|
]
|
18
20
|
}.to_json
|
19
21
|
|
20
|
-
mock_map_request 200,
|
22
|
+
mock_map_request 200, BR.path, body
|
21
23
|
|
22
|
-
route =
|
24
|
+
route = BR.find :waypoints => ['start', 'end']
|
23
25
|
end
|
24
26
|
|
25
27
|
def test_cls_format_waypoints
|
26
28
|
waypoints = ['start', 'end']
|
27
29
|
|
28
30
|
assert_equal 'waypoint.0=start&waypoint.1=end',
|
29
|
-
|
31
|
+
BR.format_waypoints(waypoints)
|
30
32
|
|
31
33
|
waypoints = ["4.9, -1.2", "1.2, 2.2"]
|
32
34
|
|
33
35
|
assert_equal 'waypoint.0=4.9%2C+-1.2&waypoint.1=1.2%2C+2.2',
|
34
|
-
|
36
|
+
BR.format_waypoints(waypoints)
|
37
|
+
|
38
|
+
assert_equal nil, BR.format_waypoints(nil)
|
39
|
+
end
|
35
40
|
|
36
|
-
|
41
|
+
def test_cls_path
|
42
|
+
assert_match %r[Route], BR.path
|
37
43
|
end
|
38
44
|
|
39
45
|
def test_initialize_with_bbox
|
40
46
|
resource = { 'bbox' => %w[south west north east] }
|
41
47
|
|
42
|
-
bl =
|
48
|
+
bl = BR.new resource
|
43
49
|
|
44
50
|
assert_equal 'north', bl.bounding_box[:north]
|
45
51
|
assert_equal 'east', bl.bounding_box[:east]
|
@@ -50,7 +56,7 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
50
56
|
def test_initialize_with_distance_unit
|
51
57
|
resource = { 'distanceUnit' => 'miles' }
|
52
58
|
|
53
|
-
br =
|
59
|
+
br = BR.new resource
|
54
60
|
|
55
61
|
assert_equal 'miles', br.distance_unit
|
56
62
|
end
|
@@ -58,7 +64,7 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
58
64
|
def test_initialize_with_duration_unit
|
59
65
|
resource = { 'durationUnit' => 'sec' }
|
60
66
|
|
61
|
-
br =
|
67
|
+
br = BR.new resource
|
62
68
|
|
63
69
|
assert_equal 'sec', br.duration_unit
|
64
70
|
end
|
@@ -70,7 +76,7 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
70
76
|
}]
|
71
77
|
}
|
72
78
|
|
73
|
-
br =
|
79
|
+
br = BR.new resource
|
74
80
|
|
75
81
|
assert_equal [1,2], br.ending_coordinates
|
76
82
|
end
|
@@ -85,7 +91,7 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
85
91
|
}]
|
86
92
|
}
|
87
93
|
|
88
|
-
br =
|
94
|
+
br = BR.new resource
|
89
95
|
|
90
96
|
assert_equal 1, br.itinerary.first.distance
|
91
97
|
assert_equal 2, br.itinerary.last.distance
|
@@ -98,7 +104,7 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
98
104
|
}]
|
99
105
|
}
|
100
106
|
|
101
|
-
br =
|
107
|
+
br = BR.new resource
|
102
108
|
|
103
109
|
assert_equal [1,2], br.starting_coordinates
|
104
110
|
end
|
@@ -106,7 +112,7 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
106
112
|
def test_initialize_with_total_distance
|
107
113
|
resource = { 'travelDistance' => 117.406223 }
|
108
114
|
|
109
|
-
br =
|
115
|
+
br = BR.new resource
|
110
116
|
|
111
117
|
assert_equal 117.406223, br.total_distance
|
112
118
|
end
|
@@ -114,7 +120,7 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
114
120
|
def test_initialize_with_total_duration
|
115
121
|
resource = { 'travelDuration' => 11723 }
|
116
122
|
|
117
|
-
br =
|
123
|
+
br = BR.new resource
|
118
124
|
|
119
125
|
assert_equal 11723, br.total_duration
|
120
126
|
end
|
@@ -126,14 +132,14 @@ class TestBingRoute < MiniTest::Unit::TestCase
|
|
126
132
|
}]
|
127
133
|
}
|
128
134
|
|
129
|
-
br =
|
135
|
+
br = BR.new resource
|
130
136
|
|
131
137
|
assert_equal 'type', br.type
|
132
138
|
end
|
133
139
|
|
134
140
|
def test_initialize_without_resource_raises
|
135
|
-
assert_raises RouteResourceMissing do
|
136
|
-
|
141
|
+
assert_raises Bing::RouteResourceMissing do
|
142
|
+
BR.new nil
|
137
143
|
end
|
138
144
|
end
|
139
145
|
|
data/test/test_core_ext.rb
CHANGED
@@ -10,10 +10,10 @@ class TestCoreExt < MiniTest::Unit::TestCase
|
|
10
10
|
|
11
11
|
def test_to_param
|
12
12
|
query = {:hi_mom => true}
|
13
|
-
assert_equal 'hiMom=true', query.
|
13
|
+
assert_equal 'hiMom=true', query.to_lower_camelized_param
|
14
14
|
|
15
15
|
query[:yep] = false
|
16
|
-
assert_equal 'hiMom=true&yep=false', query.
|
16
|
+
assert_equal 'hiMom=true&yep=false', query.to_lower_camelized_param
|
17
17
|
end
|
18
18
|
|
19
19
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Avilla
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-08-
|
18
|
+
date: 2011-08-05 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: net-http-persistent
|
@@ -140,18 +140,19 @@ files:
|
|
140
140
|
- lib/bing.rb
|
141
141
|
- lib/bing/core_ext.rb
|
142
142
|
- lib/bing/errors.rb
|
143
|
-
- lib/bing/formatting_helper.rb
|
144
143
|
- lib/bing/location.rb
|
145
144
|
- lib/bing/request.rb
|
145
|
+
- lib/bing/rest_resource.rb
|
146
146
|
- lib/bing/route.rb
|
147
147
|
- lib/bing/route/itinerary.rb
|
148
148
|
- test/helper.rb
|
149
149
|
- test/test_bing.rb
|
150
150
|
- test/test_bing_location.rb
|
151
|
+
- test/test_bing_request.rb
|
152
|
+
- test/test_bing_rest_resource.rb
|
151
153
|
- test/test_bing_route.rb
|
152
154
|
- test/test_bing_route_itinerary.rb
|
153
155
|
- test/test_core_ext.rb
|
154
|
-
- test/test_request.rb
|
155
156
|
- .gemtest
|
156
157
|
homepage: https://github.com/hekaldama/bing
|
157
158
|
licenses: []
|
@@ -190,7 +191,8 @@ summary: Bing api client library that exposes all of Bing's api features.
|
|
190
191
|
test_files:
|
191
192
|
- test/test_bing.rb
|
192
193
|
- test/test_bing_location.rb
|
194
|
+
- test/test_bing_rest_resource.rb
|
193
195
|
- test/test_core_ext.rb
|
194
196
|
- test/test_bing_route.rb
|
195
197
|
- test/test_bing_route_itinerary.rb
|
196
|
-
- test/
|
198
|
+
- test/test_bing_request.rb
|
@@ -1,21 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# Responsible for consolidating response formatting logic shared
|
3
|
-
# amongst resources.
|
4
|
-
|
5
|
-
module Bing::FormattingHelper
|
6
|
-
|
7
|
-
##
|
8
|
-
# Decipher bounding box from bbox in Bing response.
|
9
|
-
|
10
|
-
def bbox box
|
11
|
-
south, west, north, east = *box
|
12
|
-
{
|
13
|
-
:north => north,
|
14
|
-
:east => east,
|
15
|
-
:south => south,
|
16
|
-
:west => west,
|
17
|
-
}
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|