Class: Beway::Bidder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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
   = nil
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



16
17
18
# File 'lib/beway/bidder.rb', line 16

def agent
  @agent
end

#last_login_timeObject (readonly)

Returns the value of attribute last_login_time.



16
17
18
# File 'lib/beway/bidder.rb', line 16

def 
  
end

#logged_inObject (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

Parameters:

  • value

    the value to set the attribute password to.



15
16
17
# File 'lib/beway/bidder.rb', line 15

def password=(value)
  @password = value
end

#usernameObject

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

Raises:



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 (bid_response)
    bid_response = (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_button = confirm_form.button_with( :value => 'Confirm Bid' )
  raise BidderError, "Couldn't find confirm bid button" unless confirm_button
  confirm_response = confirm_form.submit( confirm_button )

  confirm_response
end

#loginObject

log user in with credentials. returns boolean representing success

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/beway/bidder.rb', line 29

def 
  ebay_home_page = @agent.get(EBAY_HOME)

   = ebay_home_page.link_with( :text => 'Sign in' )
  raise BidderError, "Couldn't find sign in link" unless 
   = .click

  ()

  return @logged_in
end