lita-onewheel-finance 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 316a69ee9c4dd4c0e88009906080be2d326bb4cac5723526956e29c569466607
4
- data.tar.gz: e9bee0f2b4e414dcc58391707f61db410d39062ecda51ee94f185e5f613160c6
3
+ metadata.gz: 86924c83a1b54b7034f09552a2ff66c46723de2baa4c3b061efbdfc9bcb39d70
4
+ data.tar.gz: 4b2908349083d6dc6c740fd5c2ddc4b18e7cd90d6180fc672644c09be07530cc
5
5
  SHA512:
6
- metadata.gz: c45a1b428948933a9680df0b80cdd1686939484e4819f0cc9a2c22046029c4a61d1337eea10586918a0721cc06d5151c6f6a3788310dfd7fe4cc33a318bbd726
7
- data.tar.gz: 1051b4205d68f27c59875ef6ee986b78aecd9cbeb58acda85c8d420cc257562c8a3955f9d76671c104074b64e1766bc8751690f7e4f3e6869aa5cd9a2ef00df6
6
+ metadata.gz: 750a2b4e836adfaf7bea9913051037a6277c8f26787861fe14ae38389c5479c5e97e8412a9a1cd3251d4c6ad451914cf42776c400d1e97babf88adcdcdbfb4d9
7
+ data.tar.gz: d67f291c283fc66939cb3a3c59b2954247f7d807c4ba208c8beda96bc4ac6c5c504823dae63f31d944f1f43a6a3cbb51ba40f718a31f39eeb15bc725dd5aed6f
@@ -68,7 +68,7 @@ class AlphaVantageQuote
68
68
  end
69
69
 
70
70
  class WorldTradeDataQuote
71
- attr_reader :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent, :exchange, :error, :name
71
+ attr_reader :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent, :exchange, :error, :name, :message
72
72
  attr_accessor :symbol
73
73
 
74
74
  def initialize(symbol, api_key)
@@ -76,6 +76,10 @@ class WorldTradeDataQuote
76
76
  @symbol = symbol
77
77
  @api_key = api_key
78
78
 
79
+ if @symbol[':']
80
+ (@exchange, @symbol) = @symbol.split /:/
81
+ end
82
+
79
83
  self.call_api
80
84
 
81
85
  hash = JSON.parse(@response)
@@ -84,13 +88,25 @@ class WorldTradeDataQuote
84
88
  if hash['Message'].to_s.include? 'Error'
85
89
  @error = true
86
90
  self.run_search
87
- self.call_api
88
- hash = JSON.parse(@response)
89
- @error = false
91
+
92
+ if @message
93
+ @error = true
94
+ return
95
+ else
96
+ self.call_api
97
+ hash = JSON.parse(@response)
98
+ @error = false
99
+ end
90
100
  else
91
101
  @error = false
92
102
  end
93
103
 
104
+ unless hash['data']
105
+ @message = "Error getting data for #{@symbol}"
106
+ @error = true
107
+ return
108
+ end
109
+
94
110
  quote = hash['data'][0]
95
111
 
96
112
  quote.keys.each do |key|
@@ -128,6 +144,10 @@ class WorldTradeDataQuote
128
144
  url = "#{@base_uri}/stock"
129
145
  params = {symbol: @symbol, api_token: @api_key}
130
146
 
147
+ if @exchange
148
+ params[:stock_exchange] = @exchange
149
+ end
150
+
131
151
  Lita.logger.debug "call_api: #{url} #{params.inspect}"
132
152
 
133
153
  @response = RestClient.get url, {params: params}
@@ -149,62 +169,15 @@ class WorldTradeDataQuote
149
169
 
150
170
  response = RestClient.get url, {params: params}
151
171
 
152
- Lita.logger.debug "response: #{@response}"
172
+ Lita.logger.debug "response: #{response}"
153
173
  result = JSON.parse(response)
154
174
 
155
175
  if result['total_returned'] == 1
156
176
  @symbol = result['data'][0]['symbol']
