Class: Pcloud::Folder

Inherits:
Object
  • Object
show all
Includes:
Parser, TimeHelper
Defined in:
lib/pcloud/folder.rb,
lib/pcloud/folder/parser.rb

Defined Under Namespace

Modules: Parser Classes: InvalidParameter, InvalidParameters, MissingParameter

Constant Summary collapse

SUPPORTED_UPDATE_PARAMS =
[:name, :parent_folder_id, :path].freeze
SUPPORTED_FIND_BY_PARAMS =
[:id, :path, :recursive].freeze

Constants included from TimeHelper

TimeHelper::TIMEZONE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

included

Constructor Details

#initialize(params) ⇒ Folder

Returns a new instance of Folder.



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

def initialize(params)
  @id = params.fetch(:id)
  @path = params.fetch(:path)
  @name = params.fetch(:name)
  @parent_folder_id = params.fetch(:parent_folder_id)
  @contents = params.fetch(:contents)
  # Some APIs (mainly recursive operations according to pCloud) return either a
  # nil or an empty array of contents. In these cases, the @contents_are_confirmed
  # flag is set to `false` in order to allow one retry to fetch the actual
  # contents if the `contents` method is called on a folder object that does not
  # have any contents set yet.
  @contents_are_confirmed = @contents && @contents.size > 0
  @is_deleted = params.fetch(:is_deleted) || false
  @created_at = time_from(params.fetch(:created_at))
  @modified_at = time_from(params.fetch(:modified_at))
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



13
14
15
# File 'lib/pcloud/folder.rb', line 13

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/pcloud/folder.rb', line 13

def id
  @id
end

#is_deletedObject (readonly)

Returns the value of attribute is_deleted.



13
14
15
# File 'lib/pcloud/folder.rb', line 13

def is_deleted
  @is_deleted
end

#modified_atObject (readonly)

Returns the value of attribute modified_at.



13
14
15
# File 'lib/pcloud/folder.rb', line 13

def modified_at
  @modified_at
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/pcloud/folder.rb', line 13

def name
  @name
end

#parent_folder_idObject (readonly)

Returns the value of attribute parent_folder_id.



13
14
15
# File 'lib/pcloud/folder.rb', line 13

def parent_folder_id
  @parent_folder_id
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/pcloud/folder.rb', line 13

def path
  @path
end

Class Method Details

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/pcloud/folder.rb', line 86

def exists?(id)
  find(id)
  true
rescue Pcloud::Client::ErrorResponse => e
  return false if e.message == "Directory does not exist."
  raise e
end

.find(id, opts = {}) ⇒ Object



94
95
96
# File 'lib/pcloud/folder.rb', line 94

def find(id, opts={})
  parse_one(Client.execute("listfolder", query: { folderid: id, recursive: opts[:recursive] == true }))
end

.find_by(params) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
# File 'lib/pcloud/folder.rb', line 98

def find_by(params)
  unless (params.keys - SUPPORTED_FIND_BY_PARAMS).empty?
    raise InvalidParameters.new("Must be one of #{SUPPORTED_FIND_BY_PARAMS}")
  end
  raise InvalidParameters.new(":id takes precedent over :path, please only use one or the other") if params[:path] && params[:id]
  query = { path: params[:path], folderid: params[:id], recursive: params[:recursive] == true }.compact
  parse_one(Client.execute("listfolder", query: query))
end

.first_or_create(params) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/pcloud/folder.rb', line 76

def first_or_create(params)
  if params[:parent_folder_id] && params[:name]
    parse_one(Client.execute("createfolderifnotexists", query: { folderid: params[:parent_folder_id], name: params[:name] }))
  elsif params[:path]
    parse_one(Client.execute("createfolderifnotexists", query: { path: params[:path] }))
  else
    raise InvalidParameters.new("either :path or a combination of :parent_folder_id and :name params are required")
  end
end

Instance Method Details

#contentsObject

Some APIs return ‘nil` or `[]` for contents when a folder does actually have contents. This method allows us to re-try one time if we try to get the contents and find that they’re missing.



68
69
70
71
72
73
# File 'lib/pcloud/folder.rb', line 68

def contents
  return @contents if @contents_are_confirmed
  @contents = Folder.find(id).contents
  @contents_are_confirmed = true
  @contents
end

#deleteObject

This method is the safest way to delte folders and will fail if the folder has contents.



51
52
53
# File 'lib/pcloud/folder.rb', line 51

def delete
  parse_one(Client.execute("deletefolder", query: { folderid: id }))
end

#delete!Object

This method will delete a folder and recursively delete all its contents



56
57
58
59
# File 'lib/pcloud/folder.rb', line 56

def delete!
  Client.execute("deletefolderrecursive", query: { folderid: id })
  true # we don't get anything helpful back from pCloud on this request
end

#parent_folderObject



61
62
63
# File 'lib/pcloud/folder.rb', line 61

def parent_folder
  @parent_folder ||= Folder.find(parent_folder_id)
end

#update(params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pcloud/folder.rb', line 33

def update(params)
  unless (params.keys - SUPPORTED_UPDATE_PARAMS).empty?
    raise InvalidParameters.new("Must be one of #{SUPPORTED_UPDATE_PARAMS}")
  end
  if params[:path] && params[:path][0] != "/"
    raise InvalidParameter.new(":path parameter must start with `/`")
  end
  query = {
    folderid: id,
    tofolderid: params[:parent_folder_id] || nil,
    toname: params[:name] || nil,
    topath: params[:path] || nil
  }.compact
  parse_one(Client.execute("renamefolder", query: query))
end