lita-onewheel-finance 0.3.1 → 0.4.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: 113679ad593f4f4ca6d87833981502ae6f85ec63291ea72fd22385cb66197558
4
- data.tar.gz: '09f4b7139aa8f0c637391b7508101e2adec2ab044358289f41d0ea87555b2d0f'
3
+ metadata.gz: 316a69ee9c4dd4c0e88009906080be2d326bb4cac5723526956e29c569466607
4
+ data.tar.gz: e9bee0f2b4e414dcc58391707f61db410d39062ecda51ee94f185e5f613160c6
5
5
  SHA512:
6
- metadata.gz: 945c1ccecab692ec3d96ac783344ace668aa2bc5d9e32f8deda7a110cf6b5236fe5928a7b1d5653bfe54d3097bf12adf595b036f334e91c2e01cb42ed1e67efb
7
- data.tar.gz: 5cefd0c3fa137f66fda888281d6835f1189ba1d2ea771b6b7a1eb608baf1e4df5d49a2ec6c95cc017a30a020c7555e8f475ec61d847890ea4708341a2a9ff261
6
+ metadata.gz: c45a1b428948933a9680df0b80cdd1686939484e4819f0cc9a2c22046029c4a61d1337eea10586918a0721cc06d5151c6f6a3788310dfd7fe4cc33a318bbd726
7
+ data.tar.gz: 1051b4205d68f27c59875ef6ee986b78aecd9cbeb58acda85c8d420cc257562c8a3955f9d76671c104074b64e1766bc8751690f7e4f3e6869aa5cd9a2ef00df6
@@ -71,6 +71,100 @@ class WorldTradeDataQuote
71
71
  attr_reader :open, :high, :low, :price, :volume, :trading_day, :prev_close, :change, :change_percent, :exchange, :error, :name
72
72
  attr_accessor :symbol
73
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
+
74
168
  def initialize(json_blob)
75
169
  Lita.logger.debug "parsing: #{json_blob}"
76
170
  hash = JSON.parse(json_blob)
@@ -146,15 +240,7 @@ module Lita
146
240
  end
147
241
 
148
242
  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
243
+ stock = WorldTradeDataQuote.new symbol, config.apikey
158
244
  if stock.error
159
245
  stock.symbol = symbol
160
246
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-onewheel-finance"
3
- spec.version = "0.3.1"
3
+ spec.version = "0.4.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"
@@ -0,0 +1,17 @@
1
+ {
2
+ "total_returned": 1,
3
+ "total_results": 1,
4
+ "total_pages": 1,
5
+ "limit": 5,
6
+ "page": 1,
7
+ "data": [
8
+ {
9
+ "symbol": "NKE",
10
+ "name": "NIKE, Inc.",
11
+ "currency": "USD",
12
+ "price": "102.20",
13
+ "stock_exchange_long": "New York Stock Exchange",
14
+ "stock_exchange_short": "NYSE"
15
+ }
16
+ ]
17
+ }
@@ -30,4 +30,10 @@ describe Lita::Handlers::OnewheelFinance, lita_handler: true do
30
30
  send_command 'quote in'
31
31
  expect(replies.last).to eq('`in` not found on any stock exchange.')
32
32
  end
33
+
34
+ #it 'searches with 1 result' do
35
+ # mock_up 'worldtradedata-search-1'
36
+ # send_command 'quote nike'
37
+ # expect(replies.last).to eq('NKE')
38
+ #end
33
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-onewheel-finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kreps
@@ -144,6 +144,7 @@ files:
144
144
  - spec/fixtures/worldtradedata-error.json
145
145
  - spec/fixtures/worldtradedata-quote-down.json
146
146
  - spec/fixtures/worldtradedata-quote-up.json
147
+ - spec/fixtures/worldtradedata-search-1.json
147
148
  - spec/lita/handlers/onewheel_finance_spec.rb
148
149
  - spec/spec_helper.rb
149
150
  homepage: https://github.com/onewheelskyward/lita-onewheel-finance
@@ -177,5 +178,6 @@ test_files:
177
178
  - spec/fixtures/worldtradedata-error.json
178
179
  - spec/fixtures/worldtradedata-quote-down.json
179
180
  - spec/fixtures/worldtradedata-quote-up.json
181
+ - spec/fixtures/worldtradedata-search-1.json
180
182
  - spec/lita/handlers/onewheel_finance_spec.rb
181
183
  - spec/spec_helper.rb