Class: Tangocard::ExchangeRate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency_code, rate) ⇒ ExchangeRate

Returns a new instance of ExchangeRate.


61
62
63
64
# File 'lib/tangocard/exchange_rate.rb', line 61

def initialize(currency_code, rate)
  @currency_code = currency_code
  @rate = rate.to_f
end

Instance Attribute Details

#currency_codeObject (readonly)

Returns the value of attribute currency_code.


2
3
4
# File 'lib/tangocard/exchange_rate.rb', line 2

def currency_code
  @currency_code
end

#rateObject (readonly)

Returns the value of attribute rate.


2
3
4
# File 'lib/tangocard/exchange_rate.rb', line 2

def rate
  @rate
end

Class Method Details

.allObject

Return an array of all currency exchange rates.

Example:

>> Tangocard::ExchangeRate.all
 => [#<Tangocard::ExchangeRate:0x007ff31ab927a0 @currency_code="USD", @rate="1.00000">,
     #<Tangocard::ExchangeRate:0x007ff31ab92750 @currency_code="JPY", @rate="123.44700">, ...]

Arguments:

none

25
26
27
28
29
# File 'lib/tangocard/exchange_rate.rb', line 25

def self.all
  Tangocard::Raas.rewards_index.parsed_response['xrates']['rates'].map do |currency_code, rate|
    Tangocard::ExchangeRate.new(currency_code, rate)
  end
end

.find(currency_code) ⇒ Object

Find a exchange rate by its currency code.

Example:

>> Tangocard::ExchangeRate.find("EUR")
 => #<Tangocard::ExchangeRate:0x007ff31a2dd808 @currency_code="EUR", @rate="0.88870">

Arguments:

currency_code: (String)

39
40
41
# File 'lib/tangocard/exchange_rate.rb', line 39

def self.find(currency_code)
  self.all.select{|r| r.currency_code == currency_code}.first
end

.populate_money_ratesObject

Set all available exchange rates for Money gem. Once set allows to get reward USD representation of other currencies. For more information and use cases refer to Money gem docs.

Example:

>> Tangocard::ExchangeRate.populate_money_rates
 => true
>> reward.to_money(:denomination)
 => #<Money fractional:500 currency:EUR>
>> reward.to_money(:denomination).exchange_to('USD')
 => #<Money fractional:563 currency:USD>

Arguments:

none

56
57
58
59
# File 'lib/tangocard/exchange_rate.rb', line 56

def self.populate_money_rates
  self.all.each {|r| Money.add_rate(r.currency_code, 'USD', r.inverse_rate)}
  true
end

.timestampObject

Return current currency exchange rate timestamp.

Example:

>> Tangocard::ExchangeRate.timestamp
 => 1456956187

Arguments:

none

12
13
14
# File 'lib/tangocard/exchange_rate.rb', line 12

def self.timestamp
  Tangocard::Raas.rewards_index.parsed_response['xrates']['timestamp']
end

Instance Method Details

#inverse_rateObject

Return an inverse rate of original (float). Used to pupulate Money gem rates.

Arguments:

none

70
71
72
# File 'lib/tangocard/exchange_rate.rb', line 70

def inverse_rate
  1.0 / rate
end