Class: SchwabRb::Auth::TokenManager
- Inherits:
-
Object
- Object
- SchwabRb::Auth::TokenManager
- Defined in:
- lib/schwab_rb/auth/token_manager.rb
Instance Attribute Summary collapse
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#token_path ⇒ Object
readonly
Returns the value of attribute token_path.
Class Method Summary collapse
- .from_file(token_path) ⇒ Object
- .from_oauth2_token(oauth2_token, timestamp, token_path: SchwabRb::Constants::DEFAULT_TOKEN_PATH) ⇒ Object
Instance Method Summary collapse
-
#initialize(token, timestamp, token_path: SchwabRb::Constants::DEFAULT_TOKEN_PATH) ⇒ TokenManager
constructor
A new instance of TokenManager.
- #refresh_token(client) ⇒ Object
- #to_file ⇒ Object
- #to_h ⇒ Object
- #to_json(*_args) ⇒ Object
- #token_age ⇒ Object
Constructor Details
#initialize(token, timestamp, token_path: SchwabRb::Constants::DEFAULT_TOKEN_PATH) ⇒ TokenManager
Returns a new instance of TokenManager.
43 44 45 46 47 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 43 def initialize(token, , token_path: SchwabRb::Constants::DEFAULT_TOKEN_PATH) @token = token @timestamp = @token_path = SchwabRb::PathSupport.(token_path) end |
Instance Attribute Details
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
49 50 51 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 49 def @timestamp end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
49 50 51 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 49 def token @token end |
#token_path ⇒ Object (readonly)
Returns the value of attribute token_path.
49 50 51 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 49 def token_path @token_path end |
Class Method Details
.from_file(token_path) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 12 def from_file(token_path) token_path = SchwabRb::PathSupport.(token_path) token_data = JSON.parse(File.read(token_path)) token = SchwabRb::Auth::Token.new( token: token_data["token"]["access_token"], expires_in: token_data["token"]["expires_in"], token_type: token_data["token"]["token_type"], scope: token_data["token"]["scope"], refresh_token: token_data["token"]["refresh_token"], id_token: token_data["token"]["id_token"], expires_at: token_data["token"]["expires_at"] ) TokenManager.new(token, token_data["timestamp"], token_path: token_path) end |
.from_oauth2_token(oauth2_token, timestamp, token_path: SchwabRb::Constants::DEFAULT_TOKEN_PATH) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 28 def from_oauth2_token(oauth2_token, , token_path: SchwabRb::Constants::DEFAULT_TOKEN_PATH) token = SchwabRb::Auth::Token.new( token: oauth2_token.token, expires_in: oauth2_token.expires_in, token_type: oauth2_token.params["token_type"] || "Bearer", scope: oauth2_token.params["scope"], refresh_token: oauth2_token.refresh_token, id_token: oauth2_token.params["id_token"], expires_at: oauth2_token.expires_at ) TokenManager.new(token, , token_path: token_path) end |
Instance Method Details
#refresh_token(client) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 51 def refresh_token(client) new_token = client.session.refresh! @token = SchwabRb::Auth::Token.new( token: new_token.token, expires_in: new_token.expires_in, token_type: new_token.params["token_type"] || "Bearer", scope: new_token.params["scope"], refresh_token: new_token.refresh_token, id_token: new_token.params["id_token"], expires_at: new_token.expires_at ) @timestamp = Time.now.to_i to_file oauth = OAuth2::Client.new( client.api_key, client.app_secret, site: SchwabRb::Constants::SCHWAB_BASE_URL, token_url: "/v1/oauth/token" ) OAuth2::AccessToken.new( oauth, token.token, refresh_token: token.refresh_token, expires_at: token.expires_at ) end |
#to_file ⇒ Object
82 83 84 85 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 82 def to_file SchwabRb::PathSupport.ensure_parent_directory(token_path) File.write(token_path, to_json) end |
#to_h ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 91 def to_h { timestamp: , token: { expires_in: token.expires_in, token_type: token.token_type, scope: token.scope, refresh_token: token.refresh_token, access_token: token.token, id_token: token.id_token, expires_at: token.expires_at } } end |
#to_json(*_args) ⇒ Object
106 107 108 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 106 def to_json(*_args) to_h.to_json end |
#token_age ⇒ Object
87 88 89 |
# File 'lib/schwab_rb/auth/token_manager.rb', line 87 def token_age Time.now.to_i - end |