bing 0.1.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.
data/.autotest ADDED
@@ -0,0 +1,9 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+ require 'autotest/isolate'
5
+
6
+ Autotest.add_hook :initialize do |at|
7
+ at.testlib = 'minitest/unit'
8
+ end
9
+
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ tmp
data/History.txt ADDED
@@ -0,0 +1,33 @@
1
+ === 0.1.1 / 2011-07-25
2
+ * 5 unknown:
3
+
4
+ * Make dep gems comply with my needs
5
+ * Rm'd the bin, and added changelog generation.
6
+ * Updated History.
7
+ * Updated manifest.
8
+ * Updated manifest. Added task to create manifest and added autotest task.
9
+
10
+ === 0.1.0 / 2011-07-25
11
+
12
+ * 6 minor enhancement(s):
13
+
14
+ * Added gitignore.
15
+ * Added more tests, more data coming back from Location and beginnings of Route.
16
+ * Improved coverage. Added route class and cleaned up some things.
17
+ * MiniTest is working, autotest is working, structure is getting better and coverage is happening.
18
+ * Moved everything into the lib/bing dir.
19
+ * Test helper clean up and README update.
20
+
21
+ * 0 bug fix(s):
22
+
23
+
24
+ * 7 unknown:
25
+
26
+ * Commiting what I have so far. Still a wip.
27
+ * Flattened the structure
28
+ * Little cleanup.
29
+ * Nothing significant.
30
+ * Prepped for release-0.1.0
31
+ * Rm'd unecessary test files.
32
+ * Updated manifest. Added task to create manifest and added autotest task.
33
+
data/Manifest.txt ADDED
@@ -0,0 +1,21 @@
1
+ .autotest
2
+ .gitignore
3
+ History.txt
4
+ Manifest.txt
5
+ README.txt
6
+ Rakefile
7
+ lib/bing.rb
8
+ lib/bing/core_ext.rb
9
+ lib/bing/errors.rb
10
+ lib/bing/formatting_helper.rb
11
+ lib/bing/location.rb
12
+ lib/bing/request.rb
13
+ lib/bing/route.rb
14
+ lib/bing/route/itinerary.rb
15
+ test/helper.rb
16
+ test/test_bing.rb
17
+ test/test_bing_location.rb
18
+ test/test_bing_route.rb
19
+ test/test_bing_route_itinerary.rb
20
+ test/test_core_ext.rb
21
+ test/test_request.rb
data/README.txt ADDED
@@ -0,0 +1,73 @@
1
+ = bing
2
+
3
+ * https://github.com/hekaldama/bing
4
+
5
+ == DESCRIPTION:
6
+
7
+ Bing api client library that exposes all of Bing's api features.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Uses Net::HTTP::Persistent for connections to Bing.
12
+ * Shipped with a community Bing api key. Only used for testing.
13
+ Use in production may fail.
14
+
15
+ == SYNOPSIS:
16
+
17
+ # For locations
18
+ require 'bing/location'
19
+
20
+ Bing.config[:api_key] = 'my_key'
21
+
22
+ Bing::Location.find :query => 'Strawberry, CA'
23
+
24
+ # For routing
25
+ require 'bing/route'
26
+
27
+ Bing.config[:api_key] = 'my_key'
28
+
29
+ Bing::Route.find :waypoints => ['Shasta, CA', 'Chico, CA', 'Los Angeles, CA']
30
+
31
+ == REQUIREMENTS:
32
+
33
+ For development:
34
+ * rubygems >= 1.8.2
35
+ * isolate >= 3.1.1
36
+
37
+ == INSTALL:
38
+
39
+ [sudo] gem install bing
40
+
41
+ == DEVELOPERS:
42
+
43
+ After checking out the source, run:
44
+
45
+ $ rake newb
46
+
47
+ This task will install any missing dependencies, run the tests, and generate the
48
+ RDoc.
49
+
50
+ == LICENSE:
51
+
52
+ (The MIT License)
53
+
54
+ Copyright (c) 2011 Adam Avilla
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining
57
+ a copy of this software and associated documentation files (the
58
+ 'Software'), to deal in the Software without restriction, including
59
+ without limitation the rights to use, copy, modify, merge, publish,
60
+ distribute, sublicense, and/or sell copies of the Software, and to
61
+ permit persons to whom the Software is furnished to do so, subject to
62
+ the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be
65
+ included in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
68
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
69
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
70
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
71
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
72
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
73
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,100 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugin :isolate
7
+
8
+ require 'isolate/rake'
9
+
10
+ Hoe.spec 'bing' do
11
+ developer('Adam Avilla', 'adam@avil.la')
12
+
13
+ self.testlib = :minitest
14
+
15
+ self.extra_deps << ['net-http-persistent', '>= 1.6']
16
+ self.extra_deps << ['json', '>= 1.5.3']
17
+
18
+ self.extra_dev_deps << ['minitest']
19
+ self.extra_dev_deps << ['git']
20
+ self.extra_dev_deps << ['webmock']
21
+ self.extra_dev_deps << ['ZenTest']
22
+
23
+ # make sure we use the gemmed minitest on 1.9
24
+ self.test_prelude = 'gem "minitest"'
25
+ end
26
+
27
+ desc 'run autotest'
28
+ task :autotest do
29
+ sh "rake isolate:sh[autotest]"
30
+ end
31
+
32
+ desc "create Manifest.txt with git-ls-files."
33
+ task :create_manifest do
34
+ sh "git ls-files > Manifest.txt"
35
+ end
36
+
37
+ desc "run irb with bing lib in path."
38
+ task :irb do
39
+ ARGV.clear
40
+ $: << "lib"
41
+
42
+ require 'irb'
43
+ require 'bing'
44
+
45
+ IRB.start
46
+ end
47
+
48
+ namespace :git do
49
+
50
+ desc "Generate a changelog for a given release."
51
+ task :changelog do
52
+ begin
53
+ require 'rubygems'
54
+ require 'git'
55
+ rescue LoadError
56
+ end
57
+
58
+ from = ENV['since']
59
+ to = ENV['until'] || 'HEAD'
60
+ codes = {"!" => :major, "+" => :minor, "-" => :bug}
61
+ $changes = Hash.new {|h,k| h[k] = Array.new}
62
+
63
+ def changelog_section(code)
64
+ name = {
65
+ :major => "major enhancement(s)",
66
+ :minor => "minor enhancement(s)",
67
+ :bug => "bug fix(s)",
68
+ :huh? => "unknown",
69
+ }[code]
70
+
71
+ puts "* #{$changes[code].size} #{name}:"
72
+ puts
73
+ $changes[code].sort.each do |line|
74
+ puts " * #{line}"
75
+ end
76
+ puts
77
+ end
78
+
79
+ git = Git.open(".")
80
+ git.log(nil).between(from, to).each do |log|
81
+ log.message.each_line do |msg|
82
+ if msg.match(/^([#{Regexp.escape(codes.keys.join)}])\s*(.*)/)
83
+ code, line = codes[$1], $2
84
+ $changes[code] << line
85
+ else
86
+ next if msg =~ /git-svn-id|^\s*$/
87
+ $changes[:huh?] << msg
88
+ end
89
+ end
90
+ end
91
+
92
+ changelog_section :major
93
+ changelog_section :minor
94
+ changelog_section :bug
95
+ changelog_section :huh?
96
+ end
97
+
98
+ end
99
+
100
+ # vim: syntax=ruby
data/lib/bing.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'net/http/persistent'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ module Bing
7
+
8
+ VERSION = '0.1.1'
9
+
10
+ DEFAULTS = {
11
+ :api_key => 'AtsQ7PXwSqL266EUdxMYj3b4-H5A6ubkf8DwH-B4k3rVmmPycUrhmH-lZKHeWXm-',
12
+ :api_uri => URI.parse('http://api.bing.net'),
13
+ :map_uri => URI.parse('http://dev.virtualearth.net'),
14
+ }
15
+
16
+ class << self
17
+ attr_reader :config
18
+ end
19
+
20
+ @config = DEFAULTS.dup
21
+
22
+ ##
23
+ # Set the configuration for this instance of bing.
24
+
25
+ def self.config= config
26
+ @config = DEFAULTS.merge config
27
+ end
28
+
29
+ end
30
+
31
+ require 'bing/core_ext'
32
+ require 'bing/errors'
33
+ require 'bing/formatting_helper'
34
+ require 'bing/request'
35
+
36
+ include Bing::FormattingHelper
37
+ # TODO figure out how to allow user defined request object.
38
+ include Bing::Request
@@ -0,0 +1,46 @@
1
+ # core extensions to ruby
2
+ class Object
3
+
4
+ def blank?
5
+ respond_to?(:empty?) ? empty? : !self
6
+ end
7
+
8
+ def to_param
9
+ to_s
10
+ end
11
+
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
+
19
+ "#{_key}=#{value}"
20
+ end
21
+
22
+ end
23
+
24
+ class Hash
25
+
26
+ def to_param namespace = nil
27
+ collect do |key, value|
28
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
29
+ end.sort * '&'
30
+ end
31
+
32
+ end
33
+
34
+ class String
35
+
36
+ def camelize first_letter = :upper
37
+ case first_letter
38
+ when :upper then
39
+ self.gsub(/\/(.?)/) {"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/) {$1.upcase}
40
+ when :lower then
41
+ self[0].chr.downcase + camelize()[1..-1]
42
+ end
43
+ end
44
+
45
+ end
46
+
@@ -0,0 +1,55 @@
1
+ ##
2
+ # DO NOT use these classes. They are too generic.
3
+
4
+ class ServiceError < StandardError
5
+ def status() 500 end
6
+ end
7
+
8
+ class ResourceMissing < StandardError
9
+ def status() 502 end
10
+
11
+ def message() 'Resource is empty or nil.' end
12
+ end
13
+
14
+ ##
15
+ # Raised when we get an invalid response from an underlying server
16
+
17
+ class BadGateway < ServiceError
18
+
19
+ def self.parse_error text, uri = nil
20
+ error = new "parse error#{uri ? " from #{uri}" : nil} - #{text.inspect}"
21
+ error.text = text
22
+ error.uri = uri
23
+ error
24
+ end
25
+
26
+ def self.bad_response code, uri, server_message=nil
27
+ server_message = " with message #{server_message}" if server_message
28
+ error = new "#{uri} returned #{code}#{server_message}"
29
+ error.code = code
30
+ error.uri = uri
31
+ error
32
+ end
33
+
34
+ ##
35
+ # Status code from underlying server
36
+
37
+ attr_accessor :code
38
+
39
+ ##
40
+ # Text we were unable to parse
41
+
42
+ attr_accessor :text
43
+
44
+ ##
45
+ # URI for this request
46
+
47
+ attr_accessor :uri
48
+
49
+ def status() 502 end
50
+ end
51
+
52
+ class ItineraryResourceMissing < ResourceMissing; end
53
+ class LocationResourceMissing < ResourceMissing; end
54
+ class RouteResourceMissing < ResourceMissing; end
55
+
@@ -0,0 +1,21 @@
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
+
@@ -0,0 +1,69 @@
1
+ require 'bing'
2
+
3
+ ##
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
+
9
+ class Bing::Location
10
+
11
+ ##
12
+ # Path to location resource.
13
+
14
+ PATH = '/REST/v1/Locations'
15
+
16
+ ##
17
+ # Will find locations based off of +query+ and return an array of
18
+ # Bing::Location objects.
19
+
20
+ def self.find opts
21
+ uri = Bing.config[:map_uri].merge(
22
+ "#{PATH}?key=#{Bing.config[:api_key]}&#{opts.to_param}"
23
+ )
24
+
25
+ body = JSON.parse get(uri).body
26
+
27
+ body['resourceSets'].first['resources'].map do |resource|
28
+ new resource
29
+ end.compact
30
+ end
31
+
32
+ attr_reader :address
33
+ attr_reader :bounding_box
34
+ attr_reader :canonical_description
35
+ attr_reader :coordinates
36
+ attr_reader :city
37
+ attr_reader :confidence
38
+ attr_reader :country
39
+ attr_reader :entity_type
40
+ attr_reader :full_name
41
+ attr_reader :state
42
+ attr_reader :zip
43
+
44
+ def initialize resource
45
+ raise LocationResourceMissing if resource.blank?
46
+
47
+ @confidence = resource['confidence']
48
+ @entity_type = resource['entityType']
49
+ @full_name = resource['name']
50
+
51
+ if resource['address'] then
52
+ @address = resource['address']['addressLine']
53
+ @city = resource['address']['locality']
54
+ @country = resource['address']['countryRegion']
55
+ @state = resource['address']['adminDistrict']
56
+ @zip = resource['address']['postalCode']
57
+ end
58
+
59
+ if resource['bbox'] then
60
+ @bounding_box = bbox resource['bbox']
61
+ end
62
+
63
+ if resource['point'] && resource['point']['coordinates'] then
64
+ @coordinates = resource['point']['coordinates']
65
+ end
66
+ end
67
+
68
+ end
69
+
@@ -0,0 +1,23 @@
1
+ ##
2
+ # Responsible for making requests to Bing. Uses persistent HTTP connections.
3
+
4
+ module Bing::Request
5
+
6
+ HTTP = Net::HTTP::Persistent.new
7
+ USER_AGENT = "Bing Client Version: #{Bing::VERSION}"
8
+ HTTP.headers['user-agent'] = USER_AGENT
9
+
10
+ ##
11
+ # Perform a get request and ensure that the response.code == 20\d,
12
+ # otherwise raise a BadGateway.
13
+ def get uri
14
+ response = HTTP.request uri
15
+
16
+ raise BadGateway.bad_response(response.code, uri) unless
17
+ response.code =~ /20\d/
18
+
19
+ response
20
+ end
21
+
22
+ end
23
+
data/lib/bing/route.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'bing'
2
+
3
+ class Bing::Route
4
+
5
+ ##
6
+ # Path to route resource.
7
+
8
+ PATH = '/REST/v1/Routes'
9
+
10
+ ##
11
+ # === Description
12
+ # Will return a route based off of +opts+ passed in. See
13
+ # http://msdn.microsoft.com/en-us/library/ff701717.aspx for reference on
14
+ # allowable +opts+ and what is required. All values in +opts+ are passed
15
+ # through save +waypoints+. The keys should follow ruby's convention of snake
16
+ # case which will translate into MSN's lower camelcase E.g. :date_time will be
17
+ # converted to dateTime for MSN.
18
+ #
19
+ # === opts
20
+ # +waypoints+:: Array of points. E.g. :waypoints =>
21
+ # ['start', 'next', 'end'] will turn into "waypoint.0=
22
+ # start&waypoint.1=next&waypoint.2=end". Reference above MSN
23
+ # docs for allowable waypoints. All points must be strings.
24
+ #
25
+ # === Example
26
+ # Bing::Route.find :avoid => 'minimizeHighways,tolls',
27
+ # :distance_before_first_turn => 500,
28
+ # :distance_unit => 'mi'
29
+ # :heading => 90,
30
+ # :optimize => 'time',
31
+ # :waypoints => ['start', 'next', 'end']
32
+ #
33
+ # === Return
34
+ # http://msdn.microsoft.com/en-us/library/ff701718.aspx
35
+
36
+ def self.find opts
37
+ waypoints = format_waypoints opts.delete :waypoints
38
+
39
+ uri = Bing.config[:map_uri].merge(
40
+ "#{PATH}?key=#{Bing.config[:api_key]}&#{opts.to_param}&#{waypoints}"
41
+ )
42
+
43
+ body = JSON.parse get(uri).body
44
+
45
+ body['resourceSets'].first['resources'].map do |resource|
46
+ new resource
47
+ end.compact
48
+ end
49
+
50
+ attr_reader :bounding_box
51
+ attr_reader :distance_unit
52
+ attr_reader :duration_unit
53
+ attr_reader :ending_coordinates
54
+ attr_reader :itinerary
55
+ attr_reader :starting_coordinates
56
+ attr_reader :total_distance
57
+ attr_reader :total_duration
58
+ attr_reader :type
59
+
60
+ def initialize resource
61
+ raise RouteResourceMissing if resource.blank?
62
+
63
+ @distance_unit = resource['distanceUnit']
64
+ @duration_unit = resource['durationUnit']
65
+ @total_distance = resource['travelDistance']
66
+ @total_duration = resource['travelDuration']
67
+
68
+ if resource['bbox'] then
69
+ @bounding_box = bbox resource['bbox']
70
+ end
71
+
72
+ # TODO does not support multiple route legs. My testing thus far just
73
+ # breaks Bing if I try to obtain multiple anyways.
74
+ if resource['routeLegs'] && leg = resource['routeLegs'].first then
75
+ @type = leg['type']
76
+
77
+ if leg['actualEnd'] && leg['actualEnd']['coordinates'] then
78
+ @ending_coordinates = leg['actualEnd']['coordinates']
79
+ end
80
+
81
+ if leg['actualStart'] && leg['actualStart']['coordinates'] then
82
+ @starting_coordinates = leg['actualStart']['coordinates']
83
+ end
84
+
85
+ unless leg['itineraryItems'].blank? then
86
+ @itinerary = leg['itineraryItems'].collect do |itinerary|
87
+ Bing::Route::Itinerary.new itinerary
88
+ end.compact
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def self.format_waypoints waypoints
96
+ return unless waypoints
97
+ ways = []
98
+
99
+ waypoints.each_with_index do |way, i|
100
+ ways << "waypoint.#{i}=#{CGI.escape way}"
101
+ end
102
+
103
+ ways.join '&'
104
+ end
105
+
106
+ end
107
+
108
+ require 'bing/route/itinerary'
@@ -0,0 +1,28 @@
1
+ class Bing::Route::Itinerary
2
+
3
+ attr_reader :action
4
+ attr_reader :coordinates
5
+ attr_reader :distance
6
+ attr_reader :duration
7
+ attr_reader :instruction
8
+ attr_reader :travel_mode
9
+
10
+ def initialize resource
11
+ raise ItineraryResourceMissing if resource.blank?
12
+
13
+ @distance = resource['travelDistance']
14
+ @duration = resource['travelDuration']
15
+ @travel_mode = resource['travelMode']
16
+
17
+ if instructions = resource['instruction'] then
18
+ @action = instructions['maneuverType']
19
+ @instruction = instructions['text']
20
+ end
21
+
22
+ if resource['maneuverPoint'] && resource['maneuverPoint']['coordinates'] then
23
+ @coordinates = resource['maneuverPoint']['coordinates']
24
+ end
25
+ end
26
+
27
+ end
28
+
data/test/helper.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'logger'
3
+ require "minitest/autorun"
4
+ require "webmock"
5
+
6
+ require "bing"
7
+ require "bing/location"
8
+ require "bing/route"
9
+
10
+ include WebMock::API
11
+
12
+ def mock_map_request status, path, body, headers={}
13
+ stub_request(:any, /.*virtualearth.*#{path}.*/).
14
+ to_return(:status => status, :body => body, :headers => headers)
15
+ end
data/test/test_bing.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'helper'
2
+
3
+ class TestBing < MiniTest::Unit::TestCase
4
+
5
+ def test_defines_version
6
+ assert Bing::VERSION.to_f > 0.0
7
+ end
8
+
9
+ def test_setting_config
10
+ Bing.config = {}
11
+ assert_equal Bing.config, Bing::DEFAULTS
12
+
13
+ Bing.config[:api_key] = 'frank'
14
+ assert_equal 'frank', Bing.config[:api_key]
15
+ refute_equal Bing.config, Bing::DEFAULTS
16
+ end
17
+
18
+ end
19
+
@@ -0,0 +1,103 @@
1
+ require 'helper'
2
+
3
+ class TestBingLocation < MiniTest::Unit::TestCase
4
+
5
+ def test_cls_find_successful
6
+ body = {
7
+ 'resourceSets' => [
8
+ 'resources' => [
9
+ {'name' => 'full name'}
10
+ ]
11
+ ]
12
+ }.to_json
13
+
14
+ mock_map_request 200, '/REST/v1/Locations', body
15
+
16
+ locs = Bing::Location.find :query => '123'
17
+
18
+ assert_equal 'full name', locs.first.full_name
19
+ end
20
+
21
+ def test_cls_find_failure
22
+ mock_map_request 400, '/REST/v1/Locations', '{}'
23
+
24
+ assert_raises BadGateway do
25
+ Bing::Location.find :query => '123'
26
+ end
27
+ end
28
+
29
+ def test_initialize_with_coordinates
30
+ resource = {
31
+ 'point' => {
32
+ 'coordinates' => [123, 456]
33
+ }
34
+ }
35
+
36
+ bl = Bing::Location.new resource
37
+
38
+ assert_equal [123, 456], bl.coordinates
39
+ end
40
+
41
+ def test_initialize_with_address
42
+ resource = {
43
+ 'address' => {
44
+ 'addressLine' => 'address',
45
+ 'locality' => 'city',
46
+ 'countryRegion' => 'country',
47
+ 'adminDistrict' => 'state',
48
+ 'postalCode' => 'zip',
49
+ }
50
+ }
51
+
52
+ bl = Bing::Location.new resource
53
+
54
+ assert_equal 'address', bl.address
55
+ assert_equal 'city', bl.city
56
+ assert_equal 'country', bl.country
57
+ assert_equal 'state', bl.state
58
+ assert_equal 'zip', bl.zip
59
+ end
60
+
61
+ def test_initialize_with_confidence
62
+ resource = { 'confidence' => 'High' }
63
+
64
+ bl = Bing::Location.new resource
65
+
66
+ assert_equal 'High', bl.confidence
67
+ end
68
+
69
+ def test_initialize_with_bbox
70
+ resource = { 'bbox' => %w[south west north east] }
71
+
72
+ bl = Bing::Location.new resource
73
+
74
+ assert_equal 'north', bl.bounding_box[:north]
75
+ assert_equal 'east', bl.bounding_box[:east]
76
+ assert_equal 'south', bl.bounding_box[:south]
77
+ assert_equal 'west', bl.bounding_box[:west]
78
+ end
79
+
80
+ def test_initialize_with_entity_type
81
+ resource = { 'entityType' => 'Postal' }
82
+
83
+ bl = Bing::Location.new resource
84
+
85
+ assert_equal 'Postal', bl.entity_type
86
+ end
87
+
88
+ def test_initialize_with_full_name
89
+ resource = { 'name' => '123 street, ca' }
90
+
91
+ bl = Bing::Location.new resource
92
+
93
+ assert_equal '123 street, ca', bl.full_name
94
+ end
95
+
96
+ def test_initialize_without_resource_raises
97
+ assert_raises LocationResourceMissing do
98
+ Bing::Location.new nil
99
+ end
100
+ end
101
+
102
+ end
103
+
@@ -0,0 +1,141 @@
1
+ require 'helper'
2
+
3
+ class Bing::Route
4
+ class << self
5
+ public :format_waypoints
6
+ end
7
+ end
8
+
9
+ class TestBingRoute < MiniTest::Unit::TestCase
10
+
11
+ def test_cls_find
12
+ body = {
13
+ 'resourceSets' => [
14
+ 'resources' => [
15
+ {'name' => 'full name'}
16
+ ]
17
+ ]
18
+ }.to_json
19
+
20
+ mock_map_request 200, '/REST/v1/Routes', body
21
+
22
+ route = Bing::Route.find :waypoints => ['start', 'end']
23
+ end
24
+
25
+ def test_cls_format_waypoints
26
+ waypoints = ['start', 'end']
27
+
28
+ assert_equal 'waypoint.0=start&waypoint.1=end',
29
+ Bing::Route.format_waypoints(waypoints)
30
+
31
+ waypoints = ["4.9, -1.2", "1.2, 2.2"]
32
+
33
+ assert_equal 'waypoint.0=4.9%2C+-1.2&waypoint.1=1.2%2C+2.2',
34
+ Bing::Route.format_waypoints(waypoints)
35
+
36
+ assert_equal nil, Bing::Route.format_waypoints(nil)
37
+ end
38
+
39
+ def test_initialize_with_bbox
40
+ resource = { 'bbox' => %w[south west north east] }
41
+
42
+ bl = Bing::Route.new resource
43
+
44
+ assert_equal 'north', bl.bounding_box[:north]
45
+ assert_equal 'east', bl.bounding_box[:east]
46
+ assert_equal 'south', bl.bounding_box[:south]
47
+ assert_equal 'west', bl.bounding_box[:west]
48
+ end
49
+
50
+ def test_initialize_with_distance_unit
51
+ resource = { 'distanceUnit' => 'miles' }
52
+
53
+ br = Bing::Route.new resource
54
+
55
+ assert_equal 'miles', br.distance_unit
56
+ end
57
+
58
+ def test_initialize_with_duration_unit
59
+ resource = { 'durationUnit' => 'sec' }
60
+
61
+ br = Bing::Route.new resource
62
+
63
+ assert_equal 'sec', br.duration_unit
64
+ end
65
+
66
+ def test_initialize_with_ending_coordinates
67
+ resource = {
68
+ 'routeLegs' => [{
69
+ 'actualEnd' => {'coordinates' => [1,2]}
70
+ }]
71
+ }
72
+
73
+ br = Bing::Route.new resource
74
+
75
+ assert_equal [1,2], br.ending_coordinates
76
+ end
77
+
78
+ def test_initialize_with_itinerary
79
+ resource = {
80
+ 'routeLegs' => [{
81
+ 'itineraryItems' => [
82
+ {'travelDistance' => 1},
83
+ {'travelDistance' => 2},
84
+ ]
85
+ }]
86
+ }
87
+
88
+ br = Bing::Route.new resource
89
+
90
+ assert_equal 1, br.itinerary.first.distance
91
+ assert_equal 2, br.itinerary.last.distance
92
+ end
93
+
94
+ def test_initialize_with_starting_coordinates
95
+ resource = {
96
+ 'routeLegs' => [{
97
+ 'actualStart' => {'coordinates' => [1,2]}
98
+ }]
99
+ }
100
+
101
+ br = Bing::Route.new resource
102
+
103
+ assert_equal [1,2], br.starting_coordinates
104
+ end
105
+
106
+ def test_initialize_with_total_distance
107
+ resource = { 'travelDistance' => 117.406223 }
108
+
109
+ br = Bing::Route.new resource
110
+
111
+ assert_equal 117.406223, br.total_distance
112
+ end
113
+
114
+ def test_initialize_with_total_duration
115
+ resource = { 'travelDuration' => 11723 }
116
+
117
+ br = Bing::Route.new resource
118
+
119
+ assert_equal 11723, br.total_duration
120
+ end
121
+
122
+ def test_initialize_with_type
123
+ resource = {
124
+ 'routeLegs' => [{
125
+ 'type' => 'type'
126
+ }]
127
+ }
128
+
129
+ br = Bing::Route.new resource
130
+
131
+ assert_equal 'type', br.type
132
+ end
133
+
134
+ def test_initialize_without_resource_raises
135
+ assert_raises RouteResourceMissing do
136
+ Bing::Route.new nil
137
+ end
138
+ end
139
+
140
+ end
141
+
@@ -0,0 +1,60 @@
1
+ require 'helper'
2
+
3
+ class TestBingRouteItinerary < MiniTest::Unit::TestCase
4
+
5
+ def test_initialize_with_action
6
+ resource = { 'instruction' => {'maneuverType' => 'turn'} }
7
+
8
+ bl = Bing::Route::Itinerary.new resource
9
+
10
+ assert_equal 'turn', bl.action
11
+ end
12
+
13
+ def test_initialize_with_distance
14
+ resource = { 'travelDistance' => '3.0' }
15
+
16
+ bl = Bing::Route::Itinerary.new resource
17
+
18
+ assert_equal '3.0', bl.distance
19
+ end
20
+
21
+ def test_initialize_with_duration
22
+ resource = { 'travelDuration' => '100' }
23
+
24
+ bl = Bing::Route::Itinerary.new resource
25
+
26
+ assert_equal '100', bl.duration
27
+ end
28
+
29
+ def test_initialize_with_instruction
30
+ resource = { 'instruction' => {'text' => 'go right'} }
31
+
32
+ bl = Bing::Route::Itinerary.new resource
33
+
34
+ assert_equal 'go right', bl.instruction
35
+ end
36
+
37
+ def test_initialize_with_coordinates
38
+ resource = { 'maneuverPoint' => {'coordinates' => [1,2]} }
39
+
40
+ bl = Bing::Route::Itinerary.new resource
41
+
42
+ assert_equal [1,2], bl.coordinates
43
+ end
44
+
45
+ def test_initialize_with_travel_mode
46
+ resource = { 'travelMode' => 'driving' }
47
+
48
+ bl = Bing::Route::Itinerary.new resource
49
+
50
+ assert_equal 'driving', bl.travel_mode
51
+ end
52
+
53
+ def test_initialize_without_resource_raises
54
+ assert_raises ItineraryResourceMissing do
55
+ Bing::Route::Itinerary.new nil
56
+ end
57
+ end
58
+
59
+ end
60
+
@@ -0,0 +1,20 @@
1
+ require 'helper'
2
+
3
+ class TestCoreExt < MiniTest::Unit::TestCase
4
+
5
+ def test_camelize
6
+ assert_equal 'hiMom', 'hi_mom'.camelize(:lower)
7
+ assert_equal 'HiMom', 'hi_mom'.camelize
8
+ assert_equal 'Hi', 'hi'.camelize
9
+ end
10
+
11
+ def test_to_param
12
+ query = {:hi_mom => true}
13
+ assert_equal 'hiMom=true', query.to_param
14
+
15
+ query[:yep] = false
16
+ assert_equal 'hiMom=true&yep=false', query.to_param
17
+ end
18
+
19
+ end
20
+
@@ -0,0 +1,26 @@
1
+ require 'helper'
2
+
3
+ class TestRequest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @uri = URI.parse 'http://example.com'
7
+ end
8
+
9
+ def test_get_failure
10
+ stub_request(:any, "http://example.com").to_return(:status => 500)
11
+
12
+ assert_raises BadGateway do
13
+ get @uri
14
+ end
15
+ end
16
+
17
+ def test_get_success
18
+ stub_request(:any, "http://example.com").to_return(:status => 200)
19
+
20
+ response = get @uri
21
+
22
+ assert_equal '200', response.code
23
+ end
24
+
25
+ end
26
+
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bing
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Adam Avilla
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: net-http-persistent
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 1
31
+ - 6
32
+ version: "1.6"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 5
44
+ segments:
45
+ - 1
46
+ - 5
47
+ - 3
48
+ version: 1.5.3
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: minitest
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: git
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: webmock
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ type: :development
92
+ version_requirements: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ name: ZenTest
95
+ prerelease: false
96
+ requirement: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ type: :development
106
+ version_requirements: *id006
107
+ - !ruby/object:Gem::Dependency
108
+ name: hoe
109
+ prerelease: false
110
+ requirement: &id007 !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ~>
114
+ - !ruby/object:Gem::Version
115
+ hash: 23
116
+ segments:
117
+ - 2
118
+ - 10
119
+ version: "2.10"
120
+ type: :development
121
+ version_requirements: *id007
122
+ description: Bing api client library that exposes all of Bing's api features.
123
+ email:
124
+ - adam@avil.la
125
+ executables: []
126
+
127
+ extensions: []
128
+
129
+ extra_rdoc_files:
130
+ - History.txt
131
+ - Manifest.txt
132
+ - README.txt
133
+ files:
134
+ - .autotest
135
+ - .gitignore
136
+ - History.txt
137
+ - Manifest.txt
138
+ - README.txt
139
+ - Rakefile
140
+ - lib/bing.rb
141
+ - lib/bing/core_ext.rb
142
+ - lib/bing/errors.rb
143
+ - lib/bing/formatting_helper.rb
144
+ - lib/bing/location.rb
145
+ - lib/bing/request.rb
146
+ - lib/bing/route.rb
147
+ - lib/bing/route/itinerary.rb
148
+ - test/helper.rb
149
+ - test/test_bing.rb
150
+ - test/test_bing_location.rb
151
+ - test/test_bing_route.rb
152
+ - test/test_bing_route_itinerary.rb
153
+ - test/test_core_ext.rb
154
+ - test/test_request.rb
155
+ - .gemtest
156
+ homepage: https://github.com/hekaldama/bing
157
+ licenses: []
158
+
159
+ post_install_message:
160
+ rdoc_options:
161
+ - --main
162
+ - README.txt
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ hash: 3
171
+ segments:
172
+ - 0
173
+ version: "0"
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ none: false
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ hash: 3
180
+ segments:
181
+ - 0
182
+ version: "0"
183
+ requirements: []
184
+
185
+ rubyforge_project: bing
186
+ rubygems_version: 1.8.6
187
+ signing_key:
188
+ specification_version: 3
189
+ summary: Bing api client library that exposes all of Bing's api features.
190
+ test_files:
191
+ - test/test_bing.rb
192
+ - test/test_bing_location.rb
193
+ - test/test_core_ext.rb
194
+ - test/test_bing_route.rb
195
+ - test/test_bing_route_itinerary.rb
196
+ - test/test_request.rb