Skip to content
Open
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
4 changes: 3 additions & 1 deletion app/models/billing/currency_rate_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ class Billing::CurrencyRateProvider < ApplicationEnum
FRANKFURTER = 1
BANK_OF_ISRAEL = 2
NBU = 3
ALL_RATES_TODAY = 4

setup_collection do
[
{ id: FRANKFURTER, name: 'Frankfurter', service_class: 'CurrencyRates::Providers::Frankfurter' },
{ id: BANK_OF_ISRAEL, name: 'Bank of Israel', service_class: 'CurrencyRates::Providers::BankOfIsrael' },
{ id: NBU, name: 'National Bank of Ukraine', service_class: 'CurrencyRates::Providers::Nbu' }
{ id: NBU, name: 'National Bank of Ukraine', service_class: 'CurrencyRates::Providers::Nbu' },
{ id: ALL_RATES_TODAY, name: 'AllRatesToday', service_class: 'CurrencyRates::Providers::AllRatesToday' }
]
end

Expand Down
62 changes: 62 additions & 0 deletions app/services/currency_rates/providers/all_rates_today.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

module CurrencyRates
module Providers
# Central bank tables served by https://allratestoday.com, one bank
# per installation: set currency_rates.central_bank_code in yeti_web.yml
# (e.g. bcb for Brazil). The endpoint has no history, it always returns the
# bank's latest published table.
#
# Rows are { base, quote, type, value }, where 1 base = value quote. Banks
# differ in orientation (USD/BRL at bcb, EUR/USD at ecb), so the home
# currency is the one that appears in most pairs. A pair can have several
# rows: the published mid rate is used, otherwise the mean of a buy and sell
# quote. Period averages are skipped, they are not a current rate.
class AllRatesToday < CrossRateBase
LABEL = 'AllRatesToday'
DEFAULT_URL = 'https://allratestoday.com'
PATH = '/api/open/central-bank'

MID_RATE_TYPES = %w[reference middle official indicative close spot informational].freeze
BUY_SELL_TYPES = [%w[buy sell], %w[tt_buy tt_sell]].freeze

private

def rates_url
raise Error, "#{label} requires currency_rates.central_bank_code in yeti_web.yml" if bank_code.blank?

"#{(config&.central_bank_url.presence || DEFAULT_URL).chomp('/')}#{PATH}/#{bank_code}"
end

def bank_code
config&.central_bank_code
end

def home_quotes(data)
raise Error, "#{label} table for #{bank_code} is stale, last published #{data['rate_date']}" if data['stale']

pairs = data.fetch('rates').group_by { |row| row.values_at('base', 'quote') }
.transform_values { |rows| mid_rate(rows) }.compact
home = pairs.keys.flatten.tally.max_by(&:last)&.first
raise Error, "#{label} table for #{bank_code} has no usable rates" if home.nil?

pairs.each_with_object(home => 1.0) do |((base, quote), value), quotes|
quotes[base] = value if quote == home
quotes[quote] = 1.0 / value if base == home
end
end

def mid_rate(rows)
values = rows.to_h { |row| [row.fetch('type'), row.fetch('value').to_f] }
rate = values.values_at(*MID_RATE_TYPES).compact.first
rate ||= buy_sell_mean(values)
rate if rate&.positive?
end

def buy_sell_mean(values)
buy, sell = BUY_SELL_TYPES.find { |types| types.all? { |type| values.key?(type) } }
(values[buy] + values[sell]) / 2 if buy
end
end
end
end
4 changes: 4 additions & 0 deletions config/yeti_web.yml.distr
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ tmpdir: 'tmp'
# http_proxy: http://proxy.example.com:3128
# # Honour the standard HTTP(S)_PROXY env vars when http_proxy is not set.
# use_env_proxy: false
# # Central bank table used by the AllRatesToday provider, e.g. bcb, rbi, ecb.
# central_bank_code: bcb
# # Base url of that provider, defaults to https://allratestoday.com.
# central_bank_url: https://allratestoday.com

# Logging (optional). Every component - puma, delayed_job, scheduler, cdr_processor,
# prometheus exporter - logs to stdout and, when an url is set below, ships the same
Expand Down
1 change: 1 addition & 0 deletions spec/models/billing/currency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
it { is_expected.to allow_value(Billing::CurrencyRateProvider::FRANKFURTER).for(:rate_provider_id) }
it { is_expected.to allow_value(Billing::CurrencyRateProvider::BANK_OF_ISRAEL).for(:rate_provider_id) }
it { is_expected.to allow_value(Billing::CurrencyRateProvider::NBU).for(:rate_provider_id) }
it { is_expected.to allow_value(Billing::CurrencyRateProvider::ALL_RATES_TODAY).for(:rate_provider_id) }
it { is_expected.not_to allow_value(999).for(:rate_provider_id) }
end

Expand Down
237 changes: 237 additions & 0 deletions spec/services/currency_rates/providers/all_rates_today_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
# frozen_string_literal: true

RSpec.describe CurrencyRates::Providers::AllRatesToday, '#rates' do
subject do
described_class.new.rates(base: base)
end

let(:base) { 'USD' }
let(:currency_rates_config) { { central_bank_code: 'bcb' } }

let(:request_stub) do
stub_request(:get, 'https://allratestoday.com/api/open/central-bank/bcb')
.to_return(response_attrs)
end
let(:response_attrs) do
{
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: { bank: 'bcb', rate_date: '2026-11-06', stale: stale, rates: table }.to_json
}
end
let(:stale) { false }
let(:table) do
[
{ base: 'USD', quote: 'BRL', type: 'reference', value: 4.0 },
{ base: 'EUR', quote: 'BRL', type: 'reference', value: 4.4 },
{ base: 'JPY', quote: 'BRL', type: 'reference', value: 2.0 }
]
end

