class OandaApiV20::Api

Attributes

account_id[RW]
base_uri[RW]
client[RW]
headers[RW]
http_verb[RW]
instrument[W]
last_action[RW]
last_arguments[RW]

Public Class Methods

api_methods() click to toggle source
# File lib/oanda_api_v20/api.rb, line 25
def api_methods
  Accounts.instance_methods + Instruments.instance_methods + Orders.instance_methods + Trades.instance_methods + Positions.instance_methods + Transactions.instance_methods + Pricing.instance_methods
end
new(options = {}) click to toggle source
# File lib/oanda_api_v20/api.rb, line 14
def initialize(options = {})
  options.each do |key, value|
    self.send("#{key}=", value) if self.respond_to?("#{key}=")
  end

  raise OandaApiV20::ApiError, 'No client object was supplid.' unless client
  @base_uri ||= client.base_uri
  @headers  ||= client.headers
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/oanda_api_v20/api.rb, line 44
def method_missing(name, *args, &block)
  case name
  when :show, :create, :update, :cancel, :close
    set_http_verb(name, last_action)

    if respond_to?(last_action)
      api_result = {}
      client.update_last_api_request_at
      client.govern_api_request_rate

      begin
        response = Http::Exceptions.wrap_and_check do
          last_arguments.nil? || last_arguments.empty? ? send(last_action, &block) : send(last_action, *last_arguments, &block)
        end
      rescue Http::Exceptions::HttpException => e
        raise OandaApiV20::RequestError.new(e.message, response: e.response, original_exception: e.original_exception)
      end

      if response.body && !response.body.empty?
        api_result.merge!(JSON.parse(response.body))
      end
    end

    self.http_verb = nil
    api_result
  else
    super
  end
end

Private Instance Methods

parse(buffer, fragment) { |parse| ... } click to toggle source
# File lib/oanda_api_v20/api.rb, line 96
def parse(buffer, fragment, &block)
  buffer.split("\n").each do |message|
    cleaned_message = message.strip
    next if cleaned_message.empty?
    yield JSON.parse(cleaned_message)
  end
rescue JSON::ParserError => e
  raise OandaApiV20::ParseError, "#{e.message} in '#{fragment}'"
ensure
  buffer.clear
end
set_http_verb(action, last_action) click to toggle source
# File lib/oanda_api_v20/api.rb, line 83
def set_http_verb(action, last_action)
  case action
  when :show
    self.http_verb = :get
  when :update, :cancel, :close
    [:configuration].include?(last_action) ? self.http_verb = :patch : self.http_verb = :put
  when :create
    self.http_verb = :post
  else
    self.http_verb = nil
  end
end
set_last_action_and_arguments(action, *args) click to toggle source
# File lib/oanda_api_v20/api.rb, line 78
def set_last_action_and_arguments(action, *args)
  self.last_action    = action.to_sym
  self.last_arguments = args
end