Class: Oanda
- Inherits:
-
Object
- Object
- Oanda
- Defined in:
- lib/oanda_exchange/oanda.rb
Constant Summary collapse
- PRECISION =
8
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#base_currency ⇒ Object
Returns the value of attribute base_currency.
-
#date ⇒ Object
Returns the value of attribute date.
-
#days ⇒ Object
Returns the value of attribute days.
-
#interbank ⇒ Object
Returns the value of attribute interbank.
-
#price ⇒ Object
Returns the value of attribute price.
-
#quote_currency ⇒ Object
Returns the value of attribute quote_currency.
-
#resp_hash ⇒ Object
Returns the value of attribute resp_hash.
-
#response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
- #api_request(request_string, &block) ⇒ Object
- #currencies ⇒ Object
- #currencies_request ⇒ Object
- #exchange(base_currency, options = {}) ⇒ Object
- #exchange_request(request_string) ⇒ Object
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def amount @amount end |
#base_currency ⇒ Object
Returns the value of attribute base_currency.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def base_currency @base_currency end |
#date ⇒ Object
Returns the value of attribute date.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def date @date end |
#days ⇒ Object
Returns the value of attribute days.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def days @days end |
#interbank ⇒ Object
Returns the value of attribute interbank.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def interbank @interbank end |
#price ⇒ Object
Returns the value of attribute price.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def price @price end |
#quote_currency ⇒ Object
Returns the value of attribute quote_currency.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def quote_currency @quote_currency end |
#resp_hash ⇒ Object
Returns the value of attribute resp_hash.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def resp_hash @resp_hash end |
#response ⇒ Object
Returns the value of attribute response.
9 10 11 |
# File 'lib/oanda_exchange/oanda.rb', line 9 def response @response end |
Class Method Details
.currencies ⇒ Object
17 18 19 |
# File 'lib/oanda_exchange/oanda.rb', line 17 def self.currencies new.currencies end |
.exchange(base_currency, options = {}) ⇒ Object
13 14 15 |
# File 'lib/oanda_exchange/oanda.rb', line 13 def self.exchange(base_currency, = {}) new.exchange(base_currency, ) end |
Instance Method Details
#api_request(request_string, &block) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/oanda_exchange/oanda.rb', line 45 def api_request(request_string, &block) OandaExchange::Config.logger.debug "OANDA API: request body\n#{request_string}" resp = yield request_string OandaExchange::Config.logger.debug "OANDA API: response #{resp.code}\n#{resp.body}" raise Exception.new("Unexpected response code from OANDA API. See log entries with debug level to get more information.") unless resp.code == 200 resp.body end |
#currencies ⇒ Object
38 39 40 41 42 |
# File 'lib/oanda_exchange/oanda.rb', line 38 def currencies self.response = currencies_request self.resp_hash = Hash.from_xml response extract_currencies end |
#currencies_request ⇒ Object
61 62 63 64 65 66 |
# File 'lib/oanda_exchange/oanda.rb', line 61 def currencies_request api_request "" do OandaExchange::Config.logger.debug "OANDA API: request \n#{config[:currencies_url]}" RestClient.get "#{config[:currencies_url]}" end end |
#exchange(base_currency, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/oanda_exchange/oanda.rb', line 21 def exchange(base_currency, = {}) raise Exception.new("no base currency specified") if base_currency.blank? raise Exception.new("no quote currency specified") if [:to].blank? self.price = [:price] == :midpoint ? [:ask, :bid] : [[:price] || :bid] self.base_currency = base_currency.to_s.upcase self.quote_currency = [:to].to_s.upcase self.date = [:date] || Date.today self.days = [:days] || 1 self.amount = [:amount] || 1 self.interbank = [:interbank] || 0 self.response = exchange_request(build_request) self.resp_hash = Hash.from_xml(response)["RESPONSE"] average_rate = calculate_average_rate self.interbank = average_rate * interbank.to_f / 100 base_currency == quote_currency ? average_rate : average_rate - interbank end |
#exchange_request(request_string) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/oanda_exchange/oanda.rb', line 53 def exchange_request(request_string) api_request request_string do OandaExchange::Config.logger.debug "OANDA API: request \n#{config[:exchange_service_url]}" OandaExchange::Config.logger.info "OANDA API: requesting #{base_currency} conversion into #{quote_currency}" RestClient.get "#{config[:exchange_service_url]}", :params => {:fxmlrequest => request_string} end end |