google-directions 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e19eb410507ff5019a50d5a49df9aeba361bbdee
4
- data.tar.gz: 3a71fbdb9363aee2db70b8fef3811af691c9618a
3
+ metadata.gz: 18eefda36b4175718d1978a0fe2e8da85ae7e2d3
4
+ data.tar.gz: c8cd1c3f39ac23a0e1df27166481538288a887c4
5
5
  SHA512:
6
- metadata.gz: 13f9e555b93df9cc2b621600facf2eb8c711d7b89ad14603e4280fb0d8084a4531db04d6cefb2c4065bcf41cf694d166830b43f31050c09726becac734874062
7
- data.tar.gz: c418df9bf808f37818d77da127334d901d17d970aecae6138ae5fa3cbae2c4314bc56edd234b93bdef70b9e450dd71998cb3ad534b14441d62510ac1aa15c931
6
+ metadata.gz: 38936c2b87b7c041f1166b42685ef48544d443e2aa855c29e4c23a2a509a2c1de9ad70861961784a3b4f1f63f0a88313ead5b4a6b8cd149185777d1d291fde48
7
+ data.tar.gz: 0bf13f927f97ea077b349b4e6233b1abd96a96236ee752c77b963b7dc6e54ce98ba95ca580fa89a7df4190e7ba751e8f3a52fbeccc2b561c79f0df9a6fa3f47c
@@ -16,6 +16,7 @@ module Google
16
16
 
17
17
  autoload :Config , 'google/directions/config'
18
18
  autoload :Error , 'google/directions/error'
19
+ autoload :Encoder , 'google/directions/encoder'
19
20
  autoload :Request , 'google/directions/request'
20
21
  autoload :Version , 'google/directions/version'
21
22
  end
@@ -3,7 +3,8 @@ module Google
3
3
  class Config
4
4
  include Singleton
5
5
 
6
- attr_accessor :client_id, :private_key, :timeout, :connect_timeout
6
+ attr_accessor :client_id, :private_key, :sign,
7
+ :timeout, :connect_timeout
7
8
 
8
9
  def timeout
9
10
  @timeout || 10
@@ -12,6 +13,10 @@ module Google
12
13
  def connect_timeout
13
14
  @connect_timeout || 5
14
15
  end
16
+
17
+ def sign
18
+ @sign || false
19
+ end
15
20
  end
16
21
  end
17
22
  end
@@ -0,0 +1,24 @@
1
+ require 'base64'
2
+ require 'digest/sha1'
3
+
4
+ module Google
5
+ module Directions
6
+ class Encoder
7
+ attr_reader :uri_with_params, :private_key
8
+
9
+ def initialize(uri_with_params, private_key)
10
+ @uri_with_params = uri_with_params
11
+ @private_key = private_key
12
+ end
13
+
14
+ def encode
15
+ binary_key = Base64.decode64(private_key.tr('-_','+/'))
16
+ digest = OpenSSL::Digest.new('sha1')
17
+ signature = OpenSSL::HMAC.digest(digest, binary_key, uri_with_params)
18
+ signature = Base64.encode64(signature.tr('+/','-_')).tr("\n", '')
19
+
20
+ "#{uri_with_params}&signature=#{signature}"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,7 +1,7 @@
1
1
  module Google
2
2
  module Directions
3
3
  class Request
4
- BASE_URL = 'http://maps.googleapis.com'
4
+ BASE_URL = 'maps.googleapis.com'
5
5
  GET_PATH = '/maps/api/directions/json'
6
6
 
7
7
  DRIVING_MODE = 'driving'
@@ -9,16 +9,18 @@ module Google
9
9
  attr_reader :params, :response
10
10
 
11
11
  def initialize
12
- session.base_url = BASE_URL
12
+ session.base_url = ((sign? || private_key) ? 'https://' : 'http://') + BASE_URL
13
13
  session.timeout = Google::Directions.config.timeout
