Class: CrateAPI::Crates

Inherits:
Object
  • Object
show all
Defined in:
lib/crate_api/crates.rb

Overview

Crates class which is used to add, list, manipulate the Crates on a given account.

Constant Summary collapse

CRATE_ACTIONS =

Hash of available actions to take upon the Crates endpoint.

{
  :add => "add.json",
  :list => "list.json",
  :rename => "rename/%s.json",
  :destroy => "destroy/%s.json"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_array(array) ⇒ Array

Class method to return an array of crate objects.

Returns:

  • (Array)

    an array of initialized Crate objects.



46
47
48
49
50
51
52
53
# File 'lib/crate_api/crates.rb', line 46

def self.from_array(array)
  return [] unless array != nil
  crates = Array.new
  array.each do |crate|
    crates.push(Crate.new(crate))
  end
  return crates
end

Instance Method Details

#add(name) ⇒ nil

Add a new crate.

Parameters:

  • name (String)

    of the crate to add.

Returns:

  • (nil)

    no output from this method is expected if everything goes right.

Raises:



20
21
22
23
# File 'lib/crate_api/crates.rb', line 20

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

#allArray

Get a list of crates (w/ files).

Returns:

  • (Array)

    an array of crate objects.



38
39
40
41
# File 'lib/crate_api/crates.rb', line 38

def all
  hash = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CRATE_ACTIONS[:list]}", :get))
  return Crates.from_array(hash["crates"])
end

#listArray

Note:

This will not return crate objects with the files they contain.

Get a list of crates (w/o files).

Returns:

  • (Array)

    an array of crate objects.

See Also:



30
31
32
33
# File 'lib/crate_api/crates.rb', line 30

def list
  hash = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CRATE_ACTIONS[:list]}", :get))
  return Crates.from_array(hash["crates"])
end