Class: CloudFS::Share

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudfs/share.rb

Overview

Share class is used to create and manage shares in end-user's account

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rest_adapter, **properties) ⇒ Share

Returns a new instance of Share.

Parameters:

  • rest_adapter (RestAdapter)

    cloudfs RESTful api object

  • properties (Hash)

    metadata of share @option properties [String] :share_key @option properties [String] :share_type @option properties [String] :share_name @option properties [String] :url @option properties [String] :short_url @option properties [String] :share_size @option properties [Fixnum] :date_created



88
89
90
91
92
93
# File 'lib/cloudfs/share.rb', line 88

def initialize(rest_adapter, ** properties)
  fail RestAdapter::Errors::ArgumentError,
       'Invalid RestAdapter, input type must be CloudFS::RestAdapter' unless rest_adapter.is_a?(CloudFS::RestAdapter)
  @rest_adapter = rest_adapter
  set_share_info(** properties)
end

Instance Attribute Details

#application_dataString (readonly)

Returns application data.

Returns:

  • (String)

    application data



29
30
31
# File 'lib/cloudfs/share.rb', line 29

def application_data
  @application_data
end

#date_content_last_modifiedTime (readonly)

Returns modified time.

Returns:

  • (Time)

    modified time



60
61
62
63
64
65
66
# File 'lib/cloudfs/share.rb', line 60

def date_content_last_modified
  if @date_content_last_modified
    Time.at(@date_content_last_modified)
  else
    nil
  end
end

#date_createdTime (readonly)

Returns creation time.

Returns:

  • (Time)

    creation time



50
51
52
53
54
55
56
# File 'lib/cloudfs/share.rb', line 50

def date_created
  if @date_created
    Time.at(@date_created)
  else
    nil
  end
end

#date_meta_last_modifiedTime (readonly)

Returns modified time.

Returns:

  • (Time)

    modified time



70
71
72
73
74
75
76
# File 'lib/cloudfs/share.rb', line 70

def date_meta_last_modified
  if @date_meta_last_modified
    Time.at(@date_meta_last_modified)
  else
    nil
  end
end

#nameString (readonly)

Returns name.

Returns:

  • (String)

    name



14
15
16
# File 'lib/cloudfs/share.rb', line 14

def name
  @name
end

#share_keyString (readonly)

Returns share_key.

Returns:

  • (String)

    share_key



11
12
13
# File 'lib/cloudfs/share.rb', line 11

def share_key
  @share_key
end

#short_urlString (readonly)

Returns short_url.

Returns:

  • (String)

    short_url



23
24
25
# File 'lib/cloudfs/share.rb', line 23

def short_url
  @short_url
end

#sizeString (readonly)

Returns size.

Returns:

  • (String)

    size



26
27
28
# File 'lib/cloudfs/share.rb', line 26

def size
  @size
end

#typeString (readonly)

Returns type.

Returns:

  • (String)

    type



17
18
19
# File 'lib/cloudfs/share.rb', line 17

def type
  @type
end

#urlString (readonly)

Returns url.

Returns:

  • (String)

    url



20
21
22
# File 'lib/cloudfs/share.rb', line 20

def url
  @url
end

Instance Method Details

#change_attributes(values, password: nil) ⇒ Boolean

Changes, adds, or removes the share’s password or updates the name.

Parameters:

  • values (Hash)

    metadata of share.

  • password (String) (defaults to: nil)

    current password of the share.

Options Hash (values):

  • :current_password (String)
  • :password (String)
  • :name (String)

Returns:

  • (Boolean)

    based on the success or fail status of the action.



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cloudfs/share.rb', line 197

def change_attributes(values, password: nil)
  current_password = values.has_key?('current_password') ? values['current_password'] : password
  new_password = values.has_key?('password') ? values['password'] : nil
  name = values.has_key?('name') ? values['name'] : nil

  response = @rest_adapter.alter_share_info(
      @share_key, current_password: current_password, password: new_password, name: name)

  set_share_info(** response)
  response.has_key?(:share_key)
end

#deletetrue

Note:

Subsequent operations shall fail

Delete this share

RestAdapter::Errors::InvalidShareError

RestAdapter::Errors::InvalidShareError]

Returns:

  • (true)

Raises:



155
156
157
158
159
160
# File 'lib/cloudfs/share.rb', line 155

def delete
  FileSystemCommon.validate_share_state(self)
  @rest_adapter.delete_share(@share_key)
  @exists = false
  true
end

#exists?Boolean

Returns whether the share exists, false only if it has been deleted.

Returns:

  • (Boolean)

    whether the share exists, false only if it has been deleted



125
126
127
# File 'lib/cloudfs/share.rb', line 125

def exists?
  @exists
end

#listArray<File, Folder>

List items in this share RestAdapter::Errors::InvalidShareError]

Returns:

Raises:



135
136
137
138
139
140
141
142
143
# File 'lib/cloudfs/share.rb', line 135

def list
  FileSystemCommon.validate_share_state(self)
  response = @rest_adapter.browse_share(@share_key).fetch(:items)
  FileSystemCommon.create_items_from_hash_array(
      response,
      @rest_adapter,
      parent_state: @parent_state,
      in_share: true)
end

#receive(path: nil, exists: 'RENAME') ⇒ Array<File, Folder>

Receive contents of this share at specified path in user's filesystem. All items found in share are copied to given location.

RestAdapter::Errors::InvalidShareError]

Parameters:

  • path (String) (defaults to: nil)

    path in user's account to receive share at, default is "/" root

  • exists (String) (defaults to: 'RENAME')

    ('RENAME', 'FAIL', 'OVERWRITE') action to take in case of conflict with existing items at path

Returns:

Raises:



245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cloudfs/share.rb', line 245

def receive(path: nil, exists: 'RENAME')
  FileSystemCommon.validate_share_state(self)
  response = @rest_adapter.receive_share(
      @share_key,
      path: path,
      exists: exists)

  FileSystemCommon.create_items_from_hash_array(
      response,
      @rest_adapter,
      parent: path)
end

#refreshShare

Note:

Locally changed properties i.e. name get discarded

Note:

raises RestAdapter::Errors::ServiceError if share is locked, unlock share if password is set

Refresh this share to latest state RestAdapter::Errors::InvalidShareError]

Returns:

  • (Share)

    returns self

Raises:



269
270
271
272
273
274
# File 'lib/cloudfs/share.rb', line 269

def refresh
  FileSystemCommon.validate_share_state(self)
  response = @rest_adapter.browse_share(share_key).fetch(:share)
  set_share_info(** response)
  self
end

#save(password: nil) ⇒ Share

Save this share's current state. Only name, is committed to this share in user's account

Parameters:

  • password (String) (defaults to: nil)

    current password for this share, if has been set, it is necessary even if share has been unlocked

Returns:

  • (Share)

    returns self

Raises:



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cloudfs/share.rb', line 219

def save(password: nil)
  FileSystemCommon.validate_share_state(self)
  if @changed_properties[:name]
    response = @rest_adapter.alter_share_info(
        @share_key,
        current_password: password,
        name: @changed_properties[:name])

    set_share_info(** response)
  end
  self
end

#set_name(new_name, password: nil) ⇒ Object

Set the name of the share

Parameters:

  • new_name (String)

    new name of the share.

  • password (String) (defaults to: nil)

    current password of the share.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cloudfs/share.rb', line 34

def set_name(new_name, password: nil)
  FileSystemCommon.validate_share_state(self)
  fail RestAdapter::Errors::ArgumentError,
       'Invalid input, expected new name' if RestAdapter::Utils.is_blank?(new_name)

  response = @rest_adapter.alter_share_info(
      @share_key,
      current_password: password,
      name: new_name)

  set_share_info(** response)
  self
end

#set_password(password, current_password: nil) ⇒ Share

Change password of this share

RestAdapter::Errors::InvalidShareError]

Parameters:

  • password (String)

    new password for this share

  • current_password (String) (defaults to: nil)

    is required if password is already set for this share

Returns:

  • (Share)

    return self

Raises:



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/cloudfs/share.rb', line 173

def set_password(password, current_password: nil)
  FileSystemCommon.validate_share_state(self)
  fail RestAdapter::Errors::ArgumentError,
       'Invalid input, expected the password' if RestAdapter::Utils.is_blank?(password)

  response = @rest_adapter.alter_share_info(
      @share_key,
      current_password: current_password,
      password: password)

  set_share_info(** response)
  self
end