14
14
  session.connect_timeout = Google::Directions.config.connect_timeout
15
15
  end
16
16
 
17
17
  def get(params)
18
18
  @params = params.with_indifferent_access
19
- @response = session.get GET_PATH + '?' + parse_params.to_query
20
19
 
21
- ::JSON.parse response.body
20
+ url = sign_url_if_needed(GET_PATH + '?' + parse_params.to_query)
21
+ @response = session.get url
22
+
23
+ response ? ::JSON.parse(response.body) : nil
22
24
  end
23
25
 
24
26
  private
@@ -33,9 +35,15 @@ module Google
33
35
  parsed_params[:waypoints] = parse_waypoints if params[:waypoints]
34
36
  parsed_params[:mode] = params[:mode] || DRIVING_MODE
35
37
 
38
+ set_auth_params!(parsed_params)
39
+
36
40
  parsed_params
37
41
  end
38
42
 
43
+ def sign_url_if_needed(uri_with_params)
44
+ sign? ? Encoder.new(uri_with_params, private_key).encode : uri_with_params
45
+ end
46
+
39
47
  def parse_waypoints
40
48
  optimize = !!params[:optimize] || false
41
49
  waypoints = params[:waypoints].join('|')
@@ -43,6 +51,17 @@ module Google
43
51
  "optimize:#{optimize}|#{waypoints}"
44
52
  end
45
53
 
54
+ def set_auth_params!(parsed_params)
55
+ if sign?
56
+ missing('client_id') unless client_id
57
+ missing('private_key') unless private_key
58
+
59
+ parsed_params[:client] = client_id
60
+ elsif private_key
61
+ parsed_params[:key] = private_key
62
+ end
63
+ end
64
+
46
65
  def session
47
66
  @session ||= Patron::Session.new
48
67
  end
@@ -50,6 +69,18 @@ module Google
50
69
  def missing(name)
51
70
  raise Error, "Missing parameter: #{name}"
52
71
  end
72
+
73
+ def private_key
74
+ Google::Directions.config.private_key
75
+ end
76
+
77
+ def client_id
78
+ Google::Directions.config.client_id
79
+ end
80
+
81
+ def sign?
82
+ Google::Directions.config.sign
83
+ end
53
84
  end
54
85
  end
55
86
  end
@@ -1,5 +1,5 @@
1
1
  module Google
2
2
  module Directions
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Google::Directions::Encoder do
4
+ describe '#encode' do
5
+ let(:uri_with_params) { '/maps/api/geocode/json?address=Florianopolis&client=123' }
6
+ let(:private_key) { 'vNIXE0xscrmjlyV-12Nj_BvUPaw=' }
7
+
8
+ subject { described_class.new(uri_with_params, private_key).encode }
9
+
10
+ it 'creates signature from path and query' do
11
+ expect(subject).to eq("#{uri_with_params}&signature=VqUOoDBw//HLG5g76wWQLSg0/mw=")
12
+ end
13
+ end
14
+ end
@@ -17,6 +17,61 @@ describe Google::Directions::Request do
17
17
  end
18
18
  end
19
19
 
