Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement stock price update job and test. Supports #146 #175

Merged
merged 3 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/api_keys.rb

# ignore local database.yml
/config/database.yml
Expand Down
23 changes: 17 additions & 6 deletions app/jobs/stock_prices_update_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ def perform(*args)
# update the stocks table with each new closing cost
stock_symbols = ["KO", "SNE", "TWX", "DIS", "SIRI", "F", "EA", "FB", "UA", "LUV", "GPS"]
stock_symbols.each do |symbol|
api_request(symbol)
stock_db = Stock.find_by(ticker: symbol)
end
data = api_request(symbol)
price = data["Global Quote"]["05. price"]
stock_rec = Stock.find_by(ticker: symbol)
if stock_rec
stock_rec.price = price
stock_rec.save
else
stock_rec = Stock.new
stock_rec.ticker = symbol
stock_rec.price = price
stock_rec.save
end
end
end

private
private

def api_request(symbol)
url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=#{symbol}&apikey=#{API_KEY}"
uri = URI.parse(url)
print Net::HTTP.get(uri)
end
return JSON.parse Net::HTTP.get(uri)
end
end

2 changes: 2 additions & 0 deletions bin/dc.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo off
docker compose run stocks %*
14 changes: 14 additions & 0 deletions test/data/global_quote_dis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "DIS",
"02. open": "101.5200",
"03. high": "104.0800",
"04. low": "101.4100",
"05. price": "103.9100",
"06. volume": "14735791",
"07. latest trading day": "2024-05-31",
"08. previous close": "101.7000",
"09. change": "2.2100",
"10. change percent": "2.1731%"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_ea.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "EA",
"02. open": "131.2100",
"03. high": "133.0200",
"04. low": "129.0800",
"05. price": "132.8800",
"06. volume": "4525269",
"07. latest trading day": "2024-05-31",
"08. previous close": "131.2300",
"09. change": "1.6500",
"10. change percent": "1.2573%"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "F",
"02. open": "11.7700",
"03. high": "12.1500",
"04. low": "11.6600",
"05. price": "12.1300",
"06. volume": "57205750",
"07. latest trading day": "2024-05-31",
"08. previous close": "11.7700",
"09. change": "0.3600",
"10. change percent": "3.0586%"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_fb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "FB",
"02. open": "n/a",
"03. high": "n/a",
"04. low": "n/a" ,
"05. price": "467.29",
"06. volume": "n/a",
"07. latest trading day": "2024-05-31",
"08. previous close": "n/a",
"09. change": "n/a",
"10. change percent": "n/a"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_gps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "GPS",
"02. open": "n/a",
"03. high": "n/a",
"04. low": "n/a" ,
"05. price": "28.96",
"06. volume": "n/a",
"07. latest trading day": "2024-05-31",
"08. previous close": "n/a",
"09. change": "n/a",
"10. change percent": "n/a"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_ko.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "KO",
"02. open": "61.9900",
"03. high": "63.0250",
"04. low": "61.6900",
"05. price": "62.9300",
"06. volume": "19816630",
"07. latest trading day": "2024-05-31",
"08. previous close": "61.9700",
"09. change": "0.9600",
"10. change percent": "1.5491%"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_luv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "LUV",
"02. open": "n/a",
"03. high": "n/a",
"04. low": "n/a" ,
"05. price": "26.84",
"06. volume": "n/a",
"07. latest trading day": "2024-05-31",
"08. previous close": "n/a",
"09. change": "n/a",
"10. change percent": "n/a"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_siri.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "SIRI",
"02. open": "2.7350",
"03. high": "2.9300",
"04. low": "2.7200",
"05. price": "2.8200",
"06. volume": "101093349",
"07. latest trading day": "2024-05-31",
"08. previous close": "2.7400",
"09. change": "0.0800",
"10. change percent": "2.9197%"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_sne.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "SNE",
"02. open": "81.7500",
"03. high": "82.3750",
"04. low": "81.4700",
"05. price": "82.3400",
"06. volume": "968344",
"07. latest trading day": "2024-05-31",
"08. previous close": "80.7900",
"09. change": "1.5500",
"10. change percent": "1.9186%"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_twx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "TWX",
"02. open": "98.7700",
"03. high": "98.7700",
"04. low": "98.7700",
"05. price": "98.7700",
"06. volume": "8475",
"07. latest trading day": "2018-06-15",
"08. previous close": "98.7700",
"09. change": "0.0000",
"10. change percent": "0.0000%"
}
}
14 changes: 14 additions & 0 deletions test/data/global_quote_ua.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Global Quote": {
"01. symbol": "UA",
"02. open": "6.7800",
"03. high": "6.9700",
"04. low": "6.6900",
"05. price": "6.9600",
"06. volume": "3699250",
"07. latest trading day": "2024-05-31",
"08. previous close": "6.7400",
"09. change": "0.2200",
"10. change percent": "3.2641%"
}
}
1 change: 1 addition & 0 deletions test/data/stocks_roster.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["KO", "SNE", "TWX", "DIS", "SIRI", "F", "EA", "FB", "UA", "LUV", "GPS"]
6 changes: 1 addition & 5 deletions test/jobs/stock_prices_update_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StockPricesUpdateJobTest < ActiveJob::TestCase
"User-Agent" => "Ruby"
}
)
.to_return(status: 200, body: "", headers: {})
.to_return(status: 200, body: File.open(Rails.root.join('./test/data/global_quote_f.json')), headers: {})
end

test "makes API calls" do
Expand All @@ -23,16 +23,12 @@ class StockPricesUpdateJobTest < ActiveJob::TestCase
end

test "creates Stock records" do
skip("waiting for implementation")

assert_difference("Stock.count", 11) do
StockPricesUpdateJob.perform_now
end
end

test "sets Stock ticker and price" do
skip("waiting for implementation")

StockPricesUpdateJob.perform_now
STOCK_SYMBOLS.each do |ticker|
stock = Stock.find_by(ticker: ticker)
Expand Down