class Goodwill::Account
Allows for basic account interaction
Constants
- PER_PAGE
Attributes
password[R]
threads[R]
username[R]
Public Class Methods
new(username, password = '', threads = 10)
click to toggle source
# File lib/goodwill/account.rb, line 25 def initialize(username, password = '', threads = 10) @username = username @password = password @threads = threads Goodwill::Mechanize.username = @username Goodwill::Mechanize.password = @password end
Public Instance Methods
bid(itemid, bid)
click to toggle source
# File lib/goodwill/account.rb, line 50 def bid(itemid, bid) item = mechanize.get(ITEM_SEARCH_URL + itemid.to_s) href = item.search(SELLER_ID_LINK).attribute('href').value seller_id = href.split('/').last params = { itemId: itemid, bidAmount: bid, quantity: 1, sellerId: seller_id } # bidresult # 1 - success (check message for 'Bid Received!') # 1 - outbid (check message for 'You have already been outbid.') # -5 - bid less than minimum mechanize.get(BID_URL, params) do |page| res = JSON.parse(page.body) case res['BidResult'] when 1 return true if res['BidResultMessage'].include?('Bid Received') return false if res['BidResultMessage'].include?('You have already been outbid.') raise Goodwill::BidError, res['BidResultMessage'] when -5 raise Goodwill::BidError, res['BidResultMessage'] end end end
in_progress()
click to toggle source
# File lib/goodwill/account.rb, line 33 def in_progress in_progress_page = mechanize.get(OPEN_ORDERS_URL) Parallel.map(in_progress_page.search(IN_PROGRESS_ROWS), in_threads: @threads) do |row| Goodwill::BiddingAuction.new(itemid_from_open_order_row(row), mechanize) end end
search(item_title)
click to toggle source
# File lib/goodwill/account.rb, line 40 def search(item_title) search_page = mechanize.get(SEARCH_URL + item_title) Array.new(pages(total_items(search_page))) do |i| search_page = search_page.link_with(text: '>').click unless i.zero? Parallel.map(search_page.search(SEARCH_ROWS), in_threads: @threads) do |row| Goodwill::Auction.new(itemid_from_search_row(row)) end end.flatten end
Private Instance Methods
buyer_token()
click to toggle source
# File lib/goodwill/account.rb, line 83 def buyer_token mechanize.cookies.select { |c| c.name == 'AuthenticatedBuyerToken' }.first.value end
itemid_from_open_order_row(row)
click to toggle source
# File lib/goodwill/account.rb, line 87 def itemid_from_open_order_row(row) row.search('a').text end
itemid_from_search_row(row)
click to toggle source
# File lib/goodwill/account.rb, line 91 def itemid_from_search_row(row) row.search('a').first.attributes['href'].value.split('/').last end
pages(items)
click to toggle source
# File lib/goodwill/account.rb, line 95 def pages(items) (items / 40.to_f).ceil end
request_token()
click to toggle source
# File lib/goodwill/account.rb, line 99 def request_token mechanize.cookies.select { |c| c.name == '__RequestVerificationToken' }.first.value end
total_items(page)
click to toggle source
# File lib/goodwill/account.rb, line 103 def total_items(page) return 0 unless page.search(NO_ITEMS_FOUND_PATH).empty? page.search(ITEMS_COUNT_PATH).first.text.split(' of ')[1].split(' ').first.to_i end