Class: Beway::Bidder
- Inherits:
-
Object
- Object
- Beway::Bidder
- Defined in:
- lib/beway/bidder.rb
Overview
Bidder
Wrapper for Mechanize to perform actions on ebay
Constant Summary collapse
- EBAY_HOME =
'http://www.ebay.com'
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#last_login_time ⇒ Object
readonly
Returns the value of attribute last_login_time.
-
#logged_in ⇒ Object
readonly
Returns the value of attribute logged_in.
-
#password ⇒ Object
writeonly
Sets the attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#bid(auction_url, amount) ⇒ Object
bid amount on given auction.
-
#initialize(username, password) ⇒ Bidder
constructor
create a bidder with login credentials.
-
#login ⇒ Object
log user in with credentials.
Constructor Details
#initialize(username, password) ⇒ Bidder
create a bidder with login credentials
19 20 21 22 23 24 25 |
# File 'lib/beway/bidder.rb', line 19 def initialize(username, password) @username = username @password = password @agent = Mechanize.new @logged_in = false @last_login_time = nil end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
16 17 18 |
# File 'lib/beway/bidder.rb', line 16 def agent @agent end |
#last_login_time ⇒ Object (readonly)
Returns the value of attribute last_login_time.
16 17 18 |
# File 'lib/beway/bidder.rb', line 16 def last_login_time @last_login_time end |
#logged_in ⇒ Object (readonly)
Returns the value of attribute logged_in.
16 17 18 |
# File 'lib/beway/bidder.rb', line 16 def logged_in @logged_in end |
#password=(value) ⇒ Object (writeonly)
Sets the attribute password
15 16 17 |
# File 'lib/beway/bidder.rb', line 15 def password=(value) @password = value end |
#username ⇒ Object
Returns the value of attribute username.
14 15 16 |
# File 'lib/beway/bidder.rb', line 14 def username @username end |
Instance Method Details
#bid(auction_url, amount) ⇒ Object
bid amount on given auction
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/beway/bidder.rb', line 42 def bid(auction_url, amount) # get auction auction_page = @agent.get(auction_url) # get the bid form forms = auction_page.forms_with( :action => /http:\/\/offer\.ebay\.com\// ) raise BidderError, "Couldn't find auction bid form" if forms.length != 1 bid_form = forms[0] # fill in, submit bid form bid_form.maxbid = amount bid_response = bid_form.submit # if given a login page, do it and get redirected to confirm bid if is_login_page?(bid_response) bid_response = handle_login_page(bid_response) end # get confirm bid form forms = bid_response.forms_with( :action => 'http://offer.ebay.com/ws/eBayISAPI.dll' ) raise BidderError, "Couldn't find confirm bid form" if forms.length != 1 confirm_form = forms[0] # click confirm button = confirm_form.( :value => 'Confirm Bid' ) raise BidderError, "Couldn't find confirm bid button" unless confirm_response = confirm_form.submit( ) confirm_response end |
#login ⇒ Object
log user in with credentials. returns boolean representing success
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/beway/bidder.rb', line 29 def login ebay_home_page = @agent.get(EBAY_HOME) sign_in_link = ebay_home_page.link_with( :text => 'Sign in' ) raise BidderError, "Couldn't find sign in link" unless sign_in_link login_page = sign_in_link.click handle_login_page(login_page) return @logged_in end |