Class: Beway::CliRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/beway/cli_runner.rb

Overview

CliRunner

A UI for the Beway module to snipe an ebay auction

Constant Summary collapse

BID_THRESHOLD =
10

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCliRunner

Returns a new instance of CliRunner.



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

def initialize
end

Class Method Details

.startObject



12
13
14
15
# File 'lib/beway/cli_runner.rb', line 12

def self.start
  runner = self.new
  runner.run
end

Instance Method Details

#auction_from_userObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/beway/cli_runner.rb', line 87

def auction_from_user
  loop do
    begin
      auction = Auction.new(prompt_url)
      if auction.complete?
        display_auction auction
        puts
        puts "That auction is done already!  Try another, or 'exit' to quit."
        next
      end
    rescue InvalidUrlError
      puts "Sorry, we can't snipe that url."
      next
    rescue AuctionParseError
      puts "Sorry, we can't parse that url as an auction"
      next
    end

    return auction if prompt_confirm_auction(auction)
  end
end

#bid_for_auction_from_user(a) ⇒ Object



80
81
82
83
84
85
# File 'lib/beway/cli_runner.rb', line 80

def bid_for_auction_from_user(a)
  loop do
    bid_amount = prompt_bid()#a.min_bid)
    return bid_amount if prompt_confirm_bid(bid_amount)
  end
end

#display_auction(a) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/beway/cli_runner.rb', line 113

def display_auction(a)
  puts
  puts "URL:            #{a.url}"
  puts "Description:    #{a.description}"
  puts "Auction Number: #{a.auction_number}"
  puts "Current Bid:    #{a.current_bid}"
  puts "Min Bid:        #{a.min_bid || '-- bidding closed --'}"
  puts "Time Left:      #{a.time_left || '-- bidding closed --'}"
  puts "End Time:       #{a.end_time}"
end

#display_introObject



109
110
111
# File 'lib/beway/cli_runner.rb', line 109

def display_intro
  puts "Welcome to Beway Version #{VERSION}"
end

#get_user_inputObject



179
180
181
182
183
# File 'lib/beway/cli_runner.rb', line 179

def get_user_input
  s = gets
  exit if 'exit' == s.chomp.downcase
  s
end

#prompt_bid(min = nil) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/beway/cli_runner.rb', line 139

def prompt_bid(min=nil)
  print "Enter your bid for the item >> "
  bid = get_user_input.to_f
  puts
  return bid if min.nil? or bid >= min
  puts "The minimum bid for this auction is #{min}.  Try again, or type 'exit' to quit."
  prompt_bid(min)
end

#prompt_confirm_auction(a) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/beway/cli_runner.rb', line 159

def prompt_confirm_auction(a)
  display_auction(a)
  print 'Does this look like your auction? (y\n) >> '
  confirm = get_user_input
  if confirm.downcase.chr == 'y'
    true
  else
    false
  end
end

#prompt_confirm_bid(amount) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/beway/cli_runner.rb', line 148

def prompt_confirm_bid(amount)
  printf "Are you sure you want to bid %.2f? (y\\n) >> ", amount
  confirm = get_user_input
  puts
  if confirm.downcase.chr == 'y'
    true
  else
    false
  end
end

#prompt_passwordObject



129
130
131
132
133
134
135
136
137
# File 'lib/beway/cli_runner.rb', line 129

def prompt_password
  print "Enter eBay password >> "
  system "stty -echo"
  pass = get_user_input
  system "stty echo"
  puts

  pass.chomp
end

#prompt_urlObject



170
171
172
173
174
175
176
177
# File 'lib/beway/cli_runner.rb', line 170

def prompt_url
  puts
  puts "Enter an ebay auction url (or 'exit' to exit): "
  print "\n>> "
  url = get_user_input

  url.chomp
end

#prompt_usernameObject



124
125
126
127
# File 'lib/beway/cli_runner.rb', line 124

def prompt_username
  print "Enter eBay username >> "
  return get_user_input.chomp
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
73
74
75
76
77
78
# File 'lib/beway/cli_runner.rb', line 20

def run
  display_intro

  # login prep
  user = prompt_username
  pass = prompt_password
  bidder = Bidder.new(user, pass)
  puts "Logging in..."
  bidder.
  if bidder.logged_in
    puts "          ...success"
  else
    puts "Bad ebay username/password combo!  Please try again."
    exit
  end

  # bid prep
  auction = auction_from_user
  bid_amount = bid_for_auction_from_user(auction)

  # our tools
  ebay = EbayData.instance

  loop do
    display_auction auction

    seconds_to_end = ebay.seconds_to(auction.end_time)

    if seconds_to_end <= BID_THRESHOLD
      puts "Placing bid..."
      begin
        bidder.bid(auction.url, bid_amount)
        puts "           ...placed."
        puts "Sleeping til end of auction..."
        sleep ebay.seconds_to(auction.end_time).ceil
        auction.refresh_doc
        display_auction auction
        exit
      rescue BidderError => e
        puts "           ...oh no!"
        puts "There was an error placing your bid:"
        puts
        puts "  " + e.message
        puts
        puts "So sorry!"
        exit
      end
    end

    seconds = seconds_to_end.floor / 2
    seconds = 5 if seconds_to_end < (2 * BID_THRESHOLD)
    puts "Sleeping #{seconds} seconds..."
    sleep seconds

    puts "Updating auction..."
    puts
    auction.refresh_doc
  end
end