Class: GunBroker::User::ItemsDelegate

Inherits:
Object
  • Object
show all
Includes:
TokenHeader
Defined in:
lib/gun_broker/user/items_delegate.rb

Overview

Used to scope Item actions by GunBroker::User.

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ ItemsDelegate

Returns a new instance of ItemsDelegate.

Parameters:



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

def initialize(user)
  @user = user
end

Instance Method Details

#allArray<Item>

Note:

GET /Items

Returns all the User's items (both selling and not selling).

Returns:

Raises:



20
21
22
23
# File 'lib/gun_broker/user/items_delegate.rb', line 20

def all
  # NOTE: this endpoint will not return items that were sold
  @all ||= fetch_items(:Items, params_for(:sellername))
end

#bid_onArray<Item>

Note:

GET /ItemsBidOn

Returns all the items the User has bid on.

Returns:



28
29
30
# File 'lib/gun_broker/user/items_delegate.rb', line 28

def bid_on
  @bid_on ||= fetch_items(:ItemsBidOn)
end

#create(attributes = {}) ⇒ GunBroker::Item

Sends a multipart/form-data POST request to create an Item with the given attributes.

Returns:



34
35
36
37
38
# File 'lib/gun_broker/user/items_delegate.rb', line 34

def create(attributes = {})
  create!
rescue GunBroker::Error
  false
end

#create!(attributes = {}) ⇒ GunBroker::Item

Same as #create but raises GunBroker::Error::RequestError on failure.

Returns:

Raises:



44
45
46
47
48
# File 'lib/gun_broker/user/items_delegate.rb', line 44

def create!(attributes = {})
  response = GunBroker::API.multipart_post('/Items', attributes, token_header(@user.token))
  item_id = response.body['links'].first['title']
  GunBroker::Item.find(item_id)
end

#find(item_id) ⇒ Item

Finds a specific User's Item by ID. Calls Item.find to get full Item details.

Returns:

  • (Item)

    Returns the Item or nil if no Item found.

Raises:



53
54
55
56
57
58
59
60
# File 'lib/gun_broker/user/items_delegate.rb', line 53

def find(item_id)
  # HACK: This has to filter through `#all`, since the GunBroker API currently has no way to scope the `/Items/{itemID}` endpoint by user.
  if all.select { |item| item.id.to_s == item_id.to_s }.first
    GunBroker::Item.find(item_id)
  else
    nil
  end
end

#find!(item_id) ⇒ Item

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

Returns:

  • (Item)

    Returns the Item or nil if no Item found.

Raises:



65
66
67
68
69
# File 'lib/gun_broker/user/items_delegate.rb', line 65

def find!(item_id)
  item = find(item_id)
  raise GunBroker::Error::NotFound.new("Couldn't find item with ID '#{item_id}'") if item.nil?
  item
end

#not_won(options = {}) ⇒ Array<Item>

Note:

GET /ItemsNotWon

Items the User has bid on, but not won.

Returns:



74
75
76
# File 'lib/gun_broker/user/items_delegate.rb', line 74

def not_won(options = {})
  @not_won ||= fetch_items(:ItemsNotWon, params_for(:timeframe, options))
end

#selling(options = {}) ⇒ Array<Item>

Note:

GET /Items

Returns Items that are currently selling.

Parameters:

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

    ItemID=>ItemID.

Returns:

Raises:



84
85
86
# File 'lib/gun_broker/user/items_delegate.rb', line 84

def selling(options = {})
  @selling ||= fetch_items(:ItemsSelling, params_for(:itemid, options))
end

#sold(options = {}) ⇒ Array<Item>

Note:

GET /ItemsSold

Items the User has sold.

Parameters:

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

    ItemID=>ItemID.

Returns:



92
93
94
95
96
97
98
99
# File 'lib/gun_broker/user/items_delegate.rb', line 92

def sold(options = {})
  params = [
    *params_for(:timeframe, options),
    *params_for(:itemid, options)
  ].to_h

  @sold ||= fetch_items(:ItemsSold, params)
end

#unsold(options = {}) ⇒ Array<Item>

Note:

GET /ItemsUnsold

Items that were listed, but not sold.

Parameters:

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

    ItemID=>ItemID.

Returns:



105
106
107
108
109
110
111
112
# File 'lib/gun_broker/user/items_delegate.rb', line 105

def unsold(options = {})
  params = [
    *params_for(:timeframe, options),
    *params_for(:itemid, options)
  ].to_h

  @unsold ||= fetch_items(:ItemsUnsold, params)
end

#update(*args) ⇒ GunBroker::Item

Updates an Item with the given attributes.

Parameters:

  • item_id (Integer, String)

    ID of the Item to update.

  • attributes (Hash)

    The new Item attributes.

Returns:

  • (GunBroker::Item)

    The updated Item instance or false if update fails.



117
118
119
120
121
# File 'lib/gun_broker/user/items_delegate.rb', line 117

def update(*args)
  update!(*args)
rescue GunBroker::Error
  false
end

#update!(item_id, attributes = {}) ⇒ GunBroker::Item

Same as #update but raises exceptions on error.

Parameters:

  • item_id (Integer, String)

    ID of the Item to update.

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

    The new Item attributes.

Returns:

Raises:



129
130
131
132
# File 'lib/gun_broker/user/items_delegate.rb', line 129

def update!(item_id, attributes = {})
  GunBroker::API.put("/Items/#{item_id}", attributes, token_header(@user.token))
  GunBroker::Item.find!(item_id)
end

#won(options = {}) ⇒ Array<Item>

Note:

GET /ItemsWon

Items the User has won.

Returns:



137
138
139
# File 'lib/gun_broker/user/items_delegate.rb', line 137

def won(options = {})
  @won ||= fetch_items(:ItemsWon, params_for(:timeframe, options))
end