Class: Lariconv

Inherits:
Object
  • Object
show all
Defined in:
lib/lariconv.rb

Overview

TODO: Add info from TBC, Liberty, Basisbank, as well as from Wise Business and Revolut Business

Defined Under Namespace

Classes: InvalidAmountException, UnavailableCurrencyException

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, currency:, date: nil) ⇒ Lariconv

Returns a new instance of Lariconv.



29
30
31
32
33
# File 'lib/lariconv.rb', line 29

def initialize(amount:, currency:, date: nil)
  @amount = amount
  @currency = currency.upcase
  @date = date
end

Class Method Details

.convertObject



25
26
27
# File 'lib/lariconv.rb', line 25

def self.convert(...)
  new(...).convert
end

Instance Method Details

#amountObject



45
46
47
48
49
# File 'lib/lariconv.rb', line 45

def amount
  raise InvalidAmountException unless [Integer, Float, BigDecimal].include? @amount.class

  @amount.to_d
end

#convertBigDecimal

Returns Converted amount of money.

Parameters:

  • amount (Integer, Float, BigDecimal)

    Amount of money to convert

  • currency (String)

    Currency to convert to Lari from

  • date (String)

    Date to get the exchange rate for (default: today)

Returns:

  • (BigDecimal)

    Converted amount of money



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

def convert
  quantity, rate = fetch_currency_data
  result = (amount / quantity) * rate
  result.round(2)
end

#currencyObject



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

def currency
  raise UnavailableCurrencyException unless CURRENCIES.include? @currency

  @currency
end

#dateObject



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

def date
  @date ||= parse_date
end