access_token_wrapper 0.1.0 → 0.2.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: fa3e4dc49fcef736188a2ac6c92c62f9be9aa49c
4
- data.tar.gz: b2d6e2a5ca52da58f59c31f2d0d76ba72c54f2f3
3
+ metadata.gz: 97e4602f04e6cfbc043882a7aa5a0859c59b8ee6
4
+ data.tar.gz: 748826f2514ec5124d06e6888f659ac6d118e210
5
5
  SHA512:
6
- metadata.gz: a24c1e9508895fed609af3208451bbb14436968205bfc47fa26072eb950fd7e5e142914364b7b4e189ea9cb8a8ab20ffcbbf0948651a9ef0858d6c3f77ea6166
7
- data.tar.gz: 4888d4057b10b4981e5860ee1042bf396c58f2fb6f4ad122b81387e2431a89e0833ca13778d64d8c88987055fc5cf88bd9bdd9593bfa3d6e085058f37c69f1a4
6
+ metadata.gz: '0874f0e738ed1ed9cf360805caf45b97eea88e17aa5795ae4b07658a5caecfbc7bacf6dc0985281d2214982d2dfac96c1ad3c5e2c71f26c7b4319071846daab8'
7
+ data.tar.gz: 3c6472be726db31508ae06ee6e7a84d6568f9364dc54cb9103d0eae8bbe41d8b1c053705f61632bfd23981d6a846e8bffb9f530a1ad08a0bf623c7fc05016355
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## 0.2.0 (2018-01-16)
2
+ - [FEATURE] The library now checks the expires_at date of the token before attempting an API call.
3
+ - [BREAKING] The refresh callback now only optionally contains an exception, when an exception is raised, if the refresh is due to the expires_at field (which should be the majority of the time) it will be `nil`
4
+
5
+ ## 0.1.0 (2017-09-20)
6
+ - [BREAKING] `AccessTokenWrapper::Base#token` was renamed to `#raw_token`.
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ access_token_wrapper (0.2.0)
5
+ oauth2
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ faraday (0.12.2)
11
+ multipart-post (>= 1.2, < 3)
12
+ jwt (1.5.6)
13
+ multi_json (1.12.2)
14
+ multi_xml (0.6.0)
15
+ multipart-post (2.0.0)
16
+ oauth2 (1.4.0)
17
+ faraday (>= 0.8, < 0.13)
18
+ jwt (~> 1.0)
19
+ multi_json (~> 1.3)
20
+ multi_xml (~> 0.5)
21
+ rack (>= 1.2, < 3)
22
+ rack (2.0.3)
23
+ rake (12.3.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ access_token_wrapper!
30
+ bundler (~> 1.3)
31
+ rake
32
+
33
+ BUNDLED WITH
34
+ 1.16.1
data/Rakefile CHANGED
@@ -1 +1,18 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task default: [:test]
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.pattern = "test/**/*_test.rb"
9
+ end
10
+
11
+ desc "Open an irb session"
12
+ task :console do
13
+ require 'irb'
14
+ require 'irb/completion'
15
+ require 'access_token_wrapper'
16
+ ARGV.clear
17
+ IRB.start
18
+ end
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "oauth2"
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
22
23
  spec.add_development_dependency "rake"
23
24
  end
@@ -1,29 +1 @@
1
- require "access_token_wrapper/version"
2
-
3
- module AccessTokenWrapper
4
- class Base
5
- NON_ERROR_CODES = [402, 404, 422, 414, 429, 500, 503]
6
- attr_reader :raw_token
7
-
8
- def initialize(raw_token, &callback)
9
- @raw_token = raw_token
10
- @callback = callback
11
- end
12
-
13
- def method_missing(method, *args, &block)
14
- @raw_token.send(method, *args, &block)
15
- rescue OAuth2::Error => exception
16
- if NON_ERROR_CODES.include?(exception.response.status)
17
- raise exception
18
- else
19
- @raw_token = @raw_token.refresh!
20
- @callback.call(@raw_token, exception)
21
- @raw_token.send(method, *args, &block)
22
- end
23
- end
24
-
25
- def respond_to_missing?(method_name, include_private = false)
26
- @raw_token.respond_to?(method_name, include_private) || super
27
- end
28
- end
29
- end
1
+ require "access_token_wrapper/base"
@@ -0,0 +1,37 @@
1
+ module AccessTokenWrapper
2
+ class Base
3
+ NON_ERROR_CODES = [402, 404, 422, 414, 429, 500, 503]
4
+ EXPIRY_GRACE_SEC = 30
5
+ attr_reader :raw_token
6
+
7
+ def initialize(raw_token, &callback)
8
+ @raw_token = raw_token
9
+ @callback = callback
10
+ end
11
+
12
+ def method_missing(method_name, *args, &block)
13
+ refresh_token! if token_expiring?
14
+ @raw_token.send(method_name, *args, &block)
15
+ rescue OAuth2::Error => exception
16
+ if NON_ERROR_CODES.include?(exception.response.status)
17
+ raise exception
18
+ else
19
+ refresh_token!(exception)
20
+ @raw_token.send(method_name, *args, &block)
21
+ end
22
+ end
23
+
24
+ def refresh_token!(exception = nil)
25
+ @raw_token = @raw_token.refresh!
26
+ @callback.call(@raw_token, exception)
27
+ end
28
+
29
+ def token_expiring?
30
+ @raw_token.expires_at < (Time.now.to_i + EXPIRY_GRACE_SEC)
31
+ end
32
+
33
+ def respond_to_missing?(method_name, include_private = false)
34
+ @raw_token.respond_to?(method_name, include_private) || super
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module AccessTokenWrapper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/test/base_test.rb ADDED
@@ -0,0 +1,91 @@
1
+ require 'test_helper'
2
+
3
+ class AccessTokenWrapperTest < Minitest::Test
4
+ class FakeToken
5
+ attr_reader :refreshed
6
+
7
+ def initialize(options = {})
8
+ @has_run = nil
9
+ @options = options
10
+ end
11
+
12
+ def get(*)
13
+ ""
14
+ end
15
+
16
+ def expires_at
17
+ @options[:expires_at] || Time.now.to_i + 3600
18
+ end
19
+
20
+ def get_and_raise(code)
21
+ if @has_run
22
+ ""
23
+ else
24
+ @has_run = true
25
+ raise OAuth2::Error, OpenStruct.new(status: code, parsed: { 'error' => code }, body: '')
26
+ end
27
+ end
28
+
29
+ def refresh!
30
+ @refreshed = true
31
+ self
32
+ end
33
+ end
34
+
35
+ def described_class
36
+ AccessTokenWrapper::Base
37
+ end
38
+
39
+ def test_doesnt_run_block_if_no_exception
40
+ @run = false
41
+
42
+ token = described_class.new(FakeToken.new) do |new_token, exception|
43
+ @run = true
44
+ end
45
+
46
+ token.get('/')
47
+ assert !@run
48
+ assert !token.refreshed
49
+ end
50
+
51
+ def test_runs_refresh_block_if_exception
52
+ @run = false
53
+
54
+ token = described_class.new(FakeToken.new) do |new_token, exception|
55
+ @run = true
56
+ end
57
+
58
+ token.get_and_raise(401)
59
+ assert @run
60
+ assert token.refreshed
61
+ end
62
+
63
+
64
+ def test_runs_refresh_block_if_expiring
65
+ @run = false
66
+
67
+ token = described_class.new(FakeToken.new(expires_at: Time.now.to_i - 1)) do |new_token, exception|
68
+ @run = true
69
+ end
70
+
71
+ token.get('/')
72
+ assert @run
73
+ assert token.refreshed
74
+ end
75
+
76
+ def test_doesnt_run_block_if_non_auth_exception
77
+ @run = false
78
+
79
+ token = described_class.new(FakeToken.new) do |new_token, exception|
80
+ @run = true
81
+ end
82
+
83
+ begin
84
+ token.get_and_raise(429)
85
+ rescue OAuth2::Error
86
+ end
87
+
88
+ assert !@run
89
+ assert !token.refreshed
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'access_token_wrapper'
3
+ require 'oauth2'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: access_token_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bradley Priest
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-30 00:00:00.000000000 Z
11
+ date: 2018-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -46,13 +60,19 @@ executables: []
46
60
  extensions: []
47
61
  extra_rdoc_files: []
48
62
  files:
63
+ - ".gitignore"
64
+ - CHANGELOG.md
49
65
  - Gemfile
66
+ - Gemfile.lock
50
67
  - LICENSE.txt
51
68
  - README.md
52
69
  - Rakefile
53
70
  - access_token_wrapper.gemspec
54
71
  - lib/access_token_wrapper.rb
72
+ - lib/access_token_wrapper/base.rb
55
73
  - lib/access_token_wrapper/version.rb
74
+ - test/base_test.rb
75
+ - test/test_helper.rb
56
76
  homepage: ''
57
77
  licenses:
58
78
  - MIT
@@ -73,9 +93,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
93
  version: '0'
74
94
  requirements: []
75
95
  rubyforge_project:
76
- rubygems_version: 2.5.2
96
+ rubygems_version: 2.6.13
77
97
  signing_key:
78
98
  specification_version: 4
79
99
  summary: Wrapper for OAuth2::Token to automatically refresh the expiry token when
80
100
  expired.
81
- test_files: []
101
+ test_files:
102
+ - test/base_test.rb
103
+ - test/test_helper.rb