Class: ExchangeRates::Rate
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ExchangeRates::Rate
- Defined in:
- lib/exchange_rates/rate.rb
Constant Summary collapse
- BASE_CURRENCY =
'HKD'.freeze
- CUSTOMS_VALUE_FALLBACK_CURRENCY =
'USD'.freeze
- NOT_SUPPORTED_CURRENCIES =
NB: to update the list of not supported currencies, run: Country.all.map(&:currency_code).uniq - Money::Currency.table.map{|c| c.to_s.upcase}
[nil, '', 'IMP', 'TVD'].freeze
Class Method Summary collapse
Class Method Details
.convert(amount, from_currency, to_currency = BASE_CURRENCY) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/exchange_rates/rate.rb', line 14 def self.convert(amount, from_currency, to_currency = BASE_CURRENCY) case when amount == 0 0 when to_currency == from_currency amount when to_currency == BASE_CURRENCY amount * get_rate(from_currency) else amount * (get_rate(from_currency) / get_rate(to_currency)) end end |
.get_rate(currency) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/exchange_rates/rate.rb', line 27 def self.get_rate(currency) return 1 if currency == BASE_CURRENCY cached = Rails.cache.fetch("#{currency.downcase}_to_#{BASE_CURRENCY.downcase}", expired_at: Date.today.midnight + ENV['RESET_CACHED_CURRENCY_AT'].to_i.hours) do where(from_currency: currency).first.rate_to_base_currency end cached || where(from_currency: currency).first.rate_to_base_currency end |