forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock-portfolio.rb
executable file
·68 lines (49 loc) · 1.24 KB
/
stock-portfolio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby
# Parameters
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Stocks
# @raycast.mode inline
# @raycast.refreshTime 1h
# Optional parameters:
# @raycast.icon 📈
# Documentation:
# @raycast.author Thomas Paul Mann
# @raycast.authorURL https://github.com/thomaspaulmann
# @raycast.description Keep track of your stock portfolio.
# Configuration
SYMBOLS = [
"AAPL",
"BYND",
"TSLA",
]
# Main program
require "json"
require "net/http"
require "uri"
if SYMBOLS.empty?
puts "No stock symbol provided"
exit(1)
end
def fetch_stock(symbol)
uri = URI("https://api.lil.software/stocks?symbol=#{symbol}")
req = Net::HTTP::Get.new(uri)
req_options = {
use_ssl: uri.scheme == "https",
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) { |http|
http.request(req)
}
if res.code == "200"
result = JSON.parse(res.body)
current = result["current"]
previous_close = result["previous_close"]
growth = ((current - previous_close) / previous_close * 100).round(1)
return "#{symbol}: #{current} (#{growth}%)"
else
puts "Failed loading stock"
exit(1)
end
end
portfolio = SYMBOLS.map { |symbol| fetch_stock symbol }
puts "#{portfolio * " "}"