before do
allow(YetiConfig).to receive(:currency_rates).and_return(OpenStruct.new(currency_rates_config))
request_stub
end

it 'returns cross rates to base currency including the home currency' do
expect(subject).to eq(
'USD' => 1.0,
'EUR' => 1.1,
'JPY' => 0.5,
'BRL' => 0.25
)
expect(request_stub).to have_been_requested
end

context 'when base is the home currency of the table' do
let(:base) { 'BRL' }

it 'returns the quoted values' do
expect(subject).to eq(
'USD' => 4.0,
'EUR' => 4.4,
'JPY' => 2.0,
'BRL' => 1.0
)
end
end

context 'when the table is quoted from the home currency' do
let(:base) { 'EUR' }
let(:table) do
[
{ base: 'EUR', quote: 'USD', type: 'reference', value: 2.0 },
{ base: 'EUR', quote: 'GBP', type: 'reference', value: 0.5 }
]
end

it 'inverts the rows' do
expect(subject).to eq(
'EUR' => 1.0,
'USD' => 0.5,
'GBP' => 2.0
)
end
end

context 'when the table mixes orientations' do
let(:base) { 'USD' }
let(:table) do
[
{ base: 'USD', quote: 'JPY', type: 'reference', value: 100.0 },
{ base: 'USD', quote: 'CAD', type: 'reference', value: 1.25 },
{ base: 'EUR', quote: 'USD', type: 'reference', value: 2.0 }
]
end

it 'finds the home currency from the pairs' do
expect(subject).to eq(
'USD' => 1.0,
'JPY' => 0.01,
'CAD' => 0.8,
'EUR' => 2.0
)
end
end

context 'when a pair has a mid rate next to buy and sell' do
let(:table) do
[
{ base: 'USD', quote: 'BRL', type: 'reference', value: 1.0 },
{ base: 'EUR', quote: 'BRL', type: 'buy', value: 1.0 },
{ base: 'EUR', quote: 'BRL', type: 'middle', value: 2.0 },
{ base: 'EUR', quote: 'BRL', type: 'sell', value: 9.0 }
]
end

it 'uses the mid rate' do
expect(subject).to include('EUR' => 2.0)
end
end

context 'when a pair only has buy and sell' do
let(:table) do
[
{ base: 'USD', quote: 'BRL', type: 'buy', value: 3.0 },
{ base: 'USD', quote: 'BRL', type: 'sell', value: 5.0 },
{ base: 'EUR', quote: 'BRL', type: 'buy', value: 6.0 },
{ base: 'EUR', quote: 'BRL', type: 'sell', value: 10.0 }
]
end

it 'uses their mean' do
expect(subject).to eq(
'USD' => 1.0,
'EUR' => 2.0,
'BRL' => 0.25
)
end
end

context 'when a pair only has telegraphic transfer buy and sell' do
let(:base) { 'BRL' }
let(:table) do
[
{ base: 'USD', quote: 'BRL', type: 'tt_buy', value: 3.0 },
{ base: 'USD', quote: 'BRL', type: 'tt_sell', value: 5.0 }
]
end

it 'uses their mean' do
expect(subject).to eq('BRL' => 1.0, 'USD' => 4.0)
end
end

context 'when a row has an unusable type or value' do
let(:table) do
[
{ base: 'USD', quote: 'BRL', type: 'reference', value: 4.0 },
{ base: 'EUR', quote: 'BRL', type: 'reference', value: 4.4 },
{ base: 'CHF', quote: 'BRL', type: 'monthly_average', value: 5.0 },
{ base: 'JPY', quote: 'BRL', type: 'reference', value: 0 }
]
end

it 'skips that currency' do
expect(subject.keys).to contain_exactly('USD', 'EUR', 'BRL')
end
end

context 'when central_bank_url is configured' do
let(:currency_rates_config) { { central_bank_code: 'bcb', central_bank_url: 'https://rates.example.com/' } }
let(:request_stub) do
stub_request(:get, 'https://rates.example.com/api/open/central-bank/bcb')
.to_return(response_attrs)
end

it 'requests that host' do
subject
expect(request_stub).to have_been_requested
end
end

context 'when central_bank_code is not configured' do
let(:currency_rates_config) { {} }

it 'raises provider error without a request' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /central_bank_code/)
expect(request_stub).not_to have_been_requested
end
end

context 'when currency_rates is not configured at all' do
before { allow(YetiConfig).to receive(:currency_rates).and_return(nil) }

it 'raises provider error' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /central_bank_code/)
end
end

context 'when the table is stale' do
let(:stale) { true }

it 'raises provider error, keeping the rates in place' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /stale, last published 2026-11-06/)
end
end

context 'when the table is empty' do
let(:table) { [] }

it 'raises provider error' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /no usable rates/)
end
end

context 'when base is not in the table' do
let(:base) { 'UAH' }

it 'raises provider error' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /has no UAH rate/)
end
end

context 'when API responds with error status' do
let(:response_attrs) { { status: 404, body: '{"error":"No published table available for this source"}' } }

it 'raises provider error' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /HTTP 404/)
end
end

context 'when API responds with invalid json' do
let(:response_attrs) { { status: 200, body: 'not a json' } }

it 'raises provider error' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /unexpected response/)
end
end

context 'when API responds without rates' do
let(:response_attrs) { { status: 200, body: '{}' } }

it 'raises provider error' do
expect { subject }.to raise_error(CurrencyRates::Providers::Base::Error, /unexpected response/)
end
end
end