Skip to content

Commit

Permalink
Minor caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpigford committed Nov 11, 2024
1 parent 6833e7f commit 90be8cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
19 changes: 10 additions & 9 deletions app/controllers/stocks/info_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ class Stocks::InfoController < ApplicationController
def show
@stock = Stock.find_by(symbol: params[:stock_ticker])

headers = {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{ENV['SYNTH_API_KEY']}",
"X-Source" => "maybe_marketing",
"X-Source-Type" => "api"
}
@stock_info = Rails.cache.fetch("stock_info/v1/#{@stock.symbol}", expires_in: 24.hours) do
headers = {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{ENV['SYNTH_API_KEY']}",
"X-Source" => "maybe_marketing",
"X-Source-Type" => "api"
}

# Fetch stock information from Synth Finance API
@stock_info = Faraday.get("https://api.synthfinance.com/tickers/#{@stock.symbol}", nil, headers)
@stock_info = JSON.parse(@stock_info.body)["data"]
response = Faraday.get("https://api.synthfinance.com/tickers/#{@stock.symbol}", nil, headers)
JSON.parse(response.body)["data"]
end
end
end
15 changes: 9 additions & 6 deletions app/controllers/stocks/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ class Stocks::StatisticsController < ApplicationController
def show
@stock = Stock.find_by(symbol: params[:stock_ticker])

headers = {
@stock_statistics = Rails.cache.fetch("stock_statistics/v1/#{@stock.symbol}", expires_in: 24.hours) do
headers = {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{ENV['SYNTH_API_KEY']}",
"X-Source" => "maybe_marketing",
"X-Source-Type" => "api"
}
@stock_statistics = Faraday.get("https://api.synthfinance.com/tickers/#{@stock.symbol}", nil, headers)
parsed_data = JSON.parse(@stock_statistics.body)["data"]
@stock_statistics = parsed_data && parsed_data["market_data"] ? parsed_data["market_data"] : nil
"X-Source-Type" => "api"
}

response = Faraday.get("https://api.synthfinance.com/tickers/#{@stock.symbol}", nil, headers)
parsed_data = JSON.parse(response.body)["data"]
parsed_data && parsed_data["market_data"] ? parsed_data["market_data"] : nil
end
end
end
7 changes: 0 additions & 7 deletions app/views/stocks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@
<div class="px-4 py-12 text-xs font-bold text-center text-gray-300 uppercase">Loading info...</div>
<% end %>
</div>

<div class="p-4 bg-white border border-gray-100 rounded-lg shadow-xs">
<h3 class="text-lg font-medium text-black">News</h3>
<%= turbo_frame_tag "stock_news", src: stock_news_path(@stock), loading: :lazy do %>
<div class="px-4 py-12 text-xs font-bold text-center text-gray-300 uppercase">Loading info...</div>
<% end %>
</div>
</div>

<div class="flex flex-col gap-4">
Expand Down

0 comments on commit 90be8cf

Please sign in to comment.