Class: NbrbCurrency

Inherits:
Money::Bank::VariableExchange
  • Object
show all
Includes:
NbrbCurrencyVersion
Defined in:
lib/nbrb_currency.rb,
lib/nbrb_currency/rates_document.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: RatesDocument

Constant Summary collapse

SERIALIZER_DATE_SEPARATOR =
'_AT_'
DECIMAL_PRECISION =
5
NBRB_RATES_URL =
'https://api.nbrb.by/exrates/rates?periodicity=0'
NBRB_HISTORICAL_URL =
'https://api.nbrb.by/exrates/rates?ondate=%s&periodicity=0'
REDENOMINATION_DATE =

BYN redenomination date: July 1, 2016 (10000:1)

Date.new(2016, 7, 1)
BYR_THRESHOLD =

Threshold to detect BYR vs BYN (BYR rates are typically > 1000, BYN < 100)

100
CURRENCIES =
%w(USD EUR RUB PLN UAH GBP JPY CNY CHF SEK NOK DKK CAD AUD NZD TRY KRW SGD HKD).freeze
LEGACY_CURRENCIES =
%w(BYR).freeze

Constants included from NbrbCurrencyVersion

NbrbCurrencyVersion::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = Money::RatesStore::NbrbHistoricalStore.new) ⇒ NbrbCurrency

Returns a new instance of NbrbCurrency.



31
32
33
# File 'lib/nbrb_currency.rb', line 31

def initialize(store = Money::RatesStore::NbrbHistoricalStore.new, &)
  super
end

Instance Attribute Details

#historical_last_updatedObject

Returns the value of attribute historical_last_updated.



16
17
18
# File 'lib/nbrb_currency.rb', line 16

def historical_last_updated
  @historical_last_updated
end

#historical_rates_updated_atObject

Returns the value of attribute historical_rates_updated_at.



16
17
18
# File 'lib/nbrb_currency.rb', line 16

def historical_rates_updated_at
  @historical_rates_updated_at
end

#last_updatedObject

Returns the value of attribute last_updated.



16
17
18
# File 'lib/nbrb_currency.rb', line 16

def last_updated
  @last_updated
end

#rates_updated_atObject

Returns the value of attribute rates_updated_at.



16
17
18
# File 'lib/nbrb_currency.rb', line 16

def rates_updated_at
  @rates_updated_at
end

Instance Method Details

#check_currency_available(currency, date = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/nbrb_currency.rb', line 127

def check_currency_available(currency, date = nil)
  currency_string = currency.to_s
  base = base_currency_for_date(date)
  return true if currency_string == base
  return true if CURRENCIES.include?(currency_string)
  return true if LEGACY_CURRENCIES.include?(currency_string)

  raise CurrencyUnavailable, "No rates available for #{currency_string}"
end

#exchange(cents, from_currency, to_currency, date = nil) ⇒ Object



57
58
59
# File 'lib/nbrb_currency.rb', line 57

def exchange(cents, from_currency, to_currency, date = nil)
  exchange_with(Money.new(cents, from_currency), to_currency, date)
end

#exchange_with(from, to_currency, date = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/nbrb_currency.rb', line 61

def exchange_with(from, to_currency, date = nil)
  base_currency = base_currency_for_date(date)
  rate = get_rate(from.currency, to_currency,
    date) || calculate_cross_rate(from.currency, to_currency, base_currency, date)

  calculate_exchange(from, to_currency, rate)
end

#export_rates(format, file = nil, _opts = {}) ⇒ Object

Raises:

  • (Money::Bank::UnknownRateFormat)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nbrb_currency.rb', line 93

def export_rates(format, file = nil, _opts = {})
  raise Money::Bank::UnknownRateFormat unless RATE_FORMATS.include? format

  store.transaction do
    s = case format
        when :json
          JSON.dump(rates)
        when :ruby
          Marshal.dump(rates)
        when :yaml
          YAML.dump(rates)
        end

    File.write(file, s) unless file.nil?
    s
  end
end

#get_rate(from, to, date = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/nbrb_currency.rb', line 69

def get_rate(from, to, date = nil)
  return 1 if from == to

  check_currency_available(from, date)
  check_currency_available(to, date)

  date = date[:date] if date.is_a?(Hash)

  store.get_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code, date)
end

#import_rates(format, data, _opts = {}) ⇒ Object

Raises:

  • (Money::Bank::UnknownRateFormat)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nbrb_currency.rb', line 111

def import_rates(format, data, _opts = {})
  raise Money::Bank::UnknownRateFormat unless RATE_FORMATS.include? format

  store.transaction do
    parsed_data = parse_import_data(format, data)

    parsed_data.each do |key, rate|
      from, to = key.split(SERIALIZER_SEPARATOR)
      to, date = to.split(SERIALIZER_DATE_SEPARATOR)
      store.add_rate from, to, BigDecimal(rate, DECIMAL_PRECISION), date
    end
  end

  self
end

#ratesObject



85
86
87
88
89
90
91
# File 'lib/nbrb_currency.rb', line 85

def rates
  store.each_rate.with_object({}) do |(from, to, rate, date), hash|
    key = [from, to].join(SERIALIZER_SEPARATOR)
    key = [key, date.to_s].join(SERIALIZER_DATE_SEPARATOR) if date
    hash[key] = rate
  end
end

#save_historical_rates(cache, date = nil) ⇒ Object



52
53
54
55
# File 'lib/nbrb_currency.rb', line 52

def save_historical_rates(cache, date = nil)
  url = date ? (NBRB_HISTORICAL_URL % date.strftime('%Y-%m-%d')) : NBRB_RATES_URL
  save_rates(cache, url)
end

#save_rates(cache, url = NBRB_RATES_URL) ⇒ Object

Raises:



44
45
46
47
48
49
50
# File 'lib/nbrb_currency.rb', line 44

def save_rates(cache, url = NBRB_RATES_URL)
  raise InvalidCache unless cache

  File.open(cache, 'w') do |file|
    URI.parse(url).open { |io| io.each_line { |line| file.puts line } }
  end
end

#set_rate(from, to, rate, date = nil) ⇒ Object



80
81
82
83
# File 'lib/nbrb_currency.rb', line 80

def set_rate(from, to, rate, date = nil)
  date = date[:date] if date.is_a?(Hash)
  store.add_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code, rate, date)
end

#update_historical_rates(cache = nil, date = nil) ⇒ Object



39
40
41
42
# File 'lib/nbrb_currency.rb', line 39

def update_historical_rates(cache = nil, date = nil)
  url = date ? (NBRB_HISTORICAL_URL % date.strftime('%Y-%m-%d')) : NBRB_RATES_URL
  update_parsed_historical_rates(doc(cache, url))
end

#update_rates(cache = nil) ⇒ Object



35
36
37
# File 'lib/nbrb_currency.rb', line 35

def update_rates(cache = nil)
  update_parsed_rates(doc(cache, NBRB_RATES_URL))
end