Class: GunBroker::Item

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/gun_broker/item.rb,
lib/gun_broker/item/constants.rb

Overview

Represents a GunBroker item (listing).

Defined Under Namespace

Modules: Constants

Constant Summary

Constants included from Constants

Constants::AUCTION_DURATION, Constants::AUTO_RELIST, Constants::CONDITION, Constants::FIXED_PRICE_DURATION, Constants::INSPECTION_PERIOD, Constants::PAYMENT_METHODS, Constants::SHIPPING_CLASSES, Constants::SHIPPING_PAYER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Item

Returns a new instance of Item.

Parameters:

  • attrs (Hash) (defaults to: {})

    The JSON attributes from the API response.



31
32
33
# File 'lib/gun_broker/item.rb', line 31

def initialize(attrs = {})
  @attrs = attrs
end

Instance Attribute Details

#attrsHash (readonly)

TODO: Refactor this, #attributes, and #[] into a module.

Returns:

  • (Hash)

    Attributes parsed from the JSON response.



11
12
13
# File 'lib/gun_broker/item.rb', line 11

def attrs
  @attrs
end

Class Method Details

.find(item_id) ⇒ Item

Returns An Item instance or nil if no Item with item_id exists.

Parameters:

  • item_id (Integer, String)

    The ID of the Item to find.

Returns:

  • (Item)

    An Item instance or nil if no Item with item_id exists.



15
16
17
18
19
# File 'lib/gun_broker/item.rb', line 15

def self.find(item_id)
  find!(item_id)
rescue GunBroker::Error::NotFound
  nil
end

.find!(item_id) ⇒ Item

Same as find but raises GunBroker::Error::NotFound if no Item is found.

Parameters:

  • item_id (Integer, String)

    The ID of the Item to find.

Returns:

  • (Item)

    An Item instance or nil if no Item with item_id exists.

Raises:



25
26
27
28
# File 'lib/gun_broker/item.rb', line 25

def self.find!(item_id)
  response = GunBroker::API.get("/Items/#{item_id}")
  new(response.body)
end

Instance Method Details

#[](key) ⇒ Object

Returns The value of the given key or nil.

Parameters:

  • key (String)

    An Item attribute name (from the JSON response).

Returns:

  • The value of the given key or nil.



66
67
68
# File 'lib/gun_broker/item.rb', line 66

def [](key)
  @attrs[key]
end

#attributesHash

Returns Attributes parsed from the JSON response.

Returns:

  • (Hash)

    Attributes parsed from the JSON response.



41
42
43
# File 'lib/gun_broker/item.rb', line 41

def attributes
  @attrs
end

#categoryCategory

Returns This Items Category.

Returns:



46
47
48
# File 'lib/gun_broker/item.rb', line 46

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

#idInteger

Returns The Item ID.

Returns:

  • (Integer)

    The Item ID.



36
37
38
# File 'lib/gun_broker/item.rb', line 36

def id
  @attrs['itemID']
end

#titleString

Returns Title of this Item.

Returns:

  • (String)

    Title of this Item.



51
52
53
# File 'lib/gun_broker/item.rb', line 51

def title
  @attrs['title']
end

#urlString

Returns GunBroker.com URL for this Item.

Returns:

  • (String)

    GunBroker.com URL for this Item.



56
57
58
59
60
61
62
# File 'lib/gun_broker/item.rb', line 56

def url
  if GunBroker.sandbox
    "http://www.sandbox.gunbroker.com/Auction/ViewItem.aspx?Item=#{id}"
  else
    "http://www.gunbroker.com/Auction/ViewItem.aspx?Item=#{id}"
  end
end