gistance 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be45454a9c540c04313d8a0b0c98859a193bc73e
4
- data.tar.gz: 45714fb08ea043676b2b2ac69239f99095de65ee
3
+ metadata.gz: 0bb92df92ed4fea89666a41d5c2c2f022ffe7c38
4
+ data.tar.gz: d291398fbabe94d12d404dbf431a80ca4bb7070f
5
5
  SHA512:
6
- metadata.gz: 3ad1aff0b8d2b1695aab6e9460512c83aea526eb298a6f1a9a2ec8ce10c65f9ba7beebb1fc4280570c86582207d3c94fa292d2a27fba16693e4146f02e088a86
7
- data.tar.gz: 8af510bab65b26500fd37630cca0f145a11ff369a3f0dac7b04cfb2fe8b982b20a4f372faaca815ebab520e4fb29779feaf11366c998634d40af6e3aa942dfd5
6
+ metadata.gz: 45793bb6bcb381a7ef454c1173d09272c4886a6b6d59e5354d4b99814d994fc0b7ac95026c88823897cd0e6f04e51fff00b14bd06a72ed4e85d4c0548126fa1d
7
+ data.tar.gz: 65720f0ad44376ed8813d97d6bfbbebc65008c2db59e21b611adb997110f5360a28e511d7d95c538c7d7d561a4027e85d090f0877ba7ad36352ce47293334e28
data/README.md CHANGED
@@ -14,6 +14,7 @@ Simple Ruby wrapper for the [Google Distance Matrix API](https://developers.goog
14
14
  - 1.9.3
15
15
  - 2.0.0
16
16
  - 2.1.1
17
+ - 2.1.2
17
18
 
18
19
  ## Installation
19
20
 
@@ -53,9 +54,19 @@ The `unit`, `language` and `sensor` parameters can be set globally or can be pro
53
54
 
54
55
  ## Authentication
55
56
 
56
- Gistance only supports authentication via an API key.
57
+ Gistance supports authentication via a [simple API key](https://developers.google.com/maps/documentation/business/webservices/auth) or a [Google Maps for Business signature](https://developers.google.com/maps/documentation/business/webservices/auth).
57
58
 
58
- You can request one following these [steps](https://developers.google.com/maps/documentation/distancematrix/#api_key).
59
+ ```ruby
60
+ # Google Maps API for Business authentication
61
+ Gistance.configure do |c|
62
+ c.api_key = 'YOUR_API_KEY'
63
+ c.business = {
64
+ client_id: 'gme-fuubar',
65
+ channel: 'test' # optional
66
+ }
67
+ # etc...
68
+ end
69
+ ```
59
70
 
60
71
  ## Usage
61
72
 
@@ -7,7 +7,8 @@ module Gistance
7
7
  :api_key,
8
8
  :language,
9
9
  :unit,
10
- :sensor
10
+ :sensor,
11
+ :business
11
12
  ].freeze
12
13
 
13
14
  # Default Google Distance Matrix API endpoint
@@ -37,6 +38,7 @@ module Gistance
37
38
  self.language = 'en'
38
39
  self.unit = 'metric'
39
40
  self.sensor = false
41
+ self.business = nil
40
42
  end
41
43
 
42
44
  # Reset configuration options to default values
@@ -1,3 +1,6 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
1
4
  module Gistance
2
5
  # Methods for HTTP requests
3
6
  module Request
@@ -14,16 +17,48 @@ module Gistance
14
17
 
15
18
  def request(method, options)
16
19
  response = connection.send(method) do |request|
17
- request.params[:key] = self.api_key if self.api_key
18
-
19
20
  [:language, :unit, :sensor].each do |option|
20
21
  request.params[option] = options.delete(option) || self.public_send(option)
21
22
  end
22
23
 
23
24
  request.url(self.api_endpoint, options)
25
+
26
+ assign_api_key_to_request(request)
24
27
  end
25
28
 
26
29
  response.body
27
30
  end
31
+
32
+ def assign_api_key_to_request(request)
33
+ if self.business
34
+ request.params[:client] = business[:client_id]
35
+ request.params[:channel] = business[:channel] if business[:channel]
36
+ request.params[:signature] = generate_signature_for_request(request)
37
+ else
38
+ request.params[:key] = self.api_key if self.api_key
39
+ end
40
+ end
41
+
42
+ def generate_signature_for_request(request)
43
+ digest = OpenSSL::Digest.new('sha1')
44
+ raw_private_key = url_safe_base64_decode(self.api_key)
45
+ normalized_path = normalize_request_path(request)
46
+
47
+ raw_signature = OpenSSL::HMAC.digest(digest, raw_private_key, normalized_path)
48
+
49
+ url_safe_base64_encode(raw_signature)
50
+ end
51
+
52
+ def url_safe_base64_decode(base64_string)
53
+ Base64.decode64(base64_string.tr('-_', '+/'))
54
+ end
55
+
56
+ def url_safe_base64_encode(raw)
57
+ Base64.encode64(raw).tr('+/', '-_').strip
58
+ end
59
+
60
+ def normalize_request_path(request)
61
+ request.to_env(connection)[:url].to_s.gsub('https://maps.googleapis.com', '')
62
+ end
28
63
  end
29
64
  end
@@ -1,5 +1,5 @@
1
1
  module Gistance
2
2
  # Current version
3
3
  # @return [String]
4
- VERSION = '1.0.0'.freeze
4
+ VERSION = '1.1.0'.freeze
5
5
  end
@@ -79,5 +79,19 @@ describe Gistance::Client do
79
79
  expect(client.sensor).to be_true
80
80
  end
81
81
  end
82
+
83
+ describe 'business' do
84
+ it 'sets businnes' do
85
+ client = Gistance::Client.new
86
+
87
+ expect(client.business).to be_nil
88
+ end
89
+
90
+ it 'overrides module configuration' do
91
+ client = Gistance::Client.new(business: { client: 'gme-fuubar' })
92
+
93
+ expect(client.business[:client]).to eql('gme-fuubar')
94
+ end
95
+ end
82
96
  end
83
97
  end
@@ -8,4 +8,28 @@ describe Gistance::Request do
8
8
  it_behaves_like 'an error', 'UNKNOWN_ERROR'
9
9
  it_behaves_like 'an error', 'NOT_FOUND'
10
10
  it_behaves_like 'an error', 'ZERO_RESULTS'
11
+
12
+ context 'given a google business request' do
13
+ let(:business_client) do
14
+ Gistance.new(api_key: '4242', business: { client_id: 'gme-fuubar', channel: 'test' })
15
+ end
16
+
17
+ it 'generates a valid signature params' do
18
+ VCR.turn_off!
19
+
20
+ stub_request(:get, /.*maps.*/).to_return(body: { status: 'ok' }.to_json)
21
+
22
+ business_client.get
23
+
24
+ assert_requested :get, 'https://maps.googleapis.com/maps/api/distancematrix/json?'\
25
+ 'language=en&'\
26
+ 'unit=metric&'\
27
+ 'sensor=false&'\
28
+ 'client=gme-fuubar&'\
29
+ 'channel=test&'\
30
+ 'signature=wA6neJ6IkHTs6BxFt6ves7hxsZI%3D'
31
+
32
+ VCR.turn_on!
33
+ end
34
+ end
11
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gistance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aylic Petit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday