class Straight::ExchangeRate::Adapter
Public Class Methods
new(rates_expire_in: 1800)
click to toggle source
# File lib/straight/exchange_rate_adapter.rb, line 11 def initialize(rates_expire_in: 1800) @rates_expire_in = rates_expire_in # in seconds end
Public Instance Methods
convert_from_currency(amount_in_currency, btc_denomination: :satoshi, currency: 'USD')
click to toggle source
# File lib/straight/exchange_rate_adapter.rb, line 15 def convert_from_currency(amount_in_currency, btc_denomination: :satoshi, currency: 'USD') btc_amount = amount_in_currency.to_f/rate_for(currency) Satoshi.new(btc_amount, from_unit: :btc, to_unit: btc_denomination).to_unit end
convert_to_currency(amount, btc_denomination: :satoshi, currency: 'USD')
click to toggle source
# File lib/straight/exchange_rate_adapter.rb, line 20 def convert_to_currency(amount, btc_denomination: :satoshi, currency: 'USD') amount_in_btc = Satoshi.new(amount.to_f, from_unit: btc_denomination).to_btc amount_in_btc*rate_for(currency) end
fetch_rates!()
click to toggle source
# File lib/straight/exchange_rate_adapter.rb, line 25 def fetch_rates! raise "FETCH_URL is not defined!" unless self.class::FETCH_URL uri = URI.parse(self.class::FETCH_URL) begin @rates = JSON.parse(uri.read(read_timeout: 4)) @rates_updated_at = Time.now rescue OpenURI::HTTPError => e raise FetchingFailed end end
get_rate_value_from_hash(rates_hash, *keys)
click to toggle source
This method will get value we are interested in from hash and prevent failing with 'undefined method [] for Nil' if at some point hash doesn't have such key value pair
# File lib/straight/exchange_rate_adapter.rb, line 45 def get_rate_value_from_hash(rates_hash, *keys) keys.inject(rates_hash) do |intermediate, key| if intermediate.respond_to?(:[]) intermediate[key] else raise CurrencyNotSupported end end end
rate_for(currency_code)
click to toggle source
# File lib/straight/exchange_rate_adapter.rb, line 36 def rate_for(currency_code) if !@rates_updated_at || (Time.now - @rates_updated_at) > @rates_expire_in fetch_rates! end nil # this should be changed in descendant classes end
rate_to_f(rate)
click to toggle source
We dont want to have false positive rate, because nil.to_f is 0.0 This method checks that rate value is not nil
# File lib/straight/exchange_rate_adapter.rb, line 57 def rate_to_f(rate) rate ? rate.to_f : raise(CurrencyNotSupported) end