Skip to content

Commit

Permalink
Fix exchange rates spec
Browse files Browse the repository at this point in the history
  • Loading branch information
sethherr committed Mar 7, 2025
1 parent 4874e3b commit 5972e37
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 92 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
git_source(:gitlab) { |repo| "https://gitlab.com/#{repo}.git" }

# Update GitHub ci.yml config if Ruby version is bumped
ruby "3.3.7"

# Gems that are no longer in standard library as Ruby 3.4
Expand Down
2 changes: 1 addition & 1 deletion app/services/exchange_rate_updator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ExchangeRateUpdator
def self.update
payload = ExchangeRateAPIClient.new.latest
payload = Integrations::ExchangeRateAPIClient.new.latest
rates = payload.fetch(:rates)
base_iso = payload.fetch(:base)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ExchangeRateAPIClient
class Integrations::ExchangeRateAPIClient
attr_accessor :base_iso, :base_url, :cache_key

BASE_URL = ENV.fetch("EXCHANGE_RATE_API_BASE_URL", "https://api.exchangeratesapi.io")
Expand All @@ -18,9 +18,7 @@ def latest
req.params = {"base" => base_iso, :access_key => API_KEY}
}

unless resp.status == 200 && resp.body.is_a?(Hash)
raise ExchangeRateAPIError, resp.body
end
raise(StandardError, resp.body) unless resp.status == 200 && resp.body.is_a?(Hash)

resp.body.with_indifferent_access
end
Expand All @@ -29,17 +27,10 @@ def latest
private

def conn
@conn ||= Faraday.new(url: base_url) { |conn|
conn.response :json, content_type: /\bjson$/
# if Rails.env.development?
# conn.use Faraday::RequestResponseLogger::Middleware,
# logger_level: :info,
# logger: Rails.logger
# end
conn.adapter Faraday.default_adapter
conn.options.timeout = 5
}
@conn ||= Faraday.new(url: base_url) do |con|
con.response :json, content_type: /\bjson$/
con.adapter Faraday.default_adapter
con.options.timeout = 5
end
end
end

class ExchangeRateAPIError < StandardError; end
19 changes: 0 additions & 19 deletions spec/models/exchange_rate_api_client_spec.rb

This file was deleted.

5 changes: 2 additions & 3 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@
}
config.ignore_hosts("127.0.0.1", "0.0.0.0") # for capybara selenium

%w[GOOGLE_GEOCODER MAILCHIMP_KEY FACEBOOK_AD_TOKEN CLOUDFLARE_TOKEN MAXMIND_KEY].each do |key|
config.filter_sensitive_data("<#{key}>") { ENV[key] }
end
%w[GOOGLE_GEOCODER MAILCHIMP_KEY FACEBOOK_AD_TOKEN CLOUDFLARE_TOKEN MAXMIND_KEY
EXCHANGE_RATE_API_KEY].each { |key| config.filter_sensitive_data("<#{key}>") { ENV[key] } }

config.before_record do |i|
i.response.headers.delete("Set-Cookie")
Expand Down
23 changes: 23 additions & 0 deletions spec/services/integrations/exchange_rate_api_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Integrations::ExchangeRateAPIClient, type: :service do
describe "#latest" do
let(:instance) { described_class.new("CAD") }

it "returns a hash with :base and :rates keys" do
Rails.cache.clear(instance.cache_key)
VCR.use_cassette("exchange_rate_api_client", match_requests_on: [:path]) do
response = instance.latest
pp response unless response.key?(:base)
expect(response).to have_key(:base)
expect(response).to have_key(:rates)
expect(response).to have_key(:date)
expect(response[:base]).to eq("CAD")
expect(response[:rates]).to be_a(Hash)
expect(response[:rates]).to have_key("EUR")
end
end
end
end

This file was deleted.

61 changes: 61 additions & 0 deletions spec/vcr_cassettes/exchange_rate_api_client.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5972e37

Please sign in to comment.