Class: Danconia::Exchanges::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/danconia/exchanges/exchange.rb

Direct Known Subclasses

CurrencyLayer, FixedRates

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store: Stores::InMemory.new) ⇒ Exchange



6
7
8
# File 'lib/danconia/exchanges/exchange.rb', line 6

def initialize store: Stores::InMemory.new
  @store = store
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



4
5
6
# File 'lib/danconia/exchanges/exchange.rb', line 4

def store
  @store
end

Instance Method Details

#rate(from, to) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/danconia/exchanges/exchange.rb', line 10

def rate from, to
  if from == to
    1.0
  elsif from == 'USD' and direct_rate = @store.direct_rate(from, to)
    direct_rate
  elsif to == 'USD' and inverse_rate = @store.direct_rate(to, from)
    (1.0 / inverse_rate).round 6
  elsif from != 'USD' and to != 'USD' and from_in_usd = rate(from, 'USD') and to_per_usd = rate('USD', to)
    (from_in_usd * to_per_usd).round 6
  else
    raise Errors::ExchangeRateNotFound.new(from, to)
  end
end

#ratesObject



24
25
26
# File 'lib/danconia/exchanges/exchange.rb', line 24

def rates
  @store.rates
end

#update_rates!Object



28
29
30
# File 'lib/danconia/exchanges/exchange.rb', line 28

def update_rates!
  @store.save_rates fetch_rates
end