Class: Goodwill::Account

Inherits:
Object
  • Object
show all
Includes:
CSSPaths, Mechanize, URLPaths
Defined in:
lib/goodwill/account.rb

Overview

Allows for basic account interaction

Constant Summary collapse

PER_PAGE =
25

Constants included from CSSPaths

CSSPaths::BIDS_PATH, CSSPaths::BIN_END_TIME_PATH, CSSPaths::BUY_IT_NOW_PATH, CSSPaths::CURRENT_PRICE_PATH, CSSPaths::END_TIME_PATH, CSSPaths::IN_PROGRESS_ROWS, CSSPaths::ITEMID_PATH, CSSPaths::ITEMS_COUNT_PATH, CSSPaths::ITEM_TITLE_PATH, CSSPaths::NO_ITEMS_FOUND_PATH, CSSPaths::SEARCH_ROWS, CSSPaths::SELLER_ID_LINK, CSSPaths::SELLER_PATH, CSSPaths::SHIPPING_PATH

Constants included from URLPaths

URLPaths::BID_URL, URLPaths::ITEM_SEARCH_URL, URLPaths::LOGIN_URL, URLPaths::OPEN_ORDERS_URL, URLPaths::SEARCH_URL, URLPaths::SHIPPING_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mechanize

logged_in?, login, #mechanize, mechanize

Constructor Details

#initialize(username, password = '', threads = 10) ⇒ Account

Returns a new instance of Account.



25
26
27
28
29
30
31
# 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

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



23
24
25
# File 'lib/goodwill/account.rb', line 23

def password
  @password
end

#threadsObject (readonly)

Returns the value of attribute threads.



23
24
25
# File 'lib/goodwill/account.rb', line 23

def threads
  @threads
end

#usernameObject (readonly)

Returns the value of attribute username.



23
24
25
# File 'lib/goodwill/account.rb', line 23

def username
  @username
end

Instance Method Details

#bid(itemid, bid) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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_progressObject



33
34
35
36
37
38
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
# 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