Class: GunBroker::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/gun_broker/category.rb

Overview

Represents a GunBroker category.

Constant Summary collapse

ROOT_CATEGORY_ID =

The top-level category ID.

0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Category

Returns a new instance of Category.

Parameters:

  • attrs (Hash) (defaults to: {})

    The JSON attributes from the API response.



32
33
34
# File 'lib/gun_broker/category.rb', line 32

def initialize(attrs = {})
  @attrs = attrs
end

Class Method Details

.all(parent_id = ROOT_CATEGORY_ID) ⇒ Array<Category>

Returns An array of GunBroker::Category instances.

Parameters:

  • parent_id (Integer, String) (defaults to: ROOT_CATEGORY_ID)

    (optional) Return all subcategories of the given parent Category ID; defaults to the root (top-level) categories.

Returns:

  • (Array<Category>)

    An array of GunBroker::Category instances.



10
11
12
13
# File 'lib/gun_broker/category.rb', line 10

def self.all(parent_id = ROOT_CATEGORY_ID)
  response = GunBroker::API.get('/Categories', { 'ParentCategoryID' => parent_id, 'PageSize' => GunBroker::API::PAGE_SIZE })
  response['results'].map { |attrs| new(attrs) }
end

.find(category_id) ⇒ Category

Returns A Category instance or nil if no Category with category_id exists.

Parameters:

  • category_id (Integer, String)

    The ID of the Category to find.

Returns:

  • (Category)

    A Category instance or nil if no Category with category_id exists.



17
18
19
20
21
# File 'lib/gun_broker/category.rb', line 17

def self.find(category_id)
  find!(category_id)
rescue GunBroker::Error::NotFound
  nil
end

.find!(category_id) ⇒ Category

Same as find but raises GunBroker::Error::NotFound if no Category is found.

Parameters:

  • category_id (Integer, String)

    The ID of the Category to find.

Returns:

  • (Category)

    A Category instance or nil if no Category with category_id exists.

Raises:



27
28
29
# File 'lib/gun_broker/category.rb', line 27

def self.find!(category_id)
  new(GunBroker::API.get("/Categories/#{category_id}"))
end

Instance Method Details

#[](key) ⇒ Object

Returns The value of the given key or nil.

Parameters:

  • key (String)

    A Category attribute name (from the JSON response).

Returns:

  • The value of the given key or nil.



48
49
50
# File 'lib/gun_broker/category.rb', line 48

def [](key)
  @attrs[key]
end

#idInteger

Returns The Category ID.

Returns:

  • (Integer)

    The Category ID.



37
38
39
# File 'lib/gun_broker/category.rb', line 37

def id
  @attrs['categoryID']
end

#nameString

Returns The Category name.

Returns:

  • (String)

    The Category name.



42
43
44
# File 'lib/gun_broker/category.rb', line 42

def name
  @attrs['categoryName']
end