Sha256: 038a56fdc80426dc3e54c909918727c1f25112db89133f23662b3521826fb43f

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module GunBroker
  # Represents a GunBroker category.
  class Category

    # The top-level category ID.
    ROOT_CATEGORY_ID = 0

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

    # @param category_id [Integer, String] The ID of the Category to find.
    # @return [Category] A Category instance.
    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

    # @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

2 entries across 2 versions & 1 rubygems

Version Path
gun_broker-0.4.1 lib/gun_broker/category.rb
gun_broker-0.4.0 lib/gun_broker/category.rb