class Money::Bank::BlockchainInfoExchangeRates
Constants
- TICKER_URL
Attributes
bi_rates[R]
cache[RW]
rates_expiration[R]
ttl_in_seconds[R]
Public Instance Methods
expire_rates()
click to toggle source
# 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 = {})
click to toggle source
Calls superclass method
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 39 def get_rate(from_currency, to_currency, opts = {}) expire_rates super end
save_rates()
click to toggle source
# 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
ttl_in_seconds=(value)
click to toggle source
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 16 def ttl_in_seconds=(value) @ttl_in_seconds = value refresh_rates_expiration if ttl_in_seconds end
update_rates()
click to toggle source
# 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
Protected Instance Methods
exchange_rates()
click to toggle source
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 93 def exchange_rates @doc = JSON.parse(read_from_cache || read_from_url) @bi_rates = @doc @doc end
format_rates(data)
click to toggle source
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 84 def format_rates(data) data = JSON.parse data output = {} data.each do |currency, value| output[currency] = value["last"] end JSON.generate output end
has_valid_rates?(text)
click to toggle source
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 64 def has_valid_rates?(text) parsed = JSON.parse(text) parsed && parsed.has_key?('USD') rescue JSON::ParserError false end
read_from_cache()
click to toggle source
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 71 def read_from_cache if cache.is_a?(Proc) cache.call(nil) elsif cache.is_a?(String) && File.exist?(cache) open(cache).read end end
read_from_url()
click to toggle source
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 79 def read_from_url data = open(TICKER_URL).read format_rates(data) end
refresh_rates_expiration()
click to toggle source
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 99 def refresh_rates_expiration @rates_expiration = Time.now + ttl_in_seconds end
store_in_cache(text)
click to toggle source
Store the provided text data by calling the proc method provided for the cache, or write to the cache file.
# File lib/money/bank/blockchain_info_exchange_rates.rb, line 54 def store_in_cache(text) if cache.is_a?(Proc) cache.call(text) elsif cache.is_a?(String) open(cache, 'w') do |f| f.write(text) end end end