class StockQuoteCLI::CLI

Public Instance Methods

change(*symbols) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 22
def change(*symbols)
  output_info(symbols, :change)
end
high(*symbols) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 12
def high(*symbols)
  output_info(symbols, :days_high)
end
history(symbol) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 44
def history(symbol)
  history = API.stock_history(symbol, options['range'])
  output_stock_history(history, options['value'], symbol)
end
last(*symbols) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 7
def last(*symbols)
  output_info(symbols, :last_trade_price_only)
end
low(*symbols) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 17
def low(*symbols)
  output_info(symbols, :days_low)
end
open(*symbols) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 27
def open(*symbols)
  output_info(symbols, :open)
end
pclose(*symbols) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 32
def pclose(*symbols)
  output_info(symbols, :previous_close)
end
volume(*symbols) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 37
def volume(*symbols)
  output_info(symbols, :volume)
end

Private Instance Methods

format_company(company) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 103
def format_company(company)
  Text.green(company).rjust(40)
end
format_date(date) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 107
def format_date(date)
  Text.green(date).rjust(40)
end
format_price(price) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 111
def format_price(price)
  "$#{Text.number_with_commas(price)}"
end
format_title(vname, symbol) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 119
def format_title(vname, symbol)
  "#{Text.yellow(vname.upcase)} data for:".rjust(39) +
    " #{Text.yellow(symbol.upcase)}\n"
end
format_volume(volume) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 115
def format_volume(volume)
  "#{Text.number_with_commas(volume.to_i)} shares"
end
output_bad_symbol_message(symbol) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 95
def output_bad_symbol_message(symbol)
  puts Text.red("No data available for symbol: #{symbol.upcase}".rjust(28))
end
output_bad_vname_message(vname) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 99
def output_bad_vname_message(vname)
  puts Text.red("#{'Invalid value:'.rjust(28)} #{vname}")
end
output_info(symbols, vname) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 51
def output_info(symbols, vname)
  stocks = API.stocks_info(symbols)
  output_stocks_info(stocks, vname)
end
output_stock_history(history, vname, symbol) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 75
def output_stock_history(history, vname, symbol)
  return output_bad_symbol_message(symbol) if history.is_a?(StockQuote::NoDataForStockError)

  voptions = %w[open high low close volume]
  if voptions.include?(vname)
    puts "\n#{format_title(vname, symbol)}\n"
    history.each do |quote|
      date = format_date(quote.date.to_s)
      if vname == "volume"
        puts "#{date}: #{format_volume(quote.send(vname))}"
      else
        puts "#{date}: #{format_price(quote.send(vname))}"
      end
    end
  else
    output_bad_vname_message(vname)
  end
  puts
end
output_stocks_info(stocks, vname) click to toggle source
# File lib/stock_quote_cli/cli.rb, line 56
def output_stocks_info(stocks, vname)
  puts
  stocks.each do |quote|
    # Ensure that quote object has data. If not,
    # it probably represents an invalid symbol.
    if quote.open
      company = format_company(quote.name)
      if vname == :volume
        puts "#{company}: #{format_volume(quote.volume)}"
      else
        puts "#{company}: #{format_price(quote.send(vname))}"
      end
    else
      output_bad_symbol_message(quote.symbol)
    end
  end
  puts
end