Class: GunBroker::User::ItemsDelegate
- Inherits:
-
Object
- Object
- GunBroker::User::ItemsDelegate
- Includes:
- TokenHeader
- Defined in:
- lib/gun_broker/user/items_delegate.rb
Overview
Used to scope Item actions by GunBroker::User.
Instance Method Summary collapse
-
#all ⇒ Array<Item>
Returns all the User's items (both selling and not selling).
-
#bid_on ⇒ Array<Item>
Returns all the items the User has bid on.
-
#create(attributes = {}) ⇒ GunBroker::Item
Sends a multipart/form-data POST request to create an Item with the given
attributes. -
#create!(attributes = {}) ⇒ GunBroker::Item
Same as #create but raises GunBroker::Error::RequestError on failure.
-
#find(item_id) ⇒ Item
Finds a specific User's Item by ID.
-
#find!(item_id) ⇒ Item
Same as #find but raises GunBroker::Error::NotFound if no item is found.
-
#initialize(user) ⇒ ItemsDelegate
constructor
A new instance of ItemsDelegate.
-
#not_won(options = {}) ⇒ Array<Item>
Items the User has bid on, but not won.
-
#selling(options = {}) ⇒ Array<Item>
Returns Items that are currently selling.
-
#sold(options = {}) ⇒ Array<Item>
Items the User has sold.
-
#unsold(options = {}) ⇒ Array<Item>
Items that were listed, but not sold.
-
#update(*args) ⇒ GunBroker::Item
Updates an Item with the given attributes.
-
#update!(item_id, attributes = {}) ⇒ GunBroker::Item
Same as #update but raises exceptions on error.
-
#won(options = {}) ⇒ Array<Item>
Items the User has won.
Constructor Details
#initialize(user) ⇒ ItemsDelegate
Returns a new instance of ItemsDelegate.
11 12 13 |
# File 'lib/gun_broker/user/items_delegate.rb', line 11 def initialize(user) @user = user end |
Instance Method Details
#all ⇒ Array<Item>
GET /Items
Returns all the User's items (both selling and not selling).
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_on ⇒ Array<Item>
GET /ItemsBidOn
Returns all the items the User has bid on.
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.
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.
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.
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.
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>
GET /ItemsNotWon
Items the User has bid on, but not won.
74 75 76 |
# File 'lib/gun_broker/user/items_delegate.rb', line 74 def not_won( = {}) @not_won ||= fetch_items(:ItemsNotWon, params_for(:timeframe, )) end |
#selling(options = {}) ⇒ Array<Item>
GET /Items
Returns Items that are currently selling.
84 85 86 |
# File 'lib/gun_broker/user/items_delegate.rb', line 84 def selling( = {}) @selling ||= fetch_items(:ItemsSelling, params_for(:itemid, )) end |
#sold(options = {}) ⇒ Array<Item>
GET /ItemsSold
Items the User has sold.
92 93 94 95 96 97 98 99 |
# File 'lib/gun_broker/user/items_delegate.rb', line 92 def sold( = {}) params = [ *params_for(:timeframe, ), *params_for(:itemid, ) ].to_h @sold ||= fetch_items(:ItemsSold, params) end |
#unsold(options = {}) ⇒ Array<Item>
GET /ItemsUnsold
Items that were listed, but not sold.
105 106 107 108 109 110 111 112 |
# File 'lib/gun_broker/user/items_delegate.rb', line 105 def unsold( = {}) params = [ *params_for(:timeframe, ), *params_for(:itemid, ) ].to_h @unsold ||= fetch_items(:ItemsUnsold, params) end |
#update(*args) ⇒ GunBroker::Item
Updates an Item with the given attributes.
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.
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 |