Class: Shipay::TokenManager

Inherits:
Object show all
Defined in:
lib/shipay/token_manager.rb

Overview

Class to manage Tokens with singleton structure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTokenManager

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

#authenticatorsObject (readonly)

Returns the value of attribute authenticators.



7
8
9
# File 'lib/shipay/token_manager.rb', line 7

def authenticators
  @authenticators
end

#mutexObject (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

Examples:

Ads a new client to be used in calls to Shipay Api

Shipay::TokenManager.add_client Client.new(client_id: <CLIENT_KEY>, key: :<CLIENT_ALIAS>)

Parameters:

  • client (Shipay::Client)

    Client instance to be registered in TokenManager

Returns:

  • (Array)

    Authenticators array

Raises:



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

Parameters:

  • key (Symbol) (defaults to: Shipay.default_client_key)

    Client Key to be found in Authenticators Array ( Defaults to Shipay.default_client_key)

Returns:



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

Parameters:

  • key (Symbol) (defaults to: Shipay.default_client_key)

    Client Key to be found in Authenticators Array ( Defaults to Shipay.default_client_key)

Returns:

  • (Symbol)

    Return the cleint type ( :pdv or :e_commerce) ( Defaults to :pdv if not found)



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

.instanceObject



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

Parameters:

  • key (Symbol) (defaults to: Shipay.default_client_key)

    Client Key to be found in Authenticators Array

Returns:

  • (String)

    Auth token

Raises:



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

Parameters:

  • tokens (Array)

    Array of tokens to be registered as clients

Returns:

  • (Array)

    Authenticators array



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