diff --git a/.gitignore b/.gitignore index 718e52d..18095a1 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/app/jobs/stock_prices_update_job.rb b/app/jobs/stock_prices_update_job.rb index c7094b3..f966544 100644 --- a/app/jobs/stock_prices_update_job.rb +++ b/app/jobs/stock_prices_update_job.rb @@ -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 + diff --git a/bin/dc.cmd b/bin/dc.cmd new file mode 100644 index 0000000..1ddbb11 --- /dev/null +++ b/bin/dc.cmd @@ -0,0 +1,2 @@ +echo off +docker compose run stocks %* \ No newline at end of file diff --git a/test/data/global_quote_dis.json b/test/data/global_quote_dis.json new file mode 100644 index 0000000..1060f00 --- /dev/null +++ b/test/data/global_quote_dis.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/global_quote_ea.json b/test/data/global_quote_ea.json new file mode 100644 index 0000000..b757778 --- /dev/null +++ b/test/data/global_quote_ea.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/global_quote_f.json b/test/data/global_quote_f.json new file mode 100644 index 0000000..056d5e1 --- /dev/null +++ b/test/data/global_quote_f.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/global_quote_fb.json b/test/data/global_quote_fb.json new file mode 100644 index 0000000..c7ca24d --- /dev/null +++ b/test/data/global_quote_fb.json @@ -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" + } +} \ No newline at end of file diff --git a/test/data/global_quote_gps.json b/test/data/global_quote_gps.json new file mode 100644 index 0000000..239870e --- /dev/null +++ b/test/data/global_quote_gps.json @@ -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" + } +} \ No newline at end of file diff --git a/test/data/global_quote_ko.json b/test/data/global_quote_ko.json new file mode 100644 index 0000000..a7a604d --- /dev/null +++ b/test/data/global_quote_ko.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/global_quote_luv.json b/test/data/global_quote_luv.json new file mode 100644 index 0000000..98b902d --- /dev/null +++ b/test/data/global_quote_luv.json @@ -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" + } +} \ No newline at end of file diff --git a/test/data/global_quote_siri.json b/test/data/global_quote_siri.json new file mode 100644 index 0000000..4261484 --- /dev/null +++ b/test/data/global_quote_siri.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/global_quote_sne.json b/test/data/global_quote_sne.json new file mode 100644 index 0000000..b731b47 --- /dev/null +++ b/test/data/global_quote_sne.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/global_quote_twx.json b/test/data/global_quote_twx.json new file mode 100644 index 0000000..8961af4 --- /dev/null +++ b/test/data/global_quote_twx.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/global_quote_ua.json b/test/data/global_quote_ua.json new file mode 100644 index 0000000..c4f82f2 --- /dev/null +++ b/test/data/global_quote_ua.json @@ -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%" + } +} \ No newline at end of file diff --git a/test/data/stocks_roster.json b/test/data/stocks_roster.json new file mode 100644 index 0000000..e20b262 --- /dev/null +++ b/test/data/stocks_roster.json @@ -0,0 +1 @@ + ["KO", "SNE", "TWX", "DIS", "SIRI", "F", "EA", "FB", "UA", "LUV", "GPS"] \ No newline at end of file diff --git a/test/jobs/stock_prices_update_job_test.rb b/test/jobs/stock_prices_update_job_test.rb index 35f70d0..9860918 100644 --- a/test/jobs/stock_prices_update_job_test.rb +++ b/test/jobs/stock_prices_update_job_test.rb @@ -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 @@ -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)