routing 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ nbproject
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
@@ -19,4 +20,4 @@ test/tmp
19
20
  test/version_tmp
20
21
  tmp
21
22
  **/.DS_Store
22
- .DS_Store
23
+ .DS_Store
@@ -9,43 +9,29 @@ class Routing
9
9
  # Array of {GeoPoint}s, representing the calculated route.
10
10
  class Navteq
11
11
 
12
- ATTR_ACCESSIBLE = [ :host, :default_params ]
13
-
14
12
  def initialize(options = {})
15
- options.each do |attribute, value|
16
- send("#{attribute}=", value) if ATTR_ACCESSIBLE.include?(attribute)
17
- end
13
+ @options = {
14
+ :service_path => '/routing/6.2/calculateroute.json',
15
+ :parser => ::Routing::Parser::NavteqSimple
16
+ }.merge(options)
18
17
  end
19
18
 
20
19
  def calculate(geo_points)
21
- parse get(geo_points)
22
- end
23
-
24
- def get(geo_points)
25
- params = default_params.merge geo_points_to_params(geo_points)
26
-
27
20
  response = connection.get do |request|
28
- request.url(service_path)
29
- request.params = params
21
+ request.url(options[:service_path])
22
+ request.params = default_params.merge(geo_points_to_params(geo_points))
30
23
  end
31
-
32
- response.body
24
+ parse(response.body)
33
25
  end
34
26
 
35
- attr_writer :host
36
-
37
- def host
38
- @host
39
- end
27
+ private
40
28
 
41
29
  def parse(response)
42
- ::Routing::Parser::NavteqSimple.new(response).to_geo_points
30
+ options[:parser].new(response).to_geo_points
43
31
  end
44
32
 
45
- attr_writer :default_params
46
-
47
33
  def default_params
