Class: MangoPay::AuthorizationToken::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/mangopay/authorization_token.rb

Overview

Class Method Summary collapse

Class Method Details

.get_environment_key_for_tokenObject



33
34
35
36
37
38
# File 'lib/mangopay/authorization_token.rb', line 33

def get_environment_key_for_token
  cfg = MangoPay.configuration
  key = "#{cfg.root_url}|#{cfg.client_id}|#{cfg.client_apiKey}"
  key = Digest::MD5.hexdigest(key)
  key
end

.get_tokenObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mangopay/authorization_token.rb', line 16

def get_token
  token = storage.get
  env_key = get_environment_key_for_token
  if token.nil? || token['timestamp'].nil? || token['timestamp'] <= Time.now || token['environment_key'] != env_key
    token = MangoPay.request(:post, "/#{MangoPay.version_code}/oauth/token", {}, {}, {}, Proc.new do |req|
      cfg = MangoPay.configuration
      req.basic_auth cfg.client_id, cfg.client_apiKey
      req.body = 'grant_type=client_credentials'
      req.add_field('Content-Type', 'application/x-www-form-urlencoded')
    end)
    token['timestamp'] = Time.now + (token['expires_in'].to_i - 10)
    token['environment_key'] = env_key
    storage.store token
  end
  token
end

.storageObject



8
9
10
# File 'lib/mangopay/authorization_token.rb', line 8

def storage
  @@storage ||= StaticStorage.new
end

.storage=(storage) ⇒ Object



12
13
14
# File 'lib/mangopay/authorization_token.rb', line 12

def storage= (storage)
  @@storage = storage
end