157
- end
158
- end
159
-
160
- def fix_number(price_str)
161
- price_str.to_f.round(2)
162
- end
163
- end
164
-
165
- class WorldTradeDataSearch
166
- attr_reader :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent, :exchange, :error, :name
167
-
168
- def initialize(json_blob)
169
- Lita.logger.debug "parsing: #{json_blob}"
170
- hash = JSON.parse(json_blob)
171
-
172
- if hash['Message'].to_s.include? 'Error'
173
- @error = true
174
- return
175
- else
176
- @error = false
177
- end
178
-
179
- quote = hash['data'][0]
180
-
181
- quote.keys.each do |key|
182
- case key
183
- when "symbol"
184
- @symbol = quote[key]
185
- when "price_open"
186
- @open = self.fix_number quote[key]
187
- when "day_high"
188
- @high = self.fix_number quote[key]
189
- when "day_low"
190
- @low = self.fix_number quote[key]
191
- when "price"
192
- @price = self.fix_number quote[key]
193
- when "volume"
194
- @volume = quote[key].to_i
195
- when "last_trade_time"
196
- @trading_day = quote[key]
197
- when "08. previous close"
198
- @prev_close = self.fix_number quote[key]
199
- when "day_change"
200
- @change = self.fix_number quote[key]
201
- when "change_pct"
202
- @change_percent = self.fix_number quote[key]
203
- when 'stock_exchange_short'
204
- @exchange = quote[key].sub /NYSEARCA/, 'NYSE'
205
- when 'name'
206
- @name = quote[key]
207
- end
177
+ elsif result['total_returned'].to_i > 1
178
+ Lita.logger.debug "many search results: #{result.inspect}"
179
+ x = result['data'].map { |k| k.values[0] }
180
+ @message = "`#{symbol}` not found, did you mean one of #{x.join(', ')}?"
208
181
  end
209
182
  end
210
183
 
@@ -217,13 +190,17 @@ module Lita
217
190
  module Handlers
218
191
  class OnewheelFinance < Handler
219
192
  config :apikey, required: true
220
- route /quote\s+(.+)/i, :handle_quote, command: true
193
+ route /qu*o*t*e*\s+(.+)/i, :handle_quote, command: true
221
194
 
222
195
  def handle_quote(response)
223
196
  stock = handle_world_trade_data response.matches[0][0]
224
197
 
225
198
  if stock.error
226
- str = "`#{stock.symbol}` not found on any stock exchange."
199
+ if stock.message
200
+ str = stock.message
201
+ else
202
+ str = "`#{stock.symbol}` not found on any stock exchange."
203
+ end
227
204
  else
228
205
  str = "#{IrcColors::grey}#{stock.exchange} - #{IrcColors::reset}#{stock.symbol}: #{IrcColors::blue}$#{stock.price}#{IrcColors::reset} "
229
206
  if stock.change >= 0
@@ -248,13 +225,13 @@ module Lita
248
225
  end
249
226
 
250
227
  # deprecated for now
251
- def handle_alphavantage
252
- url = "https://www.alphavantage.co/query"
253
- params = {function: 'GLOBAL_QUOTE', symbol: response.matches[0][0], apikey: config.apikey}
254
- Lita.logger.debug "#{url} #{params.inspect}"
255
- resp = RestClient.get url, {params: params}
256
- stock = GlobalQuote.new resp
257
- end
228
+ #def handle_alphavantage
229
+ # url = "https://www.alphavantage.co/query"
230
+ # params = {function: 'GLOBAL_QUOTE', symbol: response.matches[0][0], apikey: config.apikey}
231
+ # Lita.logger.debug "#{url} #{params.inspect}"
232
+ # resp = RestClient.get url, {params: params}
233
+ # stock = GlobalQuote.new resp
234
+ #end
258
235
 
259
236
  Lita.register_handler(self)
260
237
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-onewheel-finance"
3
- spec.version = "0.4.0"
3
+ spec.version = "0.5.0"
4
4
  spec.authors = ["Andrew Kreps"]
5
5
  spec.email = ["andrew.kreps@gmail.com"]
6
6
  spec.description = "Wee li'l stock quote bot"
@@ -25,12 +25,24 @@ describe Lita::Handlers::OnewheelFinance, lita_handler: true do
25
25
  expect(replies.last).to include('(Consumer Staples Select Sector SPDR Fund)')
26
26
  end
27
27
 
28
- it 'errors' do
29
- mock_up 'worldtradedata-error'
30
- send_command 'quote in'
31
- expect(replies.last).to eq('`in` not found on any stock exchange.')
28
+ it 'nasdaq:lulu' do
29
+ mock_up 'worldtradedata-quote-up'
30
+ send_command 'q nasdaq:lulu'
31
+ expect(replies.last).to include("\u000314NASDAQ - \u0003LULU: \u000302$233.01\u0003")
32
32
  end
33
33
 
34
+ #it 'errors' do
35
+ # mock_up 'worldtradedata-error'
36
+ # send_command 'quote in'
37
+ # expect(replies.last).to eq('`in` not found on any stock exchange.')
38
+ #end
39
+
40
+
41
+ # This doesn't exist
42
+ #it 'TADAWUL:2222' do
43
+ # mock_up 'worldtradedata-TADAWUL-2222.json'
44
+ #
45
+ #end
34
46
  #it 'searches with 1 result' do
35
47
  # mock_up 'worldtradedata-search-1'
36
48
  # send_command 'quote nike'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-onewheel-finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kreps
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-02 00:00:00.000000000 Z
11
+ date: 2020-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita