Class: Pcloud::File

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

Defined Under Namespace

Modules: Parser Classes: InvalidParameter, InvalidParameters, MissingParameter, UploadFailed

Constant Summary collapse

SUPPORTED_UPDATE_PARAMS =
[:name, :parent_folder_id, :path].freeze
SUPPORTED_FIND_BY_PARAMS =
[:id, :path].freeze
FILE_CATAGORIES =
{
  "0" => "uncategorized",
  "1" => "image",
  "2" => "video",
  "3" => "audio",
  "4" => "document",
  "5" => "archive",
}.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) ⇒ File

Returns a new instance of File.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pcloud/file.rb', line 25

def initialize(params)
  @id = params.fetch(:id)
  @path = params.fetch(:path)
  @name = params.fetch(:name)
  @content_type = params.fetch(:content_type)
  @category = FILE_CATAGORIES.fetch((params.fetch(:category_id) || 0).to_s)
  @size = params.fetch(:size) # bytes
  @parent_folder_id = params.fetch(:parent_folder_id)
  @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

#categoryObject (readonly)

Returns the value of attribute category.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def category
  @category
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def content_type
  @content_type
end

#created_atObject (readonly)

Returns the value of attribute created_at.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def id
  @id
end

#is_deletedObject (readonly)

Returns the value of attribute is_deleted.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def is_deleted
  @is_deleted
end

#modified_atObject (readonly)

Returns the value of attribute modified_at.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def modified_at
  @modified_at
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def name
  @name
end

#parent_folder_idObject (readonly)

Returns the value of attribute parent_folder_id.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def parent_folder_id
  @parent_folder_id
end

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



22
23
24
# File 'lib/pcloud/file.rb', line 22

def size
  @size
end

Class Method Details

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.find(id) ⇒ Object



85
86
87
# File 'lib/pcloud/file.rb', line 85

def find(id)
  parse_one(Client.execute("stat", query: { fileid: id }))
end

.find_by(params) ⇒ Object

Raises:



89
90
91
92
93
94
95
96
# File 'lib/pcloud/file.rb', line 89

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], fileid: params[:id] }.compact
  parse_one(Client.execute("stat", query: query))
end

.upload(params) ⇒ Object



98
99
100
# File 'lib/pcloud/file.rb', line 98

def upload(params)
  process_upload(params)
end

.upload!(params) ⇒ Object



102
103
104
# File 'lib/pcloud/file.rb', line 102

def upload!(params)
  process_upload(params.merge({ overwrite: true }))
end

Instance Method Details

#deleteObject



54
55
56
# File 'lib/pcloud/file.rb', line 54

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

#download_urlObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pcloud/file.rb', line 62

def download_url
  @download_url ||= begin
    file_url_parts = Client.execute(
      "getfilelink",
      query: { fileid: id, forcedownload: 1, skipfilename: 1 }
    )
    "https://#{file_url_parts["hosts"].first}#{file_url_parts["path"]}"
  end
  # This allows us to cache the expensive part of this method, requesting
  # a download URL from pcloud, while maintaining consistency if the file
  # name changes later.
  "#{@download_url}/#{URI.encode_www_form_component(name)}"
end

#parent_folderObject



58
59
60
# File 'lib/pcloud/file.rb', line 58

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

#update(params) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pcloud/file.rb', line 38

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 param must start with `/`")
  end
  query = {
    fileid: id,
    tofolderid: params[:parent_folder_id] || nil,
    toname: params[:name] || nil,
    topath: params[:path] || nil
  }.compact
  parse_one(Client.execute("renamefile", query: query))
end