Sha256: 231701e22529d68835ec914c41462905c95b9c78d446c4cc310c644958f04428

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require 'gun_broker/item/constants'

module GunBroker
  # Represents a GunBroker item (listing).
  class Item

    include GunBroker::Item::Constants

    # TODO: Refactor this, #attributes, and #[] into a module.
    # @return [Hash] Attributes parsed from the JSON response.
    attr_reader :attrs

    # @param item_id [Integer, String] The ID of the Item to find.
    # @return [Item] An Item instance or `nil` if no Item with `item_id` exists.
    def self.find(item_id)
      find!(item_id)
    rescue GunBroker::Error::NotFound
      nil
    end

    # Same as {.find} but raises GunBroker::Error::NotFound if no Item is found.
    # @param (see .find)
    # @raise [GunBroker::Error::NotFound] If no Item with `item_id` exists.
    # @return (see .find)
    def self.find!(item_id)
      new(GunBroker::API.get("/Items/#{item_id}"))
    end

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

    # @return [Integer] The Item ID.
    def id
      @attrs['itemID']
    end

    # @return [Hash] Attributes parsed from the JSON response.
    def attributes
      @attrs
    end

    # @return [Category] This Items Category.
    def category
      GunBroker::Category.find(@attrs['categoryID'])
    end

    # @return [String] Title of this Item.
    def title
      @attrs['title']
    end

    # @param key [String] An Item 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.8 lib/gun_broker/item.rb
gun_broker-0.4.7 lib/gun_broker/item.rb