class MangoApi::FileStorage

Stores client-specific OAuth tokens in temporary files in the user-specified directory.

Public Class Methods

new() click to toggle source
# File lib/mangopay/api/auth_token_manager.rb, line 123
def initialize
  @temp_dir = MangoPay.configuration.temp_dir
  raise 'Path to temporary folder is not defined' unless @temp_dir
end

Public Instance Methods

ensure_folder_exists(folder) click to toggle source

Creates a folder at the given path if none exists.

@param folder path at which folder should exist

# File lib/mangopay/api/auth_token_manager.rb, line 169
def ensure_folder_exists(folder)
  FileUtils.mkdir_p folder unless File.directory?(folder)
end
file_path(client_id) click to toggle source

Generates the file path to a certain client's OAuth token file.

@param client_id ID of the client whose file path to generate

# File lib/mangopay/api/auth_token_manager.rb, line 146
def file_path(client_id)
  File.join(@temp_dir, "mangopay_oauth_token_#{client_id}.tmp")
end
retrieve_for(client_id) click to toggle source

Retrieves a client's OAuth token from its storage file.

@param client_id ID of the client whose OAuth token to retrieve

noinspection RubyResolve

# File lib/mangopay/api/auth_token_manager.rb, line 133
def retrieve_for(client_id)
  path = file_path(client_id)
  return nil unless File.exist? path
  File.open(path, 'r') do |file|
    file.flock File::LOCK_SH
    oauth_data = file.read
    YAML.load oauth_data || nil
  end
end
store_for(client_id, token) click to toggle source

Stores client-specific OAuth token in its own file.

@param client_id ID of the client whose token is being stored @param token OAuth token for this client

noinspection RubyResolve

# File lib/mangopay/api/auth_token_manager.rb, line 156
def store_for(client_id, token)
  ensure_folder_exists MangoPay.configuration.temp_dir
  File.open(file_path(client_id), 'w') do |file|
    file.flock File::LOCK_EX
    file.truncate(0)
    file.rewind
    file.puts YAML.dump(token)
  end
end