class GunBroker::Category

Represents a GunBroker category.

Constants

ROOT_CATEGORY_ID

The top-level category ID.

Public Class Methods

all(parent_id = ROOT_CATEGORY_ID) click to toggle source

@param parent_id [Integer, String] (optional) Return all subcategories of the given parent Category ID; defaults to the root (top-level) categories. @return [Array<Category>] An array of GunBroker::Category instances.

# 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) click to toggle source

@param category_id [Integer, String] The ID of the Category to find. @return [Category] A Category instance or `nil` if no Category with `category_id` exists.

# 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) click to toggle source

Same as {.find} but raises GunBroker::Error::NotFound if no Category is found. @param (see .find) @raise [GunBroker::Error::NotFound] If no Category with `category_id` exists. @return (see .find)

# File lib/gun_broker/category.rb, line 27
def self.find!(category_id)
  new(GunBroker::API.get("/Categories/#{category_id}"))
end
new(attrs = {}) click to toggle source

@param attrs [Hash] The JSON attributes from the API response.

# File lib/gun_broker/category.rb, line 32
def initialize(attrs = {})
  @attrs = attrs
end

Public Instance Methods

[](key) click to toggle source

@param key [String] A Category attribute name (from the JSON response). @return The value of the given `key` or `nil`.

# File lib/gun_broker/category.rb, line 48
def [](key)
  @attrs[key]
end
id() click to toggle source

@return [Integer] The Category ID.

# File lib/gun_broker/category.rb, line 37
def id
  @attrs['categoryID']
end
name() click to toggle source

@return [String] The Category name.

# File lib/gun_broker/category.rb, line 42
def name
  @attrs['categoryName']
end