lita-onewheel-finance 0.5.0 → 0.7.3

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: 86924c83a1b54b7034f09552a2ff66c46723de2baa4c3b061efbdfc9bcb39d70
4
- data.tar.gz: 4b2908349083d6dc6c740fd5c2ddc4b18e7cd90d6180fc672644c09be07530cc
3
+ metadata.gz: af486197ad5808a1a74b9c18f7a87badf1ffa46349bb80796be2d26ee6a0f240
4
+ data.tar.gz: 979ed93c618a32f47a925e28676bf599c108f85e4c219a8daeebcc54b8a54d1f
5
5
  SHA512:
6
- metadata.gz: 750a2b4e836adfaf7bea9913051037a6277c8f26787861fe14ae38389c5479c5e97e8412a9a1cd3251d4c6ad451914cf42776c400d1e97babf88adcdcdbfb4d9
7
- data.tar.gz: d67f291c283fc66939cb3a3c59b2954247f7d807c4ba208c8beda96bc4ac6c5c504823dae63f31d944f1f43a6a3cbb51ba40f718a31f39eeb15bc725dd5aed6f
6
+ metadata.gz: 6efe6ae097efeb85946835686c30d140754f140d003267a5aa91f27959668ad9b93c4e7a4e743cdd5bf5b2447d89c444ee9c6f0259c05e1a5e7de2160156bfe3
7
+ data.tar.gz: a04f6034575fb5fea5fe6dae265c4ace8dc765546b93f13e3dca32f47d7844505a20382b736f2a374ba414657f7b93bca948ad6940ccdc0a1099614e0f579acf
@@ -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,200 +1,32 @@
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, :message
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
- if @symbol[':']
80
- (@exchange, @symbol) = @symbol.split /:/
81
- end
82
-
83
- self.call_api
84
-
85
- hash = JSON.parse(@response)
86
-
87
- # We couldn't find the stock. Let's look for it real quick.
88
- if hash['Message'].to_s.include? 'Error'
89
- @error = true
90
- self.run_search
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
100
- else
101
- @error = false
102
- end
103
-
104
- unless hash['data']
105
- @message = "Error getting data for #{@symbol}"
106
- @error = true
107
- return
108
- end
109
-
110
- quote = hash['data'][0]
111
-
112
- quote.keys.each do |key|
113
- case key
114
- when "symbol"
115
- @symbol = quote[key]
116
- when "price_open"
117
- @open = self.fix_number quote[key]
118
- when "day_high"
119
- @high = self.fix_number quote[key]
120
- when "day_low"
121
- @low = self.fix_number quote[key]
122
- when "price"
123
- @price = self.fix_number quote[key]
124
- when "volume"
125
- @volume = quote[key].to_i
126
- when "last_trade_time"
127
- @trading_day = quote[key]
128
- when "08. previous close"
129
- @prev_close = self.fix_number quote[key]
130
- when "day_change"
131
- @change = self.fix_number quote[key]
132
- when "change_pct"
133
- @change_percent = self.fix_number quote[key]
134
- when 'stock_exchange_short'
135
- @exchange = quote[key].sub /NYSEARCA/, 'NYSE'
136
- when 'name'
137
- @name = quote[key]
138
- end
139
- end
140
- end
141
-
142
- # Let's see what we can get from the api.
143
- def call_api
144
- url = "#{@base_uri}/stock"
145
- params = {symbol: @symbol, api_token: @api_key}
146
-
147
- if @exchange
148
- params[:stock_exchange] = @exchange
149
- end
150
-
151
- Lita.logger.debug "call_api: #{url} #{params.inspect}"
152
-
153
- @response = RestClient.get url, {params: params}
154
-
155
- Lita.logger.debug "response: #{@response}"
156
- end
157
-
158
- def run_search
159
- url = "#{@base_uri}/stock_search"
160
- params = {search_term: @symbol,
161
- search_by: 'symbol,name',
162
- stock_exchange: 'NASDAQ,NYSE',
163
- limit: 5,
164
- page: 1,
165
- api_token: @api_key
166
- }
167
-
168
- Lita.logger.debug "run_search: #{url} #{params.inspect}"
169
-
170
- response = RestClient.get url, {params: params}
171
-
172
- Lita.logger.debug "response: #{response}"
173
- result = JSON.parse(response)
174
-
175
- if result['total_returned'] == 1
176
- @symbol = result['data'][0]['symbol']
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(', ')}?"
181
- end
182
- end
183
-
184
- def fix_number(price_str)
185
- price_str.to_f.round(2)
186
- end
187
- end
188
-
189
7
  module Lita
190
8
  module Handlers
191
9
  class OnewheelFinance < Handler
192
10
  config :apikey, required: true
11
+ config :mode, default: 'irc'
12
+ config :handler, default: 'alphavantage'
193
13
  route /qu*o*t*e*\s+(.+)/i, :handle_quote, command: true
14
+ # route /q2\s+(.+)/i, :handle_alphavantage, command: true
194
15
 
195
16
  def handle_quote(response)
196
- 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
197
28
 
29
+ # Continue!
198
30
  if stock.error
199
31
  if stock.message
200
32
  str = stock.message
@@ -202,14 +34,40 @@ module Lita
202
34
  str = "`#{stock.symbol}` not found on any stock exchange."
203
35
  end
204
36
  else
205
- str = "#{IrcColors::grey}#{stock.exchange} - #{IrcColors::reset}#{stock.symbol}: #{IrcColors::blue}$#{stock.price}#{IrcColors::reset} "
206
- if stock.change >= 0
207
- # if irc
208
- str += "#{IrcColors::green} ⬆$#{stock.change}#{IrcColors::reset}, #{IrcColors::green}#{stock.change_percent}%#{IrcColors::reset} "
209
- 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
210
57
  else
211
- str += "#{IrcColors::red} ↯$#{stock.change}#{IrcColors::reset}, #{IrcColors::red}#{stock.change_percent}%#{IrcColors::reset} "
212
- 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
213
71
  end
214
72
  end
215
73
 
@@ -225,13 +83,14 @@ module Lita
225
83
  end
226
84
 
227
85
  # deprecated for now
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
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
93
+ end
235
94
 
236
95
  Lita.register_handler(self)
237
96
  end
@@ -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