shutl_auth 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,24 +2,27 @@
2
2
  #authenticating requests
3
3
  module Shutl
4
4
  module Auth
5
- module Session
6
- def session
7
- @session ||= {}
5
+ class Cache
6
+ def initialize
7
+ @cache = {}
8
+ end
9
+
10
+ def read(key)
11
+ @cache[key]
12
+ end
13
+
14
+ def write(key, value)
15
+ @cache[key] = value
8
16
  end
9
17
  end
10
18
 
11
19
  module AuthenticatedRequest
12
20
  def self.included base
13
- unless base.instance_methods.include? :session
14
- base.class_eval do
15
- include Shutl::Auth::Session
16
- end
17
- end
21
+
18
22
  end
19
23
 
20
24
  def request_access_token
21
- Shutl::Auth.logger.debug "request_access_token: in session? #{!!session[:access_token]}"
22
- return read_token if read_token
25
+ Shutl::Auth.logger.debug "request_access_token: cached? #{!!read_token}"
23
26
 
24
27
  Shutl::Auth.logger.debug "requesting new access token"
25
28
  Shutl::Auth.access_token!
@@ -42,13 +45,25 @@ module Shutl
42
45
  end
43
46
 
44
47
  def read_token
45
- Shutl::Auth.logger.debug "access token #{session[:access_token]}"
46
- session[:access_token]
48
+ cache.read(:access_token)
47
49
  end
48
50
 
49
51
  def set_token(token)
50
- Shutl::Auth.logger.debug "setting access token #{token}"
51
- session[:access_token] = token
52
+ cache.write(:access_token, token)
53
+ end
54
+
55
+ def cache
56
+ @cache ||= build_cache
57
+ end
58
+
59
+ private
60
+
61
+ def build_cache
62
+ if Kernel.const_defined?(:Rails)
63
+ Rails.cache
64
+ else
65
+ Cache.new
66
+ end
52
67
  end
53
68
  end
54
69
  end
@@ -1,5 +1,5 @@
1
1
  module Shutl
2
2
  module Auth
3
- VERSION = "0.8.2"
3
+ VERSION = "0.8.3"
4
4
  end
5
5
  end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ module Shutl
4
+ class UnauthorizedAccess < StandardError ; end
5
+ end
6
+ describe Shutl::Auth::AuthenticatedRequest do
7
+
8
+ class ToTestAuthenticatedRequest
9
+ include Shutl::Auth::AuthenticatedRequest
10
+ end
11
+
12
+ subject { ToTestAuthenticatedRequest.new }
13
+
14
+ let(:token) { 'abcd' }
15
+ let(:spare_token) { '1234' }
16
+
17
+ before do
18
+ Shutl::Auth.stub(:access_token!).and_return(token, spare_token)
19
+ end
20
+
21
+ describe 'access_token' do
22
+ it 'requests the token' do
23
+ subject.access_token.should == token
24
+ end
25
+
26
+ it 'caches the token' do
27
+ subject.access_token
28
+
29
+ subject.access_token.should == token
30
+ end
31
+ end
32
+
33
+ describe 'authenticated_request' do
34
+ it 'execute the block' do
35
+ block = -> { 'test' }
36
+
37
+ result = subject.authenticated_request &block
38
+
39
+ result.should == 'test'
40
+ end
41
+
42
+ it 'retries if the block raise an UnauthorizedAccess' do
43
+ call = 0
44
+ block = -> do
45
+ t = subject.access_token
46
+ if call == 0
47
+ call += 1
48
+ raise Shutl::UnauthorizedAccess
49
+ end
50
+ t
51
+ end
52
+
53
+ result = subject.authenticated_request &block
54
+
55
+ result.should == spare_token
56
+ end
57
+
58
+ it 'caches the token' do
59
+ block = -> { subject.access_token }
60
+
61
+ 2.times { subject.authenticated_request &block }
62
+
63
+ subject.cache.read(:access_token).should == token
64
+ end
65
+ end
66
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shutl_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -168,6 +168,7 @@ files:
168
168
  - spec/config_spec.rb
169
169
  - spec/integration/integration_spec.rb
170
170
  - spec/spec_helper.rb
171
+ - spec/unit/authenticated_request_spec.rb
171
172
  - spec/vcr/get_token.yml
172
173
  - spec/vcr/invalid_credentials.yml
173
174
  homepage: ''
@@ -184,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
184
185
  version: '0'
185
186
  segments:
186
187
  - 0
187
- hash: 1364072325301481292
188
+ hash: -3397876732357691212
188
189
  required_rubygems_version: !ruby/object:Gem::Requirement
189
190
  none: false
190
191
  requirements:
@@ -193,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
194
  version: '0'
194
195
  segments:
195
196
  - 0
196
- hash: 1364072325301481292
197
+ hash: -3397876732357691212
197
198
  requirements: []
198
199
  rubyforge_project:
199
200
  rubygems_version: 1.8.23
@@ -205,5 +206,6 @@ test_files:
205
206
  - spec/config_spec.rb
206
207
  - spec/integration/integration_spec.rb
207
208
  - spec/spec_helper.rb
209
+ - spec/unit/authenticated_request_spec.rb
208
210
  - spec/vcr/get_token.yml
209
211
  - spec/vcr/invalid_credentials.yml