class WorldTradeDataQuote

Attributes

change[R]
change_percent[R]
error[R]
exchange[R]
high[R]
is_index[R]
low[R]
message[R]
name[R]
open[R]
prev_close[R]
price[R]
symbol[RW]
trading_day[R]
volume[R]

Public Class Methods

new(symbol, api_key) click to toggle source
# File lib/lita/handlers/worldtradedata_quote.rb, line 5
def initialize(symbol, api_key)
  @base_uri = 'https://api.worldtradingdata.com/api/v1'
  @symbol = symbol
  @api_key = api_key

  if @symbol[':']
    (@exchange, @symbol) = @symbol.split /:/
  end

  if @symbol['^']
    @is_index = true
  else
    @is_index = false
  end

  self.call_api

  hash = JSON.parse(@response)

  # We couldn't find the stock.  Let's look for it real quick.
  if hash['Message'].to_s.include? 'Error'
    @error = true
    self.run_search

    if @message
      @error = true
      return
    else
      self.call_api
      hash = JSON.parse(@response)
      @error = false
    end
  else
    @error = false
  end

  unless hash['data']
    @message = "Error getting data for #{@symbol}"
    @error = true
    return
  end

  quote = hash['data'][0]

  quote.keys.each do |key|
    case key
    when "symbol"
      @symbol = quote[key]
    when "price_open"
      @open = self.fix_number quote[key]
    when "day_high"
      @high = self.fix_number quote[key]
    when "day_low"
      @low = self.fix_number quote[key]
    when "price"
      @price = self.fix_number quote[key]
    when "volume"
      @volume = quote[key].to_i
    when "last_trade_time"
      @trading_day = quote[key]
    when "08. previous close"
      @prev_close = self.fix_number quote[key]
    when "day_change"
      @change = self.fix_number quote[key]
    when "change_pct"
      @change_percent = self.fix_number quote[key]
    when 'stock_exchange_short'
      @exchange = quote[key].sub /NYSEARCA/, 'NYSE'
    when 'name'
      @name = quote[key]
    end
  end

  def is_index?
    is_index
  end
end

Public Instance Methods

call_api() click to toggle source

Let's see what we can get from the api.

# File lib/lita/handlers/worldtradedata_quote.rb, line 84
def call_api
  url = "#{@base_uri}/stock"
  params = {symbol: @symbol, api_token: @api_key}

  if @exchange
    params[:stock_exchange] = @exchange
  end

  Lita.logger.debug "call_api: #{url} #{params.inspect}"

  @response = RestClient.get url, {params: params}

  Lita.logger.debug "response: #{@response}"
end
fix_number(price_str) click to toggle source
# File lib/lita/handlers/worldtradedata_quote.rb, line 125
def fix_number(price_str)
  price_str.to_f.round(2)
end
is_index?() click to toggle source
# File lib/lita/handlers/worldtradedata_quote.rb, line 78
def is_index?
  is_index
end