20
+ context 'when private key is set' do
21
+ let(:url_with_key) { /^\S+key=\S+$/ }
22
+
23
+ before do
24
+ allow(Google::Directions.config).to receive(:private_key).and_return('MY_PRIVATE_KEY')
25
+ end
26
+
27
+ it 'should use key over HTTPS in request' do
28
+ expect_any_instance_of(Patron::Session).to receive(:base_url=).with(/https/)
29
+ expect_any_instance_of(Patron::Session).to receive(:get).with(url_with_key).once
30
+
31
+ subject.get(params)
32
+ end
33
+ end
34
+
35
+ context 'when is set to sign request' do
36
+ let(:client_id) { 'my_client_id' }
37
+ let(:private_key) { 'vNIXE0xscrmjlyV-12Nj_BvUPaw=' }
38
+ let(:url_with_key) { /^\S+client=\S+\&signature=\S+$/ }
39
+
40
+ before do
41
+ allow(Google::Directions.config).to receive(:sign).and_return(true)
42
+ end
43
+
44
+ context 'when client_id and private_key are present' do
45
+ before do
46
+ allow(Google::Directions.config).to receive(:client_id).and_return(client_id)
47
+ allow(Google::Directions.config).to receive(:private_key).and_return(private_key)
48
+ end
49
+
50
+ it 'should add signature to query string over HTTPS' do
51
+ expect_any_instance_of(Patron::Session).to receive(:base_url=).with(/https/)
52
+ expect_any_instance_of(Patron::Session).to receive(:get).with(url_with_key).once
53
+
54
+ subject.get(params)
55
+ end
56
+ end
57
+
58
+ context 'when client_id is missing' do
59
+ it 'should raise an exception' do
60
+ expect{subject.get(params)}.to raise_error Google::Directions::Error, 'Missing parameter: client_id'
61
+ end
62
+ end
63
+
64
+ context 'when private_key is missing' do
65
+ before do
66
+ allow(Google::Directions.config).to receive(:client_id).and_return(client_id)
67
+ end
68
+
69
+ it 'should raise an exception' do
70
+ expect{subject.get(params)}.to raise_error Google::Directions::Error, 'Missing parameter: private_key'
71
+ end
72
+ end
73
+ end
74
+
20
75
  context 'when origin is missing' do
21
76
  let(:params) { { destination: 'Rua Frei Galvão, 69, São Paulo' } }
22
77
 
data/wercker.yml CHANGED
@@ -1,13 +1,21 @@
1
- box: nafu/ubuntu12.04-ruby2.1.1@1.0.1
1
+ box: wercker/rvm
2
2
  build:
3
3
  steps:
4
+ - script:
5
+ name: install ruby
6
+ code: |
7
+ rvm get latest
8
+ rvm reload
9
+ rvm install ruby-2.2.5
10
+ rvm --default use 2.2.5
11
+ gem install bundler
4
12
  - bundle-install
5
13
  - script:
6
14
  name: echo ruby information
7
15
  code: |
8
16
  echo "RUBY version $(ruby --version) running"
9
17
  echo "FROM location $(which ruby)"
10
- echo -p "Gem list: $(gem list)"
18
+ echo -p "Gem list: $(gem list)"
11
19
  - script:
12
20
  name: rspec
13
21
  code: bundle exec rspec
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-directions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parafuzo Core Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-29 00:00:00.000000000 Z
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,12 +115,14 @@ files:
115
115
  - google-directions.gemspec
116
116
  - lib/google/directions.rb
117
117
  - lib/google/directions/config.rb
118
+ - lib/google/directions/encoder.rb
118
119
  - lib/google/directions/error.rb
119
120
  - lib/google/directions/request.rb
120
121
  - lib/google/directions/version.rb
121
122
  - spec/fixtures/vcr_cassettes/get_directions_simple.yml
122
123
  - spec/fixtures/vcr_cassettes/get_directions_with_waypoints.yml
123
124
  - spec/fixtures/vcr_cassettes/get_geocoded_directions_with_waypoints.yml
125
+ - spec/google/directions/encoder_spec.rb
124
126
  - spec/google/directions/request_spec.rb
125
127
  - spec/spec_helper.rb
126
128
  - wercker.yml
@@ -152,5 +154,6 @@ test_files:
152
154
  - spec/fixtures/vcr_cassettes/get_directions_simple.yml
153
155
  - spec/fixtures/vcr_cassettes/get_directions_with_waypoints.yml
154
156
  - spec/fixtures/vcr_cassettes/get_geocoded_directions_with_waypoints.yml
157
+ - spec/google/directions/encoder_spec.rb
155
158
  - spec/google/directions/request_spec.rb
156
159
  - spec/spec_helper.rb