Class: Danconia::Exchanges::CurrencyLayer

Inherits:
Danconia::Exchange show all
Defined in:
lib/danconia/exchanges/currency_layer.rb

Overview

Fetches the rates from currencylayer.com/. The ‘access_key` must be provided. See `examples/currency_layer.rb` for a complete usage example.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Danconia::Exchange

#rate

Constructor Details

#initialize(access_key:, store: Stores::InMemory.new) ⇒ CurrencyLayer

Returns a new instance of CurrencyLayer.



12
13
14
15
# File 'lib/danconia/exchanges/currency_layer.rb', line 12

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

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



10
11
12
# File 'lib/danconia/exchanges/currency_layer.rb', line 10

def store
  @store
end

Instance Method Details

#fetch_ratesObject



17
18
19
20
21
22
23
24
# File 'lib/danconia/exchanges/currency_layer.rb', line 17

def fetch_rates
  response = JSON.parse Net::HTTP.get URI "http://www.apilayer.net/api/live?access_key=#{@access_key}"
  if response['success']
    response['quotes']
  else
    raise Errors::APIError, response
  end
end

#rates(**_opts) ⇒ Object



30
31
32
# File 'lib/danconia/exchanges/currency_layer.rb', line 30

def rates **_opts
  array_of_rates_to_hash @store.rates
end

#update_rates!Object



26
27
28
# File 'lib/danconia/exchanges/currency_layer.rb', line 26

def update_rates!
  @store.save_rates fetch_rates.map { |pair, rate| {pair: pair, rate: rate} }
end