Class: Shipay::TokenManager
Overview
Class to manage Tokens with singleton structure
Instance Attribute Summary collapse
-
#authenticators ⇒ Object
readonly
Returns the value of attribute authenticators.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Class Method Summary collapse
-
.add_client(client) ⇒ Array
Registers a new client to be used.
-
.client_for(key = Shipay.default_client_key) ⇒ Shipay::Client
Find a Client for a specific Key.
-
.client_type_for(key = Shipay.default_client_key) ⇒ Symbol
Find a Client Type for a specific Key.
- .instance ⇒ Object
-
.token_for(key = Shipay.default_client_key) ⇒ String
Find a token for a specific Client Key.
Instance Method Summary collapse
-
#initialize ⇒ TokenManager
constructor
Initializes TokenManager.
-
#setup_autenticators(tokens) ⇒ Array
Sets authenticators based on tokens passed by constructor.
Constructor Details
#initialize ⇒ TokenManager
Initializes TokenManager
This class builds authentication array with a mutex to share tokens
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/shipay/token_manager.rb', line 15 def initialize() tokens = nil if Shipay.credentials case Shipay.credentials when Array tokens = Shipay.credentials when Hash tokens = [ShiShipay.credentials] end else tokens = [{ secret_key: Shipay.secret_key, access_key: Shipay.access_key, client_id: Shipay.client_id, key: :default, default: true }] end @mutex = Mutex.new @authenticators = nil setup_autenticators tokens end |
Instance Attribute Details
#authenticators ⇒ Object (readonly)
Returns the value of attribute authenticators.
7 8 9 |
# File 'lib/shipay/token_manager.rb', line 7 def authenticators @authenticators end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
7 8 9 |
# File 'lib/shipay/token_manager.rb', line 7 def mutex @mutex end |
Class Method Details
.add_client(client) ⇒ Array
Registers a new client to be used
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/shipay/token_manager.rb', line 85 def self.add_client client self.instance unless @instance client = (client.is_a? Shipay::Client)? client : Shipay::Client.new(**client) raise ParamError.new("Client key '#{client.key}' already exists", 'Key', '') if self.client_for client.key @instance.mutex.synchronize do @instance.authenticators << Authenticator.new(client) end end |
.client_for(key = Shipay.default_client_key) ⇒ Shipay::Client
Find a Client for a specific Key
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/shipay/token_manager.rb', line 105 def self.client_for(key = Shipay.default_client_key) k = Shipay::Util.to_sym(key) self.instance unless @instance return nil unless @instance.authenticators.present? @instance.mutex.synchronize do auth = @instance.authenticators.find { |obj| obj.key == k} auth&.client end end |
.client_type_for(key = Shipay.default_client_key) ⇒ Symbol
Find a Client Type for a specific Key
123 124 125 |
# File 'lib/shipay/token_manager.rb', line 123 def self.client_type_for key = Shipay.default_client_key client_for(key)&.type || :pdv end |
.instance ⇒ Object
127 128 129 130 131 |
# File 'lib/shipay/token_manager.rb', line 127 def self.instance return @instance if @instance @instance = TokenManager.new end |
.token_for(key = Shipay.default_client_key) ⇒ String
Find a token for a specific Client Key
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/shipay/token_manager.rb', line 64 def self.token_for(key = Shipay.default_client_key) self.instance unless @instance k = Shipay::Util.to_sym(key) raise MissingCredentialsError.new("Missing credentials for key: '#{key}'") unless @instance.authenticators @instance.mutex.synchronize do auth = @instance.authenticators.find { |obj| obj.key == k} raise MissingCredentialsError.new("Missing credentials for key: '#{key}'") if auth.blank? auth.token end end |
Instance Method Details
#setup_autenticators(tokens) ⇒ Array
Sets authenticators based on tokens passed by constructor
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/shipay/token_manager.rb', line 46 def setup_autenticators tokens return @authenticators if @authenticators tokens = tokens.map{|t| Shipay::Client.new(**t)} @mutex.synchronize do @authenticators = [] tokens.each do |client| @authenticators << Authenticator.new(client) end end end |