Class: Oanda

Inherits:
Object
  • Object
show all
Defined in:
lib/oanda_exchange/oanda.rb

Constant Summary collapse

PRECISION =
8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def amount
  @amount
end

#base_currencyObject

Returns the value of attribute base_currency.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def base_currency
  @base_currency
end

#dateObject

Returns the value of attribute date.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def date
  @date
end

#daysObject

Returns the value of attribute days.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def days
  @days
end

#interbankObject

Returns the value of attribute interbank.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def interbank
  @interbank
end

#priceObject

Returns the value of attribute price.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def price
  @price
end

#quote_currencyObject

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_hashObject

Returns the value of attribute resp_hash.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def resp_hash
  @resp_hash
end

#responseObject

Returns the value of attribute response.



9
10
11
# File 'lib/oanda_exchange/oanda.rb', line 9

def response
  @response
end

Class Method Details

.currenciesObject



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, options = {})
  new.exchange(base_currency, options)
end

Instance Method Details

#api_request(request_string, &block) ⇒ Object

Raises:

  • (Exception)


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

#currenciesObject



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_requestObject



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

Raises:

  • (Exception)


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, options = {})
  raise Exception.new("no base currency specified")   if base_currency.blank?
  raise Exception.new("no quote currency specified")  if options[:to].blank?
  self.price              = options[:price] == :midpoint ? [:ask, :bid] : [options[:price] || :bid]
  self.base_currency      = base_currency.to_s.upcase
  self.quote_currency     = options[:to].to_s.upcase
  self.date               = options[:date]      || Date.today
  self.days               = options[:days]      || 1
  self.amount             = options[:amount]    || 1
  self.interbank          = options[: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