Class: Straight::ExchangeRate::Adapter
- Inherits:
-
Object
- Object
- Straight::ExchangeRate::Adapter
- Includes:
- Singleton
- Defined in:
- lib/straight/exchange_rate_adapter.rb
Direct Known Subclasses
AverageRateAdapter, BitpayAdapter, BitstampAdapter, BtceAdapter, CoinbaseAdapter, KrakenAdapter, LocalbitcoinsAdapter, OkcoinAdapter
Defined Under Namespace
Classes: CurrencyNotSupported, FetchingFailed
Instance Method Summary collapse
- #convert_from_currency(amount_in_currency, btc_denomination: :satoshi, currency: 'USD') ⇒ Object
- #convert_to_currency(amount, btc_denomination: :satoshi, currency: 'USD') ⇒ Object
- #fetch_rates! ⇒ Object
-
#get_rate_value_from_hash(rates_hash, *keys) ⇒ Object
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.
-
#initialize(rates_expire_in: 1800) ⇒ Adapter
constructor
A new instance of Adapter.
- #rate_for(currency_code) ⇒ Object
-
#rate_to_f(rate) ⇒ Object
We dont want to have false positive rate, because nil.to_f is 0.0 This method checks that rate value is not nil.
Constructor Details
#initialize(rates_expire_in: 1800) ⇒ Adapter
Returns a new instance of Adapter.
11 12 13 |
# File 'lib/straight/exchange_rate_adapter.rb', line 11 def initialize(rates_expire_in: 1800) @rates_expire_in = rates_expire_in # in seconds end |
Instance Method Details
#convert_from_currency(amount_in_currency, btc_denomination: :satoshi, currency: 'USD') ⇒ Object
15 16 17 18 |
# 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') ⇒ Object
20 21 22 23 |
# 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! ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# 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) ⇒ Object
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
45 46 47 48 49 50 51 52 53 |
# 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) ⇒ Object
36 37 38 39 40 41 |
# 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) ⇒ Object
We dont want to have false positive rate, because nil.to_f is 0.0 This method checks that rate value is not nil
57 58 59 |
# File 'lib/straight/exchange_rate_adapter.rb', line 57 def rate_to_f(rate) rate ? rate.to_f : raise(CurrencyNotSupported) end |