Class: WorldTradeDataQuote

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, api_key) ⇒ WorldTradeDataQuote

Returns a new instance of WorldTradeDataQuote.



5
6
7
8
9
10
11
12
13
14
15
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
76
77
78
79
80
81
# 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

Instance Attribute Details

#changeObject (readonly)

Returns the value of attribute change.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def change
  @change
end

#change_percentObject (readonly)

Returns the value of attribute change_percent.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def change_percent
  @change_percent
end

#errorObject (readonly)

Returns the value of attribute error.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def error
  @error
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def exchange
  @exchange
end

#highObject (readonly)

Returns the value of attribute high.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def high
  @high
end

#is_indexObject (readonly)

Returns the value of attribute is_index.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def is_index
  @is_index
end

#lowObject (readonly)

Returns the value of attribute low.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def low
  @low
end

#messageObject (readonly)

Returns the value of attribute message.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def name
  @name
end

#openObject (readonly)

Returns the value of attribute open.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def open
  @open
end

#prev_closeObject (readonly)

Returns the value of attribute prev_close.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def prev_close
  @prev_close
end

#priceObject (readonly)

Returns the value of attribute price.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def price
  @price
end

#symbolObject

Returns the value of attribute symbol.



3
4
5
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 3

def symbol
  @symbol
end

#trading_dayObject (readonly)

Returns the value of attribute trading_day.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def trading_day
  @trading_day
end

#volumeObject (readonly)

Returns the value of attribute volume.



2
3
4
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 2

def volume
  @volume
end

Instance Method Details

#call_apiObject

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



125
126
127
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 125

def fix_number(price_str)
  price_str.to_f.round(2)
end

#is_index?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 78

def is_index?
  is_index
end

#run_searchObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lita/handlers/worldtradedata_quote.rb', line 99

def run_search
  url = "#{@base_uri}/stock_search"
  params = {search_term: @symbol,
            search_by: 'symbol,name',
            stock_exchange: 'NASDAQ,NYSE',
            limit: 5,
            page: 1,
            api_token: @api_key
  }

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

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

  Lita.logger.debug "response: #{response}"
  result = JSON.parse(response)

  if result['total_returned'] == 1
    @symbol = result['data'][0]['symbol']
  elsif result['total_returned'].to_i > 1
    Lita.logger.debug "many search results: #{result.inspect}"
    x = result['data'].map { |k| k.values[0] }
    @message = "`#{symbol}` not found, did you mean one of #{x.join(', ')}?"
  end
end