Sha256: 3495294e99da4fc3f4e9c7e4c9d1d184e550d55fa1a5202f4fdbc4fc17c2011a

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

module GunBroker
  # Represents a GunBroker category.
  class Category

    # The top-level category ID.
    ROOT_CATEGORY_ID = 0

    # @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.
    def self.all(parent_id = ROOT_CATEGORY_ID)
      response = GunBroker::API.get('/Categories', { 'ParentCategoryID' => parent_id })
      response['results'].map { |attrs| new(attrs) }
    end

    # @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.
    def self.find(category_id)
      find!(category_id)
    rescue GunBroker::Error::NotFound
      nil
    end

    # 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)
    def self.find!(category_id)
      new(GunBroker::API.get("/Categories/#{category_id}"))
    end

    # @param attrs [Hash] The JSON attributes from the API response.
    def initialize(attrs = {})
      @attrs = attrs
    end

    # @return [Integer] The Category ID.
    def id
      @attrs['categoryID']
    end

    # @return [String] The Category name.
    def name
      @attrs['categoryName']
    end

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

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gun_broker-0.5.2 lib/gun_broker/category.rb
gun_broker-0.5.1 lib/gun_broker/category.rb
gun_broker-0.4.11 lib/gun_broker/category.rb
gun_broker-0.4.10 lib/gun_broker/category.rb
gun_broker-0.4.9 lib/gun_broker/category.rb
gun_broker-0.4.8 lib/gun_broker/category.rb
gun_broker-0.4.7 lib/gun_broker/category.rb