Sha256: 223ab32e7b1aabb3ab09686b75011aed90575d0bbe85fde33a8083ebb3d71f1b

Contents?: true

Size: 1.66 KB

Versions: 30

Compression:

Stored size: 1.66 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, 'PageSize' => GunBroker::API::PAGE_SIZE })
      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

30 entries across 30 versions & 1 rubygems

Version Path
gun_broker-1.4.8 lib/gun_broker/category.rb
gun_broker-1.4.7 lib/gun_broker/category.rb
gun_broker-1.4.6 lib/gun_broker/category.rb
gun_broker-1.4.5 lib/gun_broker/category.rb
gun_broker-1.4.4 lib/gun_broker/category.rb
gun_broker-1.4.3 lib/gun_broker/category.rb
gun_broker-1.4.2 lib/gun_broker/category.rb
gun_broker-1.4.1 lib/gun_broker/category.rb
gun_broker-1.4.0 lib/gun_broker/category.rb
gun_broker-1.3.2 lib/gun_broker/category.rb
gun_broker-1.3.1 lib/gun_broker/category.rb
gun_broker-1.3.0 lib/gun_broker/category.rb
gun_broker-1.2.1.1 lib/gun_broker/category.rb
gun_broker-1.2.1 lib/gun_broker/category.rb
gun_broker-1.2.0 lib/gun_broker/category.rb
gun_broker-1.1.3.1 lib/gun_broker/category.rb
gun_broker-1.1.2 lib/gun_broker/category.rb
gun_broker-1.1.1 lib/gun_broker/category.rb
gun_broker-1.1.0 lib/gun_broker/category.rb
gun_broker-1.0.0 lib/gun_broker/category.rb