lita-onewheel-finance 0.4.0 → 0.7.2

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: 705648b5037494e24e403eff8e89f00041e4df73fe32a1948b2a4483a07360ee
4
+ data.tar.gz: cd5a3b83ae4a98f140d824061c2433146481cec08ce0879c0cd668055cb3d043
5
5
  SHA512:
6
- metadata.gz: c45a1b428948933a9680df0b80cdd1686939484e4819f0cc9a2c22046029c4a61d1337eea10586918a0721cc06d5151c6f6a3788310dfd7fe4cc33a318bbd726
7
- data.tar.gz: 1051b4205d68f27c59875ef6ee986b78aecd9cbeb58acda85c8d420cc257562c8a3955f9d76671c104074b64e1766bc8751690f7e4f3e6869aa5cd9a2ef00df6
6
+ metadata.gz: 12fa54d511e7bc069bd475d9de53bdae53aa0990fd4790c794b50d36aeaec2f93b1594c609bd5d58de9ab1c5d4b1e99b33814bbb066537b961937196e4f3ccab
7
+ data.tar.gz: 965db07646a717a3adf927a7c94897ad2c21c93d3e5bc691cb173ca3db1d204eccbc5e4958fcfbc7a73277115e849873fffd90897420321f7dfb337b4f9ef148
@@ -0,0 +1,68 @@
1
+ class AlphaVantageQuote
2
+ attr_reader :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent, :exchange, :error, :name, :message, :is_index
3
+ attr_accessor :symbol
4
+
5
+ def initialize(symbol, apikey)
6
+ # Lita.logger.debug "parsing: #{json_blob}"
7
+ # hash = JSON.parse(json_blob)
8
+ @symbol = symbol
9
+ @apikey = apikey
10
+ @is_index = false
11
+
12
+ if @symbol[0] == '^'
13
+ @is_index = true
14
+ end
15
+
16
+ response = call_api
17
+ hash = JSON.parse response
18
+
19
+ quote = hash["Global Quote"]
20
+
21
+ quote.keys.each do |key|
22
+ case key
23
+ when "01. symbol"
24
+ @symbol = quote[key]
25
+ when "02. open"
26
+ @open = self.fix_number quote[key]
27
+ when "03. high"
28
+ @high = self.fix_number quote[key]
29
+ when "04. low"
30
+ @low = self.fix_number quote[key]
31
+ when "05. price"
32
+ @price = self.fix_number quote[key]
33
+ when "06. volume"
34
+ @volume = quote[key]
35
+ when "07. latest trading day"
36
+ @trading_day = quote[key]
37
+ when "08. previous close"
38
+ @prev_close = self.fix_number quote[key]
39
+ when "09. change"
40
+ @change = self.fix_number quote[key]
41
+ when "10. change percent"
42
+ @change_percent = self.fix_number quote[key]
43
+ end
44
+ end
45
+ end
46
+
47
+ def fix_number(price_str)
48
+ price_str.to_f.round(2)
49
+ end
50
+
51
+ def is_index?
52
+ @is_index
53
+ end
54
+
55
+ def call_api
56
+ url = "https://www.alphavantage.co/query"
57
+ params = {function: 'GLOBAL_QUOTE', symbol: @symbol, apikey: @apikey}
58
+ Lita.logger.debug "#{url} #{params.inspect}"
59
+ response = RestClient.get url, {params: params}
60
+
61
+ # if @exchange
62
+ # params[:stock_exchange] = @exchange
63
+ # end
64
+
65
+ Lita.logger.debug "response: #{response}"
66
+ response
67
+ end
68
+ end
@@ -0,0 +1,43 @@
1
+ module IrcColors
2
+ prefix = "\x03"
3
+ @white = "#{prefix}00"
4
+ @black = "#{prefix}01"
5
+ @blue = "#{prefix}02"
6
+ @green = "#{prefix}03"
7
+ @red = "#{prefix}04"
8
+ @brown = "#{prefix}05"
9
+ @purple = "#{prefix}06"
10
+ @orange = "#{prefix}07"
11
+ @yellow = "#{prefix}08"
12
+ @lime = "#{prefix}09"
13
+ @teal = "#{prefix}10"
14
+ @aqua = "#{prefix}11"
15
+ @royal = "#{prefix}12"
16
+ @pink = "#{prefix}13"
17
+ @grey = "#{prefix}14"
18
+ @silver = "#{prefix}15"
19
+ @reset = prefix
20
+
21
+ class << self
22
+ attr_reader :white,
23
+ :black,
24
+ :blue,
25
+ :green,
26
+ :red,
27
+ :brown,
28
+ :purple,
29
+ :orange,
30
+ :yellow,
31
+ :lime,
32
+ :teal,
33
+ :aqua,
34
+ :royal,
35
+ :pink,
36
+ :grey,
37
+ :silver,
38
+ :reset
39
+ end
40
+
41
+ def initialize
42
+ end
43
+ end
@@ -1,238 +1,73 @@
1
+ require_relative 'irc_colors'
2
+ require_relative 'alphavantage_quote'
3
+ require_relative 'worldtradedata_quote'
4
+ require_relative 'yahoo_quote'
1
5
  require 'rest-client'
2
6
 
3
- class IrcColors
4
- prefix = "\x03"
5
- @white = "#{prefix}00"
6
- @black = "#{prefix}01"
7
- @blue = "#{prefix}02"
8
- @green = "#{prefix}03"
9
- @red = "#{prefix}04"
10
- @brown = "#{prefix}05"
11
- @purple = "#{prefix}06"
12
- @orange = "#{prefix}07"
13
- @yellow = "#{prefix}08"
14
- @lime = "#{prefix}09"
15
- @teal = "#{prefix}10"
16
- @aqua = "#{prefix}11"
17
- @royal = "#{prefix}12"
18
- @pink = "#{prefix}13"
19
- @grey = "#{prefix}14"
20
- @silver = "#{prefix}15"
21
- @reset = prefix
22
-
23
- class << self
24
- attr_reader :white, :black, :blue, :green, :red, :brown, :purple, :orange, :yellow, :lime, :teal, :aqua, :royal, :pink, :grey, :silver, :reset
25
- end
26
-
27
- def initialize
28
- end
29
- end
30
-
31
- class AlphaVantageQuote
32
- attr_reader :symbol, :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent
33
-
34
- def initialize(json_blob)
35
- Lita.logger.debug "parsing: #{json_blob}"
36
- hash = JSON.parse(json_blob)
37
- quote = hash["Global Quote"]
38
-
39
- quote.keys.each do |key|
40
- case key
41
- when "01. symbol"
42
- @symbol = quote[key]
43
- when "02. open"
44
- @open = self.fix_number quote[key]
45
- when "03. high"
46
- @high = self.fix_number quote[key]
47
- when "04. low"
48
- @low = self.fix_number quote[key]
49
- when "05. price"
50
- @price = self.fix_number quote[key]
51
- when "06. volume"
52
- @volume = quote[key]
53
- when "07. latest trading day"
54
- @trading_day = quote[key]
55
- when "08. previous close"
56
- @prev_close = self.fix_number quote[key]
57
- when "09. change"
58
- @change = self.fix_number quote[key]
59
- when "10. change percent"
60
- @change_percent = self.fix_number quote[key]
61
- end
62
- end
63
- end
64
-
65
- def fix_number(price_str)
66
- price_str.to_f.round(2)
67
- end
68
- end
69
-
70
- class WorldTradeDataQuote
71
- attr_reader :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent, :exchange, :error, :name
72
- attr_accessor :symbol
73
-
74
- def initialize(symbol, api_key)
75
- @base_uri = 'https://api.worldtradingdata.com/api/v1'
76
- @symbol = symbol
77
- @api_key = api_key
78
-
79
- self.call_api
80
-
81
- hash = JSON.parse(@response)
82
-
83
- # We couldn't find the stock. Let's look for it real quick.
84
- if hash['Message'].to_s.include? 'Error'
85
- @error = true
86
- self.run_search
87
- self.call_api
88
- hash = JSON.parse(@response)
89
- @error = false
90
- else
91
- @error = false
92
- end
93
-
94
- quote = hash['data'][0]
95
-
96
- quote.keys.each do |key|
97
- case key
98
- when "symbol"
99
- @symbol = quote[key]
100
- when "price_open"
101
- @open = self.fix_number quote[key]
102
- when "day_high"
103
- @high = self.fix_number quote[key]
104
- when "day_low"
105
- @low = self.fix_number quote[key]
106
- when "price"
107
- @price = self.fix_number quote[key]
108
- when "volume"
109
- @volume = quote[key].to_i
110
- when "last_trade_time"
111
- @trading_day = quote[key]
112
- when "08. previous close"
113
- @prev_close = self.fix_number quote[key]
114
- when "day_change"
115
- @change = self.fix_number quote[key]
116
- when "change_pct"
117
- @change_percent = self.fix_number quote[key]
118
- when 'stock_exchange_short'
119
- @exchange = quote[key].sub /NYSEARCA/, 'NYSE'
120
- when 'name'
121
- @name = quote[key]
122
- end
123
- end
124
- end
125
-
126
- # Let's see what we can get from the api.
127
- def call_api
128
- url = "#{@base_uri}/stock"
129
- params = {symbol: @symbol, api_token: @api_key}
130
-
131
- Lita.logger.debug "call_api: #{url} #{params.inspect}"
132
-
133
- @response = RestClient.get url, {params: params}
134
-
135
- Lita.logger.debug "response: #{@response}"
136
- end
137
-
138
- def run_search
139
- url = "#{@base_uri}/stock_search"
140
- params = {search_term: @symbol,
141
- search_by: 'symbol,name',
142
- stock_exchange: 'NASDAQ,NYSE',
143
- limit: 5,
144
- page: 1,
145
- api_token: @api_key
146
- }
147
-
148
- Lita.logger.debug "run_search: #{url} #{params.inspect}"
149
-
150
- response = RestClient.get url, {params: params}
151
-
152
- Lita.logger.debug "response: #{@response}"
153
- result = JSON.parse(response)
154
-
155
- if result['total_returned'] == 1
156
- @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
208
- end
209
- end
210
-
211
- def fix_number(price_str)
212
- price_str.to_f.round(2)
213
- end
214
- end
215
-
216
7
  module Lita
217
8
  module Handlers
218
9
  class OnewheelFinance < Handler
219
10
  config :apikey, required: true
220
- route /quote\s+(.+)/i, :handle_quote, command: true
11
+ config :mode, default: 'irc'
12
+ config :handler, default: 'alphavantage'
13
+ route /qu*o*t*e*\s+(.+)/i, :handle_quote, command: true
14
+ # route /q2\s+(.+)/i, :handle_alphavantage, command: true
221
15
 
222
16
  def handle_quote(response)
223
- stock = handle_world_trade_data response.matches[0][0]
17
+ stock = nil
18
+ if config.handler == 'worldtradedata'
19
+ stock = handle_world_trade_data response.matches[0][0]
20
+ elsif config.handler == 'alphavantage'
21
+ stock = handle_alphavantage response.matches[0][0]
22
+ elsif config.handler == 'yahoo'
23
+ stock = handle_yahoo response.matches[0][0]
24
+ else
25
+ Lita.logger.error "Unknown/missing config.handler #{config.handler}. Try 'worldtradedata' or 'alphavantage'"
26
+ return
27
+ end
224
28
 
29
+ # Continue!
225
30
  if stock.error
226
- str = "`#{stock.symbol}` not found on any stock exchange."
31
+ if stock.message
32
+ str = stock.message
33
+ else
34
+ str = "`#{stock.symbol}` not found on any stock exchange."
35
+ end
227
36
  else
228
- str = "#{IrcColors::grey}#{stock.exchange} - #{IrcColors::reset}#{stock.symbol}: #{IrcColors::blue}$#{stock.price}#{IrcColors::reset} "
229
- if stock.change >= 0
230
- # if irc
231
- str += "#{IrcColors::green} ⬆$#{stock.change}#{IrcColors::reset}, #{IrcColors::green}#{stock.change_percent}%#{IrcColors::reset} "
232
- str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
37
+ dollar_sign = '$'
38
+ if stock.is_index?
39
+ dollar_sign = ''
40
+ end
41
+
42
+ # IRC mode
43
+ if config.mode == 'irc'
44
+ str = "#{IrcColors::grey}#{stock.exchange} - #{IrcColors::reset}#{stock.symbol}: #{IrcColors::blue}#{dollar_sign}#{"%.2f" % stock.price}#{IrcColors::reset} "
45
+ if stock.change >= 0
46
+ # if irc
47
+ str += "#{IrcColors::green} ⬆#{dollar_sign}#{"%.2f" % stock.change}#{IrcColors::reset}, #{IrcColors::green}#{stock.change_percent}%#{IrcColors::reset} "
48
+ if stock.name
49
+ str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
50
+ end
51
+ else
52
+ str += "#{IrcColors::red} ↯#{dollar_sign}#{"%.2f" % stock.change}#{IrcColors::reset}, #{IrcColors::red}#{stock.change_percent}%#{IrcColors::reset} "
53
+ if stock.name
54
+ str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
55
+ end
56
+ end
233
57
  else
234
- str += "#{IrcColors::red} ↯$#{stock.change}#{IrcColors::reset}, #{IrcColors::red}#{stock.change_percent}%#{IrcColors::reset} "
235
- str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
58
+ str = "#{stock.exchange} - #{stock.symbol}: #{dollar_sign}#{"%.2f" % stock.price} "
59
+ if stock.change >= 0
60
+ # if irc
61
+ str += " :arrow_up:#{dollar_sign}#{"%.2f" % stock.change}, :heavy_plus_sign:#{stock.change_percent}% "
62
+ if stock.name
63
+ str += "(#{stock.name})"
64
+ end
65
+ else
66
+ str += " :chart_with_downwards_trend:#{dollar_sign}#{"%.2f" % stock.change}, :heavy_minus_sign:#{stock.change_percent}% "
67
+ if stock.name
68
+ str += "(#{stock.name})"
69
+ end
70
+ end
236
71
  end
