lita-onewheel-finance 0.3.0 → 0.7.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: d7c63c6ec509d0f13881f0219acb7f1bff0b4fc7c3fac642e4156dffa60ec95e
4
- data.tar.gz: 1ae954736d137e0be01f10e9bd019686c63d6aa373d144fc6863ccdf01692dae
3
+ metadata.gz: ef2f754f9b1fe08aa1fe7749a6cc7b338e83b0ecb5d2745decb441f8773f278e
4
+ data.tar.gz: 95d1163691c52c32eeae155112c0d86fad7bec95226ec56156e28ef94eceb616
5
5
  SHA512:
6
- metadata.gz: a75061d1d8a3e4a6edcc73a1191e3696491d0a7b4e9d2d7ec538de17969ed200c287569b37123f5ea6cee392ce6654975110917140be0aa5e338d66abe3b9016
7
- data.tar.gz: 4aad8b4f39180a2c7499c89d80d756c01d526d0e47ccc5c4ff850bcfa8f0bc09b908a39fd0d85363384926d1cd80866fb23e398520066ac8c3f4b1505991285a
6
+ metadata.gz: 33982576f1623d697254375a928dab14974cac4173d8f6079d30c381d8ce56b62d13013ec799f7ca07c1b6e6c8a5f585cdce4d2ae0aaa329072483890b155e71
7
+ data.tar.gz: cd8eb5c4a74651005cf8579511af016206fbceca03337c4b323b892e6c4204057c8d7df8bc00b05c7df5788648aa7ce0940197c2533ed920d0488c5e4d5d40cb
@@ -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,144 +1,65 @@
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(json_blob)
75
- Lita.logger.debug "parsing: #{json_blob}"
76
- hash = JSON.parse(json_blob)
77
-
78
- if hash['Message'].to_s.include? 'Error'
79
- @error = true
80
- return
81
- else
82
- @error = false
83
- end
84
-
85
- quote = hash['data'][0]
86
-
87
- quote.keys.each do |key|
88
- case key
89
- when "symbol"
90
- @symbol = quote[key]
91
- when "price_open"
92
- @open = self.fix_number quote[key]
93
- when "day_high"
94
- @high = self.fix_number quote[key]
95
- when "day_low"
96
- @low = self.fix_number quote[key]
97
- when "price"
98
- @price = self.fix_number quote[key]
99
- when "volume"
100
- @volume = quote[key].to_i
101
- when "last_trade_time"
102
- @trading_day = quote[key]
103
- when "08. previous close"
104
- @prev_close = self.fix_number quote[key]
105
- when "day_change"
106
- @change = self.fix_number quote[key]
107
- when "change_pct"
108
- @change_percent = self.fix_number quote[key]
109
- when 'stock_exchange_short'
110
- @exchange = quote[key].sub /NYSEARCA/, 'NYSE'
111
- when 'name'
112
- @name = quote[key]
113
- end
114
- end
115
- end
116
-
117
- def fix_number(price_str)
118
- price_str.to_f.round(2)
119
- end
120
- end
121
-
122
7
  module Lita
123
8
  module Handlers
124
9
  class OnewheelFinance < Handler
125
10
  config :apikey, required: true
126
- 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
127
15
 
128
16
  def handle_quote(response)
129
- 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
130
28
 
29
+ # Continue!
131
30
  if stock.error
132
- 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
133
36
  else
134
- str = "#{IrcColors::grey}#{stock.exchange} - #{IrcColors::reset}#{stock.symbol}: #{IrcColors::blue}$#{stock.price}#{IrcColors::reset} "
135
- if stock.change >= 0
136
- # if irc
137
- str += "#{IrcColors::green} ⬆$#{stock.change}#{IrcColors::reset}, #{IrcColors::green}#{stock.change_percent}%#{IrcColors::reset} "
138
- 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}#{stock.price}#{IrcColors::reset} "
45
+ if stock.change >= 0
46
+ # if irc
47
+ str += "#{IrcColors::green} ⬆#{dollar_sign}#{stock.change}#{IrcColors::reset}, #{IrcColors::green}#{stock.change_percent}%#{IrcColors::reset} "
48
+ str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
49
+ else
50
+ str += "#{IrcColors::red} ↯#{dollar_sign}#{stock.change}#{IrcColors::reset}, #{IrcColors::red}#{stock.change_percent}%#{IrcColors::reset} "
51
+ str += "#{IrcColors::grey}(#{stock.name})#{IrcColors::reset}"
52
+ end
139
53
  else
140
- str += "#{IrcColors::red} ↯$#{stock.change}#{IrcColors::reset}, #{IrcColors::red}#{stock.change_percent}%#{IrcColors::reset} "
141
- str += "(#{stock.name})"
54
+ str = "#{stock.exchange} - #{stock.symbol}: #{dollar_sign}#{stock.price} "
55
+ if stock.change >= 0
56
+ # if irc
57
+ str += " :arrow_up:#{dollar_sign}#{stock.change}, :heavy_plus_sign:#{stock.change_percent}% "
58
+ str += "(#{stock.name})"
59
+ else
60
+ str += " :chart_with_downwards_trend:#{dollar_sign}#{stock.change}, :heavy_minus_sign:#{stock.change_percent}% "
61
+ str += "(#{stock.name})"
62
+ end
142
63
  end
143
64
  end
144
65
 
@@ -146,15 +67,7 @@ module Lita
146
67
  end
147
68
 
148
69
  def handle_world_trade_data(symbol)
149
- url = "https://api.worldtradingdata.com/api/v1/stock"
150
- params = {symbol: symbol, api_token: config.apikey}
151
-
152
- Lita.logger.debug "#{url} #{params.inspect}"
153
-
154
- resp = RestClient.get url, {params: params}
155
- Lita.logger.debug "response: #{resp}"
156
-
157
- stock = WorldTradeDataQuote.new resp
70
+ stock = WorldTradeDataQuote.new symbol, config.apikey
158
71
  if stock.error
159
72
  stock.symbol = symbol
160
73
  end
@@ -162,12 +75,13 @@ module Lita
162
75
  end
163
76
 
164
77
  # deprecated for now
165
- def handle_alphavantage
166
- url = "https://www.alphavantage.co/query"
167
- params = {function: 'GLOBAL_QUOTE', symbol: response.matches[0][0], apikey: config.apikey}
168
- Lita.logger.debug "#{url} #{params.inspect}"
169
- resp = RestClient.get url, {params: params}
170
- stock = GlobalQuote.new resp
78
+ def handle_alphavantage(symbol)
79
+ stock = AlphaVantageQuote.new symbol, config.apikey
80
+ end
81
+
82
+ # welp
83
+ def handle_yahoo(symbol)
84
+ stock = YahooQuote.new symbol, config.apikey
171
85
  end
172
86
 
173
87
  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