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

Returns a new instance of Exchange.



8
9
10
# File 'lib/danconia/exchanges/exchange.rb', line 8

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

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#rate(from, to) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/danconia/exchanges/exchange.rb', line 12

def rate from, to
  return 1.0 if from == to

  pair = Pair.new(from, to)
  rates = direct_and_inverted_rates()
  rates[pair] or indirect_rate(pair, rates) or raise Errors::ExchangeRateNotFound.new(from, to)
end

#ratesObject



20
21
22
# File 'lib/danconia/exchanges/exchange.rb', line 20

def rates
  @store.rates
end

#update_rates!Object



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

def update_rates!
  @store.save_rates fetch_rates
end