48
- @default_params || {
34
+ options[:default_params] || {
49
35
  departure: Time.now.utc.iso8601,
50
36
  mode0: "fastest;car",
51
37
  language: "de_DE",
@@ -54,30 +40,21 @@ class Routing
54
40
  }
55
41
  end
56
42
 
57
- protected
58
-
59
- # The path on the server to the routing service.
60
- # This is determined by the server and should not be changed.
61
- #
62
- # @returns [String]
63
- # Path to the routing service.
64
- def service_path
65
- "/routing/6.2/calculateroute.json"
66
- end
67
-
68
43
  def geo_points_to_params(geo_points)
69
44
  Hash[geo_points.each_with_index.map { |point, i| [ "waypoint#{i}", "geo!#{point.lat},#{point.lng}" ] }]
70
45
  end
71
46
 
72
47
  def connection
73
- @connection ||= Faraday.new(:url => host) do |builder|
48
+ @connection ||= Faraday.new(:url => options[:host]) do |builder|
74
49
  builder.request :url_encoded
75
- builder.response :logger
76
50
  builder.adapter :net_http
77
51
  end
78
52
  end
79
53
 
80
- end
54
+ def options
55
+ @options || {}
56
+ end
81
57
 
58
+ end
82
59
  end
83
60
  end
@@ -16,8 +16,6 @@ class Routing
16
16
  @route = response["Response"]["Route"].first
17
17
  @overall_covered_distance = 0
18
18
  @overall_relative_time = 0
19
-
20
- self
21
19
  end
22
20
 
23
21
  # Converts the server response in an Array of {GeoPoint}s
@@ -1,3 +1,3 @@
1
1
  class Routing
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,8 +2,8 @@
2
2
  require File.expand_path('../lib/routing/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Christian Bäuerlein"]
6
- gem.email = ["christian@ffwdme.com"]
5
+ gem.authors = ["Christian Bäuerlein", "Benedikt Deicke"]
6
+ gem.email = ["christian@ffwdme.com", "benedikt@synatic.net"]
7
7
  gem.summary = %q{A ruby interface for route calculation services}
8
8
  gem.description = %q{
9
9
  Provides a generic interface for routing services that can by used to calculate directions between geolocations.
@@ -2,58 +2,84 @@ require 'spec_helper'
2
2
 
3
3
  describe Routing::Adapter::Navteq do
4
4
 
5
- context 'creating a new instance' do
5
+ let(:response) { mock(:response).as_null_object }
6
+ let(:request) { mock(:request).as_null_object }
7
+ let(:connection) { mock(:connection).as_null_object }
8
+ let(:parser_class) { mock(:parser_class, :new => parser) }
9
+ let(:parser) { mock(:parser).as_null_object }
10
+
11
+ let(:options) { { :host => 'http://example.com', :parser => parser_class } }
12
+
13
+ let(:geo_points) { [stub(:lat => 1, :lng => 2), stub(:lat => 3, :lng => 4), stub(:lat => 5, :lng => 6)] }
14
+
15
+
16
+ subject { described_class.new(options) }
17
+
18
+ before do
19
+ connection.stub(:get).and_yield(request).and_return(response)
20
+ Faraday.stub(:new).and_return(connection)
21
+ end
6
22
 
23
+ after { subject.calculate(geo_points) }
24
+
25
+ context 'creating a new instance' do
7
26
  it 'can be instantiated without arguments' do
8
- expect { Routing::Adapter::Navteq.new }.to_not raise_error
27
+ expect { described_class.new }.to_not raise_error
9
28
  end
10
29
 
11
- it 'sets the host in its options' do
12
- described_class.new(:host => "new_host").host.should == "new_host"
30
+ it 'allows setting the host' do
31
+ Faraday.should_receive(:new).with(:url => 'http://other.com')
32
+ options.merge!(:host => 'http://other.com')
13
33
  end
14
34
 
15
- it 'sets the default params in its options' do
35
+ it 'allows setting the default params' do
16
36
  params = { :hello => "world" }
17
- described_class.new(:default_params => params).default_params == params
37
+ options.merge!(:default_params => params)
38
+ request.should_receive(:params=).with(hash_including(params))
18
39
  end
19
40
 
20
- it 'ignores unknown options' do
21
- expect { described_class.new(:hello => "world") }.to_not raise_error
41
+ it 'should allow setting the service path' do
42
+ options.merge!(:service_path => '/some/path.json')
43
+ request.should_receive(:url).with('/some/path.json')
22
44
  end
23
45
 
24
- end
25
-
26
- its(:service_path){ should be_a String }
27
-
28
- its(:default_params){ should be_a Hash }
46
+ it 'should use a default service path when none is given' do
47
+ request.should_receive(:url).with('/routing/6.2/calculateroute.json')
48
+ end
29
49
 
30
- describe '#connection' do
50
+ it 'should allow setting the parser' do
51
+ options.merge!(:parser => parser_class)
52
+ parser_class.should_receive(:new)
53
+ end
31
54
 
32
- it 'returns a faraday connection' do
33
- subject.send(:connection).should be_a Faraday::Connection
55
+ it 'should use a default parser when none is given' do
56
+ options.delete(:parser)
57
+ Routing::Parser::NavteqSimple.should_receive(:new).and_return(parser)
34
58
  end
35
59
 
60
+ it 'ignores unknown options' do
61
+ expect { described_class.new(:hello => "world") }.to_not raise_error
62
+ end
36
63
  end
37
64
 
38
65
  describe '#calculate' do
39
- pending
40
- end
41
-
42
- describe '#get' do
43
- pending
44
- end
45
-
46
- describe '#geo_points_to_params' do
47
-
48
- let(:geo_points) { [stub(:lat => 1, :lng => 2), stub(:lat => 3, :lng => 4), stub(:lat => 5, :lng => 6)] }
49
- let(:params){ subject.send(:geo_points_to_params, geo_points) }
66
+ it 'passes the response body to the parser' do
67
+ response.stub(:body => '...')
68
+ parser_class.should_receive(:new).with('...')
69
+ end
50
70
 
51
- it 'creates a hash with enumerated waypoint keys and the navteq special geo! syntax as values' do
52
- params['waypoint0'].should == 'geo!1,2'
53
- params['waypoint1'].should == 'geo!3,4'
54
- params['waypoint2'].should == 'geo!5,6'
71
+ it 'should return the parser\'s result' do
72
+ result = [double, double, double]
73
+ parser.stub(:to_geo_points => result)
74
+ subject.calculate(geo_points).should be(result)
55
75
  end
56
76
 
77
+ it 'should convert the geo points into a compatible format' do
78
+ request.should_receive(:params=).with hash_including({
79
+ 'waypoint0' => 'geo!1,2',
80
+ 'waypoint1' => 'geo!3,4',
81
+ 'waypoint2' => 'geo!5,6'
82
+ })
83
+ end
57
84
  end
58
-
59
85
  end
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Christian Bäuerlein
9
+ - Benedikt Deicke
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2012-05-11 00:00:00.000000000 Z
13
+ date: 2012-07-25 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: faraday
16
- requirement: &70233528536000 !ruby/object:Gem::Requirement
17
+ requirement: &70283743036520 !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ! '>='
@@ -21,10 +22,10 @@ dependencies:
21
22
  version: 0.7.0
22
23
  type: :runtime
23
24
  prerelease: false
24
- version_requirements: *70233528536000
25
+ version_requirements: *70283743036520
25
26
  - !ruby/object:Gem::Dependency
26
27
  name: json
27
- requirement: &70233528535500 !ruby/object:Gem::Requirement
28
+ requirement: &70283743036060 !ruby/object:Gem::Requirement
28
29
  none: false
29
30
  requirements:
30
31
  - - ! '>='
@@ -32,10 +33,10 @@ dependencies:
32
33
  version: '0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *70233528535500
36
+ version_requirements: *70283743036060
36
37
  - !ruby/object:Gem::Dependency
37
38
  name: rspec
38
- requirement: &70233528534920 !ruby/object:Gem::Requirement
39
+ requirement: &70283743035460 !ruby/object:Gem::Requirement
39
40
  none: false
40
41
  requirements:
41
42
  - - ! '>='
@@ -43,12 +44,13 @@ dependencies:
43
44
  version: 2.9.0
44
45
  type: :development
45
46
  prerelease: false
46
- version_requirements: *70233528534920
47
+ version_requirements: *70283743035460
47
48
  description: ! "\n Provides a generic interface for routing services that can by
48
49
  used to calculate directions between geolocations.\n Makes parsing and use-case
49
50
  specific data handling easy trough an extendable middleware stack.\n "
50
51
  email:
51
52
  - christian@ffwdme.com
53
+ - benedikt@synatic.net
52
54
  executables: []
53
55
  extensions: []
54
56
  extra_rdoc_files: []