Class: Lita::Handlers::OnewheelFinance

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_finance.rb

Instance Method Summary collapse

Instance Method Details

#handle_alphavantage(symbol) ⇒ Object

deprecated for now



86
87
88
# File 'lib/lita/handlers/onewheel_finance.rb', line 86

def handle_alphavantage(symbol)
 stock = AlphaVantageQuote.new symbol, config.apikey
end

#handle_quote(response) ⇒ Object

route /q2s+(.+)/i, :handle_alphavantage, command: true



16
17
18
19
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
# File 'lib/lita/handlers/onewheel_finance.rb', line 16

def handle_quote(response)
  stock = nil
  if config.handler == 'worldtradedata'
    stock = handle_world_trade_data response.matches[0][0]
  elsif config.handler == 'alphavantage'
    stock = handle_alphavantage response.matches[0][0]
  elsif config.handler == 'yahoo'
    stock = handle_yahoo response.matches[0][0]
  else
    Lita.logger.error "Unknown/missing config.handler #{config.handler}.  Try 'worldtradedata' or 'alphavantage'"
    return
  end

  # Continue!
  if stock.error
    if stock.message
      str = stock.message
    else
      str = "`#{stock.symbol}` not found on any stock exchange."
    end
  else
    dollar_sign = '$'
    if stock.is_index?
      dollar_sign = ''
    end

    # IRC mode
    if config.mode == 'irc'
      str = "#{IrcColors::grey}#{stock.exchange} - #{IrcColors::reset}#{stock.symbol}: #{IrcColors::teal}#{dollar_sign}#{"%.2f" % stock.price}#{IrcColors::reset} "
      if stock.change >= 0
        # if irc
        str += "#{IrcColors::green}#{dollar_sign}#{"%.2f" % stock.change}#{IrcColors::reset}, #{IrcColors::green}#{stock.change_percent}%#{IrcColors::reset} "
        if stock.name
          str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
        end
      else
        str += "#{IrcColors::red}#{dollar_sign}#{"%.2f" % stock.change}#{IrcColors::reset}, #{IrcColors::red}#{stock.change_percent}%#{IrcColors::reset} "
        if stock.name
          str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
        end
      end
    else
      str = "#{stock.exchange} - #{stock.symbol}: #{dollar_sign}#{"%.2f" % stock.price} "
      if stock.change >= 0
        # if irc
        str += " :arrow_up:#{dollar_sign}#{"%.2f" % stock.change}, :heavy_plus_sign:#{stock.change_percent}% "
        if stock.name
          str += "(#{stock.name})"
        end
      else
        str += " :chart_with_downwards_trend:#{dollar_sign}#{"%.2f" % stock.change}, :heavy_minus_sign:#{stock.change_percent}% "
        if stock.name
          str += "(#{stock.name})"
        end
      end
    end
  end

  response.reply str
end

#handle_world_trade_data(symbol) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/lita/handlers/onewheel_finance.rb', line 77

def handle_world_trade_data(symbol)
  stock = WorldTradeDataQuote.new symbol, config.apikey
  if stock.error
    stock.symbol = symbol
  end
  stock
end

#handle_yahoo(symbol) ⇒ Object

welp



91
92
93
# File 'lib/lita/handlers/onewheel_finance.rb', line 91

def handle_yahoo(symbol)
 stock = YahooQuote.new symbol, config.apikey
end