access_token_wrapper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +1 -0
- data/access_token_wrapper.gemspec +23 -0
- data/lib/access_token_wrapper.rb +29 -0
- data/lib/access_token_wrapper/version.rb +3 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fa3e4dc49fcef736188a2ac6c92c62f9be9aa49c
|
4
|
+
data.tar.gz: b2d6e2a5ca52da58f59c31f2d0d76ba72c54f2f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a24c1e9508895fed609af3208451bbb14436968205bfc47fa26072eb950fd7e5e142914364b7b4e189ea9cb8a8ab20ffcbbf0948651a9ef0858d6c3f77ea6166
|
7
|
+
data.tar.gz: 4888d4057b10b4981e5860ee1042bf396c58f2fb6f4ad122b81387e2431a89e0833ca13778d64d8c88987055fc5cf88bd9bdd9593bfa3d6e085058f37c69f1a4
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TradeGecko Pte Ltd
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# AccessTokenWrapper
|
2
|
+
|
3
|
+
Provides a wrapper for an OAuth2::Token to automatically refresh the expiry token when required.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'access_token_wrapper'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install access_token_wrapper
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
def access_token
|
23
|
+
@access_token ||= begin
|
24
|
+
token = OAuth2::AccessToken.new(oauth_client, @user.access_token,
|
25
|
+
refresh_token: @user.refresh_token,
|
26
|
+
expires_at: @user.expires_at
|
27
|
+
)
|
28
|
+
AccessTokenWrapper::Base.new(token) do |new_token, exception|
|
29
|
+
update_user_from_access_token(new_token)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def oauth_client
|
35
|
+
@oauth_client ||= OAuth2::Client.new(ENV["OAUTH_ID"], ENV["OAUTH_SECRET"], site: "https://api.tradegecko.com")
|
36
|
+
end
|
37
|
+
|
38
|
+
def update_user_from_access_token(new_token)
|
39
|
+
@user.access_token = new_token.token
|
40
|
+
@user.refresh_token = new_token.refresh_token
|
41
|
+
@user.expires_at = new_token.expires_at
|
42
|
+
@user.save
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## Note
|
47
|
+
The `AccessTokenWrapper#token` is replaced with `AccessTokenWrapper#raw_token`
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'access_token_wrapper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "access_token_wrapper"
|
8
|
+
spec.version = AccessTokenWrapper::VERSION
|
9
|
+
spec.authors = ["Bradley Priest"]
|
10
|
+
spec.email = ["bradley@tradegecko.com"]
|
11
|
+
spec.description = %q{Wrapper for OAuth2::Token to automatically refresh the expiry token when expired.}
|
12
|
+
spec.summary = %q{Wrapper for OAuth2::Token to automatically refresh the expiry token when expired.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: access_token_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bradley Priest
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Wrapper for OAuth2::Token to automatically refresh the expiry token when
|
42
|
+
expired.
|
43
|
+
email:
|
44
|
+
- bradley@tradegecko.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- access_token_wrapper.gemspec
|
54
|
+
- lib/access_token_wrapper.rb
|
55
|
+
- lib/access_token_wrapper/version.rb
|
56
|
+
homepage: ''
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.5.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Wrapper for OAuth2::Token to automatically refresh the expiry token when
|
80
|
+
expired.
|
81
|
+
test_files: []
|