class Pcloud::Folder
Constants
- SUPPORTED_FIND_BY_PARAMS
- SUPPORTED_UPDATE_PARAMS
Attributes
created_at[R]
id[R]
is_deleted[R]
modified_at[R]
name[R]
parent_folder_id[R]
path[R]
Public Class Methods
exists?(id)
click to toggle source
# 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)
click to toggle source
# File lib/pcloud/folder.rb, line 94 def find(id) parse_one(Client.execute("listfolder", query: { folderid: id })) end
find_by(params)
click to toggle source
# 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] }.compact parse_one(Client.execute("listfolder", query: query)) end
first_or_create(params)
click to toggle source
# 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
new(params)
click to toggle source
# 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
Public Instance Methods
contents()
click to toggle source
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.
# 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
delete()
click to toggle source
This method is the safest way to delte folders and will fail if the folder has contents.
# File lib/pcloud/folder.rb, line 51 def delete parse_one(Client.execute("deletefolder", query: { folderid: id })) end
delete!()
click to toggle source
This method will delete a folder and recursively delete all its contents
# 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_folder()
click to toggle source
# File lib/pcloud/folder.rb, line 61 def parent_folder @parent_folder ||= Folder.find(parent_folder_id) end
update(params)
click to toggle source
# 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