From 5848fa13f91f4a9d8509c14883d9e70af7b14e8b Mon Sep 17 00:00:00 2001 From: Elizabeth Prescott Date: Sat, 1 Jun 2024 11:49:32 -0400 Subject: [PATCH 1/7] Job now prints full output for all api calls. Next step, extract just the ticket symbol and closing price --- .gitignore | 1 + app/jobs/stock_prices_update_job.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/.gitignore b/.gitignore index 232ab1d..f46a52d 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 d46f994..c7094b3 100644 --- a/app/jobs/stock_prices_update_job.rb +++ b/app/jobs/stock_prices_update_job.rb @@ -4,5 +4,18 @@ class StockPricesUpdateJob < ApplicationJob def perform(*args) # For each stock symbol, request the latest closing cost # 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 + end + + 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 end From 5247d71e116a045376ce8fee6a9b2de845f68bef Mon Sep 17 00:00:00 2001 From: Adam Bachman Date: Sat, 1 Jun 2024 14:56:51 -0400 Subject: [PATCH 2/7] Ignore the Ruby Lint job in CI for now --- .github/workflows/ci.yml | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9bf6a37..518afa2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,22 +8,27 @@ on: types: ['opened', 'reopened', 'synchronize', 'unlocked'] jobs: - ruby-lint: - name: Ruby Lint - runs-on: ubuntu-latest + # NOTE: @abachman + # In order to move a little faster for the Ruby for Good weekend event, I'm + # commenting out the ruby-lint job. We can re-enable it after the event + # once we've had a chance to let the dust settle and run `standardrb --fix` + # on everything. + # ruby-lint: + # name: Ruby Lint + # runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 + # steps: + # - name: Checkout code + # uses: actions/checkout@v4 - - name: Set up Ruby - uses: ruby/setup-ruby@v1.178.0 - with: - ruby-version: '3.2.3' - bundler-cache: true # runs 'bundle install' and caches installed gems automatically + # - name: Set up Ruby + # uses: ruby/setup-ruby@v1.178.0 + # with: + # ruby-version: '3.2.3' + # bundler-cache: true # runs 'bundle install' and caches installed gems automatically - - name: Ruby Lint - run: bundle exec standardrb --parallel -f github + # - name: Ruby Lint + # run: bundle exec standardrb --parallel -f github rails-test: name: Rails Test From 79e42231f6103554e1df369682cf82070a0e52bc Mon Sep 17 00:00:00 2001 From: Adam Bachman Date: Sat, 1 Jun 2024 15:33:32 -0400 Subject: [PATCH 3/7] add webmock gem and setup in test_helper --- Gemfile | 1 + Gemfile.lock | 9 +++++++++ test/test_helper.rb | 1 + 3 files changed, 11 insertions(+) diff --git a/Gemfile b/Gemfile index ae7c103..797fa1f 100644 --- a/Gemfile +++ b/Gemfile @@ -39,4 +39,5 @@ end group :test do gem "capybara" gem "selenium-webdriver" + gem "webmock" end diff --git a/Gemfile.lock b/Gemfile.lock index dacf8e4..86f2482 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -108,6 +108,9 @@ GEM coderay (1.1.3) concurrent-ruby (1.2.3) connection_pool (2.4.1) + crack (1.0.0) + bigdecimal + rexml crass (1.0.6) cssbundling-rails (1.4.0) railties (>= 6.0.0) @@ -132,6 +135,7 @@ GEM ffi (1.16.3) globalid (1.2.1) activesupport (>= 6.1) + hashdiff (1.1.0) i18n (1.14.4) concurrent-ruby (~> 1.0) io-console (0.7.2) @@ -348,6 +352,10 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) + webmock (3.23.1) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) webrick (1.8.1) websocket (1.2.10) websocket-driver (0.7.6) @@ -390,6 +398,7 @@ DEPENDENCIES turbo-rails tzinfo-data web-console + webmock RUBY VERSION ruby 3.2.3p157 diff --git a/test/test_helper.rb b/test/test_helper.rb index 933209e..9e67a65 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,7 @@ ENV["RAILS_ENV"] ||= "test" require_relative "../config/environment" require "rails/test_help" +require "webmock/minitest" module ActiveSupport class TestCase From 864ced589715233355fc516bedc9a3a156632ac3 Mon Sep 17 00:00:00 2001 From: Adam Bachman Date: Sat, 1 Jun 2024 16:04:45 -0400 Subject: [PATCH 4/7] move api_keys.rb into initializers --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f46a52d..02be899 100644 --- a/.gitignore +++ b/.gitignore @@ -33,7 +33,7 @@ # Ignore master key for decrypting credentials and more. /config/master.key -/config/api_keys.rb +/config/initializers/api_keys.rb # ignore local database.yml /config/database.yml From 62f7e14b24dc79f14080efdd0578781f0c4dd379 Mon Sep 17 00:00:00 2001 From: Adam Bachman Date: Sat, 1 Jun 2024 16:06:21 -0400 Subject: [PATCH 5/7] add test stubs for stocks API --- test/jobs/stock_prices_update_job_test.rb | 42 +++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/test/jobs/stock_prices_update_job_test.rb b/test/jobs/stock_prices_update_job_test.rb index d32a446..1c79c9d 100644 --- a/test/jobs/stock_prices_update_job_test.rb +++ b/test/jobs/stock_prices_update_job_test.rb @@ -1,7 +1,43 @@ require "test_helper" class StockPricesUpdateJobTest < ActiveJob::TestCase - # test "the truth" do - # assert true - # end + STOCK_URL_MATCHER = %r{https://www\.alphavantage\.co/query\?apikey=asdf&function=GLOBAL_QUOTE&symbol=[A-Z]+} + STOCK_SYMBOLS = ["KO", "SNE", "TWX", "DIS", "SIRI", "F", "EA", "FB", "UA", "LUV", "GPS"] + + setup do + stub_request(:get, STOCK_URL_MATCHER) + .with( + headers: { + "Accept" => "*/*", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "Host" => "www.alphavantage.co", + "User-Agent" => "Ruby" + } + ) + .to_return(status: 200, body: "", headers: {}) + end + + test "makes API calls" do + StockPricesUpdateJob.perform_now + assert_requested :get, STOCK_URL_MATCHER, times: 11 + 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) + assert stock.present? + assert stock.price.present? + end + end end From 34ee8f62dea4f8aeff6d241afd017119804a6dc8 Mon Sep 17 00:00:00 2001 From: Adam Bachman Date: Sat, 1 Jun 2024 16:10:57 -0400 Subject: [PATCH 6/7] unignore api_keys initializer --- .gitignore | 3 +-- config/initializers/api_keys.rb | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 config/initializers/api_keys.rb diff --git a/.gitignore b/.gitignore index 02be899..718e52d 100644 --- a/.gitignore +++ b/.gitignore @@ -33,7 +33,6 @@ # Ignore master key for decrypting credentials and more. /config/master.key -/config/initializers/api_keys.rb # ignore local database.yml /config/database.yml @@ -42,4 +41,4 @@ !/app/assets/builds/.keep /node_modules -.vscode/ \ No newline at end of file +.vscode/ diff --git a/config/initializers/api_keys.rb b/config/initializers/api_keys.rb new file mode 100644 index 0000000..67be809 --- /dev/null +++ b/config/initializers/api_keys.rb @@ -0,0 +1 @@ +API_KEY = ENV.fetch("STOCKS_API_KEY", "test-api-key") \ No newline at end of file From ee6c1a61481cf085ae7b91b2f8a7d561981d0e50 Mon Sep 17 00:00:00 2001 From: Adam Bachman Date: Sat, 1 Jun 2024 16:13:35 -0400 Subject: [PATCH 7/7] API URL should not have hardcoded key --- test/jobs/stock_prices_update_job_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jobs/stock_prices_update_job_test.rb b/test/jobs/stock_prices_update_job_test.rb index 1c79c9d..35f70d0 100644 --- a/test/jobs/stock_prices_update_job_test.rb +++ b/test/jobs/stock_prices_update_job_test.rb @@ -1,7 +1,7 @@ require "test_helper" class StockPricesUpdateJobTest < ActiveJob::TestCase - STOCK_URL_MATCHER = %r{https://www\.alphavantage\.co/query\?apikey=asdf&function=GLOBAL_QUOTE&symbol=[A-Z]+} + STOCK_URL_MATCHER = %r{https://www\.alphavantage\.co/query\?apikey=[^&]+&function=GLOBAL_QUOTE&symbol=[A-Z]+} STOCK_SYMBOLS = ["KO", "SNE", "TWX", "DIS", "SIRI", "F", "EA", "FB", "UA", "LUV", "GPS"] setup do