Class: CloudFS::Session
- Inherits:
-
Object
- Object
- CloudFS::Session
- Defined in:
- lib/cloudfs/session.rb
Overview
Establishes a session with the api server on behalf of an authenticated end-user
It maintains a RESTful low level api Client object that provides authenticated access to CloudFS service end user's account and is shared with file system objects linked with this session - FileSystem, Container, File, Folder, Share, Account, User
session.is_linked? #=> false
session.autheticate(username, password)
session.is_linked? #=> true
folder = session.filesystem.root.create_folder(folder_name)
folder.name = newname
folder.save
file = folder.upload(local_filepath)
session.unlink
Instance Attribute Summary collapse
-
#account ⇒ Account
readonly
RestAdapter::Errors::OperationNotAllowedError].
-
#filesystem ⇒ FileSystem
readonly
FileSystem instance linked with this session.
-
#user ⇒ User
readonly
Profile of end-user linked with this session.
Instance Method Summary collapse
-
#action_history(start_version: -10,, stop_version: nil) ⇒ Array<Hash>
Action history lists history of file, folder, and share actions.
-
#authenticate(username, password) ⇒ true
Attempts to log into the end-user's filesystem, links this session to an account.
-
#create_account(username, password, email: nil, first_name: nil, last_name: nil, log_in_to_created_user: false) ⇒ Account
Creates a new end-user account for a Paid CloudFS account.
-
#initialize(end_point, clientid, secret, **http_conf) ⇒ Session
constructor
A new instance of Session.
-
#is_linked? ⇒ Boolean
Whether current session is linked to the API server and can make authenticated requests.
-
#set_admin_credentials(admin_client_id, admin_client_secret) ⇒ Object
Set the admin credentials.
-
#unlink ⇒ true
Discards current authentication.
Constructor Details
#initialize(end_point, clientid, secret, **http_conf) ⇒ Session
Returns a new instance of Session.
87 88 89 90 91 92 93 |
# File 'lib/cloudfs/session.rb', line 87
def initialize(end_point, clientid, secret, ** http_conf)
@http_debug = http_conf[:http_debug]
@rest_adapter = RestAdapter.new(clientid, secret, end_point, ** http_conf)
@unlinked = false
@admin_credentials = {}
@admin_credentials[:host] = end_point ? end_point : 'access.bitcasa.com'
end
|
Instance Attribute Details
#account ⇒ Account (readonly)
RestAdapter::Errors::OperationNotAllowedError]
53 54 55 |
# File 'lib/cloudfs/session.rb', line 53
def account
@account ||= get_account
end
|
#filesystem ⇒ FileSystem (readonly)
Returns FileSystem instance linked with this session.
31 32 33 |
# File 'lib/cloudfs/session.rb', line 31
def filesystem
@filesystem ||= FileSystem.new(@rest_adapter)
end
|
#user ⇒ User (readonly)
Returns profile of end-user linked with this session.
42 43 44 |
# File 'lib/cloudfs/session.rb', line 42
def user
@user ||= get_user
end
|
Instance Method Details
#action_history(start_version: -10,, stop_version: nil) ⇒ Array<Hash>
Action history lists history of file, folder, and share actions
default -10. It can be negative in order to get most recent actions.
RestAdapter::Errors::OperationNotAllowedError]
243 244 245 246 |
# File 'lib/cloudfs/session.rb', line 243
def action_history(start_version: -10, stop_version: nil)
validate_session
@rest_adapter.list_history(start: start_version, stop: stop_version)
end
|
#authenticate(username, password) ⇒ true
Attempts to log into the end-user's filesystem, links this session to an account
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cloudfs/session.rb', line 106
def authenticate(username, password)
validate_session
fail RestAdapter::Errors::OperationNotAllowedError,
'Cannot re-authenticate, initialize new session instance' if is_linked?
fail RestAdapter::Errors::ArgumentError,
'Invalid argument, must pass username' if RestAdapter::Utils.is_blank?(username)
fail RestAdapter::Errors::ArgumentError,
'Invalid argument, must pass password' if RestAdapter::Utils.is_blank?(password)
@rest_adapter.authenticate(username, password)
end
|
#create_account(username, password, email: nil, first_name: nil, last_name: nil, log_in_to_created_user: false) ⇒ Account
Created Account is not linked, authenticate this session with new account credentials before using it.
Creates a new end-user account for a Paid CloudFS account
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/cloudfs/session.rb', line 176
def create_account(username, password, email: nil, first_name: nil,
last_name: nil, log_in_to_created_user: false)
validate_session
fail RestAdapter::Errors::OperationNotAllowedError,
'New account creation with already linked session is not possible,
initialize new session instance' if is_linked?
fail RestAdapter::Errors::ArgumentError,
'Invalid argument, must pass username' if RestAdapter::Utils.is_blank?(username)
fail RestAdapter::Errors::ArgumentError,
'Invalid argument, must pass password' if RestAdapter::Utils.is_blank?(password)
if RestAdapter::Utils.is_blank?(@admin_credentials[:clientid]) ||
RestAdapter::Utils.is_blank?(@admin_credentials[:secret])
fail RestAdapter::Errors::ArgumentError,
'Please set the admin credentials in order to create account.'
end
admin_client = RestAdapter.new(
@admin_credentials[:clientid],
@admin_credentials[:secret], @admin_credentials[:host],
http_debug: @http_debug)
begin
response = admin_client.create_account(
username,
password, email: email,
first_name: first_name,
last_name: last_name)
if log_in_to_created_user
admin_client.authenticate(username, password)
end
Account.new(@rest_adapter, ** response)
ensure
admin_client.unlink
end
end
|
#is_linked? ⇒ Boolean
Returns whether current session is linked to the API server and can make authenticated requests.
120 121 122 |
# File 'lib/cloudfs/session.rb', line 120
def is_linked?
@rest_adapter.linked?
end
|
#set_admin_credentials(admin_client_id, admin_client_secret) ⇒ Object
Set the admin credentials.
58 59 60 61 62 63 64 65 |
# File 'lib/cloudfs/session.rb', line 58
def set_admin_credentials(admin_client_id, admin_client_secret)
if RestAdapter::Utils.is_blank?(admin_client_id) || RestAdapter::Utils.is_blank?(admin_client_secret)
fail RestAdapter::Errors::ArgumentError,
'Invalid input, expected admin client id and admin client secret'
end
@admin_credentials[:clientid] = admin_client_id ? admin_client_id : nil
@admin_credentials[:secret] = admin_client_secret ? admin_client_secret : nil
end
|
#unlink ⇒ true
CloudFS objects remain valid only till session is linked, once unlinked all RESTful objects generated through this session are expected to raise RestAdapter::Errors::SessionNotLinked exception for any RESTful operation.
Session cannot be re-authenticated once unlinked.
Discards current authentication
134 135 136 137 |
# File 'lib/cloudfs/session.rb', line 134
def unlink
@rest_adapter.unlink
@unlinked = true
end
|