Sha256: 96115cbda0cf992d69a3accb2edcda50a193fe6bdf0a47a1f6258e3ca07690fb

Contents?: true

Size: 1.1 KB

Versions: 11

Compression:

Stored size: 1.1 KB

Contents

begin
  require 'yahoofinance' # require yahoofinance gem
rescue LoadError
  puts "You must install the yahoofinance gem in order to use the Stock plugin"
  raise $!
end

class Robut::Plugin::Stock
  include Robut::Plugin

  desc "stock <symbol> - Returns a stock data from Yahoo Finance"
  match /^stock (.*)/, :sent_to_me => true do |phrase|
    stock_data = get_stock_data(format_stock_symbols(phrase))
    reply format_reply(stock_data)
  end

  private

  def format_reply(stock_data)
    r = []
    stock_data.keys.sort.each do |sym|
      sd = stock_data[sym]
      r << "#{sym}: #{format_number(sd.changePoints)} / #{format_number(sd.changePercent)}%,\tbid: #{pad_number(sd.bid)},\task: #{pad_number(sd.ask)},\tprevious close: #{pad_number(sd.previousClose)}"
    end
    r.join("\n")
  end

  def format_number(n)
    n > 0 ? "+" + pad_number(n) : pad_number(n)
  end

  def pad_number(n)
    sprintf("%.2f", n)
  end

  def format_stock_symbols(phrase)
    phrase.downcase.split(/[\s,;]+/).join(',')
  end

  def get_stock_data(symbols)
    YahooFinance::get_quotes(YahooFinance::StandardQuote, symbols)
  end


end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
sclemmer-robut-0.6.3 lib/robut/plugin/stock.rb
sclemmer-robut-0.6.2 lib/robut/plugin/stock.rb
sclemmer-robut-0.6.1 lib/robut/plugin/stock.rb
sclemmer-robut-0.6.0 lib/robut/plugin/stock.rb
sclemmer-robut-0.5.4 lib/robut/plugin/stock.rb
sclemmer-robut-0.5.3 lib/robut/plugin/stock.rb
sclemmer-robut-0.5.2 lib/robut/plugin/stock.rb
robut-0.5.2 lib/robut/plugin/stock.rb
robut-0.5.1 lib/robut/plugin/stock.rb
robut-0.5.0 lib/robut/plugin/stock.rb
robut-0.4.0 lib/robut/plugin/stock.rb