Class: Money::Bank::BlockchainInfoExchangeRates

Inherits:
VariableExchange
  • Object
show all
Defined in:
lib/money/bank/blockchain_info_exchange_rates.rb

Constant Summary collapse

TICKER_URL =
'https://blockchain.info/ticker'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bi_ratesObject (readonly)

Returns the value of attribute bi_rates.



14
15
16
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 14

def bi_rates
  @bi_rates
end

#cacheObject

Returns the value of attribute cache.



12
13
14
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 12

def cache
  @cache
end

#rates_expirationObject (readonly)

Returns the value of attribute rates_expiration.



14
15
16
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 14

def rates_expiration
  @rates_expiration
end

#ttl_in_secondsObject

Returns the value of attribute ttl_in_seconds.



14
15
16
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 14

def ttl_in_seconds
  @ttl_in_seconds
end

Instance Method Details

#expire_ratesObject



44
45
46
47
48
49
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 44

def expire_rates
  if ttl_in_seconds && rates_expiration <= Time.now
    update_rates
    refresh_rates_expiration
  end
end

#get_rate(from_currency, to_currency, opts = {}) ⇒ Object



39
40
41
42
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 39

def get_rate(from_currency, to_currency, opts = {})
  expire_rates
  super
end

#save_ratesObject



29
30
31
32
33
34
35
36
37
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 29

def save_rates
  raise InvalidCache unless cache
  text = read_from_url
  if has_valid_rates?(text)
    store_in_cache(text)
  end
rescue Errno::ENOENT
  raise InvalidCache
end

#update_ratesObject



21
22
23
24
25
26
27
# File 'lib/money/bank/blockchain_info_exchange_rates.rb', line 21

def update_rates
  exchange_rates.each do |currency, rate|
  next unless Money::Currency.find(currency)
    set_rate('BTC', currency, rate)
    set_rate(currency, 'BTC', 1.0/rate)
  end
end