class OandaRubyClient::ExchangeRatesClient

Attributes

api_key[RW]
use_ssl[RW]

Public Class Methods

new(api_key = ENV['OANDA_RUBY_CLIENT_API_KEY'], use_ssl = true) click to toggle source

@param api_key [String] The API key for the Excange Rates API. @param use_ssl [Boolean] If true, sets the URL endpoint scheme to https. If

+false+, sets the scheme to +http+.
# File lib/oanda_ruby_client/exchange_rates_client.rb, line 11
def initialize(api_key = ENV['OANDA_RUBY_CLIENT_API_KEY'], use_ssl = true)
  @api_key, @use_ssl = api_key, use_ssl
end

Public Instance Methods

currencies() click to toggle source

@return [Hash<String, String>] a hash of the supported currency codes to their descriptions

# File lib/oanda_ruby_client/exchange_rates_client.rb, line 16
def currencies
  currencies_response = HTTParty.get("#{oanda_endpoint}#{CURRENCIES_PATH}", headers: oanda_headers)
  handle_response(currencies_response.response)
  result = {}
  currencies_response.parsed_response['currencies'].each do |currency|
    result[currency['code']] = currency['description']
  end
  result
end
rates(rates_request) click to toggle source

Returns the exchanges rates based on the specified request

@param rates_request [OandaRubyClient::RatesRequest] Request criteria @return [OpenStruct] An object structured similarly to the response from the Exchange Rate API

# File lib/oanda_ruby_client/exchange_rates_client.rb, line 30
def rates(rates_request)
  rates_uri = "#{oanda_endpoint}#{RATES_BASE_PATH}#{rates_request.base_currency}.json?#{rates_request.query_string}"
  rates_response = HTTParty.get(rates_uri, headers: oanda_headers)
  handle_response(rates_response.response)
  OpenStruct.new(rates_response.parsed_response)
end
remaining_quotes() click to toggle source

Returns the number of remaining quote requests

@return [Fixnum, ‘unlimited’] Number of remaining quote requests

# File lib/oanda_ruby_client/exchange_rates_client.rb, line 40
def remaining_quotes
  remaining_quotes_response = HTTParty.get("#{oanda_endpoint}#{REMAINING_QUOTES_PATH}", headers: oanda_headers)
  handle_response(remaining_quotes_response.response)
  remaining_quotes_response.parsed_response['remaining_quotes']
end

Private Instance Methods

handle_response(response) click to toggle source
# File lib/oanda_ruby_client/exchange_rates_client.rb, line 56
def handle_response(response)
  if response.code_type != Net::HTTPOK
    if response.body
      raise JSON.parse(response.body)['message']
    else 
      raise response
    end
  end
end
oanda_endpoint() click to toggle source
# File lib/oanda_ruby_client/exchange_rates_client.rb, line 70
def oanda_endpoint
  "#{scheme}#{OANDA_ENDPOINT}"
end
oanda_headers() click to toggle source
# File lib/oanda_ruby_client/exchange_rates_client.rb, line 66
def oanda_headers
  { 'Authorization' => "Bearer #{@api_key}"}
end
scheme() click to toggle source
# File lib/oanda_ruby_client/exchange_rates_client.rb, line 74
def scheme
  @use_ssl ? 'https:' : 'http:'
end