Top Level Namespace

Defined Under Namespace

Modules: AuctionInterface Classes: AutoBidder, Bigdeal, BrowserInterface

Instance Method Summary collapse

Instance Method Details

#ensure_option_is_present(options, name, parser) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/auto_bidder.rb', line 114

def ensure_option_is_present(options, name, parser)
  if !options.has_key? name
    puts "Option #{name} is required"
    puts parser
    exit 1
  end
end

#parse_command_lineObject



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/auto_bidder.rb', line 28

def parse_command_line
  # This hash will hold all of the options
  # parsed from the command-line by
  # OptionParser.
  options = {}

  optparse = OptionParser.new do|opts|
    # Set a banner, displayed at the top
    # of the help screen.
    opts.banner = "Usage: auto-bidder [options] URL"

    # Define the options, and what they do
    options[:verbose] = false
    opts.on('-v', '--verbose', 'Be more verbose') do
      options[:verbose] = true
    end
    
    opts.on("-x", "--max-price PRICE", Float, 'Maximum price you are willing to pay for the auction item') do |max_price|
      options[:max_price] = max_price
    end
    
    opts.on("-n", "--min-price [PRICE]", Float, 'Minimum price the auction item must reach before bidding starts') do |min_price|
      options[:min_price] = min_price || 0.0
    end
    
    opts.on("-c", "--bid-cost COST", Float, 'How much each bid actually costs you') do |bid_cost|
      options[:bid_cost] = bid_cost
    end
    
    opts.on("-u", "--username USER", 'Username of your account for the auction site') do |username|
      options[:username] = username
    end
    
    opts.on("-s", "--bid-seconds [SECONDS]", Integer, 'How many seconds remaining on the clock before a bid is placed') do |bid_secs|
      options[:bid_secs] = bid_secs || 1
    end
    
    # This displays the help screen, all programs are
    # assumed to have this option.
    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
  end

  # Parse the command-line. Remember there are two forms
  # of the parse method. The 'parse' method simply parses
  # ARGV, while the 'parse!' method parses ARGV and removes
  # any options found there, as well as any parameters for
  # the options. What's left is the list of files to resize.
  optparse.parse! ARGV
  
  ensure_option_is_present options, :max_price, optparse
  ensure_option_is_present options, :bid_cost, optparse
  ensure_option_is_present options, :username, optparse
  
  url = ARGV[0]
  unless url
    puts "URL must be given"
    puts optparse
    exit 1
  end
  
  ARGV.clear
  
  if options[:username]
    print "Auction site password:"
    
    # turn off terminal echo briefly
    begin
      system "stty -echo"
      options[:password] = gets.chomp
      puts # carriage return so the first log statement isn't printed on the same line as the password prompt
    ensure
      system "stty echo"
    end
  end
  
  $log = Logger.new(STDOUT)
  $log.level = options[:verbose] ? Logger::DEBUG : Logger::INFO

  $log.debug "Verbose logging turned on" if $log.debug?
  
  [url, options]
end