Class: MangoApi::FileStorage
- Inherits:
-
Object
- Object
- MangoApi::FileStorage
- Defined in:
- lib/mangopay/api/auth_token_manager.rb
Overview
Stores client-specific OAuth tokens in temporary files in the user-specified directory.
Instance Method Summary collapse
-
#ensure_folder_exists(folder) ⇒ Object
Creates a folder at the given path if none exists.
-
#file_path(client_id) ⇒ Object
Generates the file path to a certain client’s OAuth token file.
-
#initialize ⇒ FileStorage
constructor
A new instance of FileStorage.
-
#retrieve_for(client_id) ⇒ Object
Retrieves a client’s OAuth token from its storage file.
-
#store_for(client_id, token) ⇒ Object
Stores client-specific OAuth token in its own file.
Constructor Details
#initialize ⇒ FileStorage
Returns a new instance of FileStorage.
123 124 125 126 |
# 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 |
Instance Method Details
#ensure_folder_exists(folder) ⇒ Object
Creates a folder at the given path if none exists.
169 170 171 |
# 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) ⇒ Object
Generates the file path to a certain client’s OAuth token file.
146 147 148 |
# 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) ⇒ Object
Retrieves a client’s OAuth token from its storage file.
noinspection RubyResolve
133 134 135 136 137 138 139 140 141 |
# 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) ⇒ Object
Stores client-specific OAuth token in its own file.
noinspection RubyResolve
156 157 158 159 160 161 162 163 164 |
# 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 |