Class: CrateAPI::Crate

Inherits:
CrateObject show all
Defined in:
lib/crate_api/crate.rb

Overview

Crate object class which is used to manipulate the single Crate object.

Instance Attribute Summary collapse

Attributes inherited from CrateObject

#id, #name, #short_code

Instance Method Summary collapse

Methods inherited from CrateObject

#short_url

Constructor Details

#initialize(hash) ⇒ Crate

Returns a new instance of Crate.



17
18
19
20
# File 'lib/crate_api/crate.rb', line 17

def initialize(hash)
  super(hash)
  @files = CrateAPI::Items.from_array(hash["files"])
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



16
17
18
# File 'lib/crate_api/crate.rb', line 16

def files
  @files
end

Instance Method Details

#add_file(path) ⇒ CrateFileAlreadyExistsError?

Add a file to the given crate object.

Parameters:

  • This (String)

    is the path to the file that you wish to upload.

Returns:

  • (CrateFileAlreadyExistsError, nil)

    if there is an issue uploading the file to the crate, an error will be raised with the message explaining why.

Raises:



42
43
44
45
46
# File 'lib/crate_api/crate.rb', line 42

def add_file(path)
  file = File.new(path)
  response = CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CrateAPI::Items::ITEM_ACTIONS[:upload]}", :post, {:body => {:file => file, :crate_id => @id}})
  raise CrateFileAlreadyExistsError, response["message"] unless response["status"] != "failure"
end

#destroyCrateDestroyError?

Destroys the given crate object.

Returns:

  • (CrateDestroyError, nil)

    if there is an issue destroying the crate, an error will be raised with the message explaining why.

Raises:



25
26
27
28
# File 'lib/crate_api/crate.rb', line 25

def destroy
  response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CrateAPI::Crates::CRATE_ACTIONS[:destroy] % ["#{self.id}"]}", :post))
  raise CrateDestroyError, response["message"] unless response["status"] != "failure"
end

#rename(name) ⇒ CrateRenameError?

Renamed the given crate object.

Returns:

  • (CrateRenameError, nil)

    if there is an issue with renaming the crate, an error will be raised with the message explaining why.

Raises:



33
34
35
36
# File 'lib/crate_api/crate.rb', line 33

def rename(name)
  response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CrateAPI::Crates::CRATE_ACTIONS[:rename] % ["#{self.id}"]}", :post, {:body => {:name => name}}))
  raise CrateRenameError, response["message"] unless response["status"] != "failure"
end