237
72
  end
238
73
 
@@ -248,12 +83,13 @@ module Lita
248
83
  end
249
84
 
250
85
  # 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
86
+ def handle_alphavantage(symbol)
87
+ stock = AlphaVantageQuote.new symbol, config.apikey
88
+ end
89
+
90
+ # welp
91
+ def handle_yahoo(symbol)
92
+ stock = YahooQuote.new symbol, config.apikey
257
93
  end
258
94
 
259
95
  Lita.register_handler(self)
@@ -0,0 +1,128 @@
1
+ class WorldTradeDataQuote
2
+ attr_reader :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent, :exchange, :error, :name, :message, :is_index
3
+ attr_accessor :symbol
4
+
5
+ def initialize(symbol, api_key)
6
+ @base_uri = 'https://api.worldtradingdata.com/api/v1'
7
+ @symbol = symbol
8
+ @api_key = api_key
9
+
10
+ if @symbol[':']
11
+ (@exchange, @symbol) = @symbol.split /:/
12
+ end
13
+
14
+ if @symbol['^']
15
+ @is_index = true
16
+ else
17
+ @is_index = false
18
+ end
19
+
20
+ self.call_api
21
+
22
+ hash = JSON.parse(@response)
23
+
24
+ # We couldn't find the stock. Let's look for it real quick.
25
+ if hash['Message'].to_s.include? 'Error'
26
+ @error = true
27
+ self.run_search
28
+
29
+ if @message
30
+ @error = true
31
+ return
32
+ else
33
+ self.call_api
34
+ hash = JSON.parse(@response)
35
+ @error = false
36
+ end
37
+ else
38
+ @error = false
39
+ end
40
+
41
+ unless hash['data']
42
+ @message = "Error getting data for #{@symbol}"
43
+ @error = true
44
+ return
45
+ end
46
+
47
+ quote = hash['data'][0]
48
+
49
+ quote.keys.each do |key|
50
+ case key
51
+ when "symbol"
52
+ @symbol = quote[key]
53
+ when "price_open"
54
+ @open = self.fix_number quote[key]
55
+ when "day_high"
56
+ @high = self.fix_number quote[key]
57
+ when "day_low"
58
+ @low = self.fix_number quote[key]
59
+ when "price"
60
+ @price = self.fix_number quote[key]
61
+ when "volume"
62
+ @volume = quote[key].to_i
63
+ when "last_trade_time"
64
+ @trading_day = quote[key]
65
+ when "08. previous close"
66
+ @prev_close = self.fix_number quote[key]
67
+ when "day_change"
68
+ @change = self.fix_number quote[key]
69
+ when "change_pct"
70
+ @change_percent = self.fix_number quote[key]
71
+ when 'stock_exchange_short'
72
+ @exchange = quote[key].sub /NYSEARCA/, 'NYSE'
73
+ when 'name'
74
+ @name = quote[key]
75
+ end
76
+ end
77
+
78
+ def is_index?
79
+ is_index
80
+ end
81
+ end
82
+
83
+ # Let's see what we can get from the api.
84
+ def call_api
85
+ url = "#{@base_uri}/stock"
86
+ params = {symbol: @symbol, api_token: @api_key}
87
+
88
+ if @exchange
89
+ params[:stock_exchange] = @exchange
90
+ end
91
+
92
+ Lita.logger.debug "call_api: #{url} #{params.inspect}"
93
+
94
+ @response = RestClient.get url, {params: params}
95
+
96
+ Lita.logger.debug "response: #{@response}"
97
+ end
98
+
99
+ def run_search
100
+ url = "#{@base_uri}/stock_search"
101
+ params = {search_term: @symbol,
102
+ search_by: 'symbol,name',
103
+ stock_exchange: 'NASDAQ,NYSE',
104
+ limit: 5,
105
+ page: 1,
106
+ api_token: @api_key
107
+ }
108
+
109
+ Lita.logger.debug "run_search: #{url} #{params.inspect}"
110
+
111
+ response = RestClient.get url, {params: params}
112
+
113
+ Lita.logger.debug "response: #{response}"
114
+ result = JSON.parse(response)
115
+
116
+ if result['total_returned'] == 1
117
+ @symbol = result['data'][0]['symbol']
118
+ elsif result['total_returned'].to_i > 1
119
+ Lita.logger.debug "many search results: #{result.inspect}"
120
+ x = result['data'].map { |k| k.values[0] }
121
+ @message = "`#{symbol}` not found, did you mean one of #{x.join(', ')}?"
122
+ end
123
+ end
124
+
125
+ def fix_number(price_str)
126
+ price_str.to_f.round(2)
127
+ end
128
+ end