Class: Money::Bank::OANDA
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::OANDA
- Defined in:
- lib/oanda_bank.rb
Defined Under Namespace
Classes: FetchError
Constant Summary collapse
- INSTRUMENTS_URL =
"https://api-fxtrade.oanda.com/v1/instruments"
- RATES_URL =
"https://api-fxtrade.oanda.com/v1/prices"
Instance Attribute Summary collapse
-
#rates_expiration ⇒ Time
readonly
Returns the time when the rates expire.
-
#ttl_in_seconds ⇒ Integer
Returns the Time To Live (TTL) in seconds.
Instance Method Summary collapse
- #expire_rates ⇒ Object
- #get_rate(from, to, opts = {}) ⇒ Object
-
#initialize(account_id, access_token, opts = {}) ⇒ OANDA
constructor
A new instance of OANDA.
- #update_rates! ⇒ Object
Constructor Details
#initialize(account_id, access_token, opts = {}) ⇒ OANDA
Returns a new instance of OANDA.
23 24 25 26 27 28 29 |
# File 'lib/oanda_bank.rb', line 23 def initialize(account_id, access_token, opts={}) @client = OAuth2::Client.new(nil, nil) @access_token = OAuth2::AccessToken.new(@client, access_token) @account_id = account_id @updating_mutex = Mutex.new setup end |
Instance Attribute Details
#rates_expiration ⇒ Time (readonly)
Returns the time when the rates expire.
21 22 23 |
# File 'lib/oanda_bank.rb', line 21 def rates_expiration @rates_expiration end |
#ttl_in_seconds ⇒ Integer
Returns the Time To Live (TTL) in seconds.
18 19 20 |
# File 'lib/oanda_bank.rb', line 18 def ttl_in_seconds @ttl_in_seconds end |
Instance Method Details
#expire_rates ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/oanda_bank.rb', line 66 def expire_rates if @ttl_in_seconds && @rates_expiration <= Time.now if @updating_mutex.try_lock begin update_rates! true ensure @updating_mutex.unlock end end else false end end |
#get_rate(from, to, opts = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/oanda_bank.rb', line 43 def get_rate(from, to, opts = {}) expire_rates fn = Proc.new do straight_through = @rates[rate_key_for(from, to)] if straight_through straight_through else to_usd = @rates[rate_key_for(from, 'USD')] from_usd = @rates[rate_key_for('USD', to)] if (to_usd && from_usd) to_usd * from_usd else nil end end end if opts[:without_mutex] fn.call else @mutex.synchronize { fn.call } end end |
#update_rates! ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/oanda_bank.rb', line 31 def update_rates! begin new_rates = fetch_rates @mutex.synchronize do @rates = new_rates refresh_rates_expiration! end rescue StandardError => e raise FetchError.new(e.) end end |