Class: OandaApiV20::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/oanda_api_v20/client.rb

Constant Summary collapse

BASE_URI =
{
  live:     'https://api-fxtrade.oanda.com/v3',
  practice: 'https://api-fxpractice.oanda.com/v3'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/oanda_api_v20/client.rb', line 13

def initialize(options = {})
  options.each do |key, value|
    self.send("#{key}=", value) if self.respond_to?("#{key}=")
  end

  @base_uri = options[:practice] == true ? BASE_URI[:practice] : BASE_URI[:live]

  @headers = {}
  @headers['Authorization']            = "Bearer #{access_token}"
  @headers['X-Accept-Datetime-Format'] = 'RFC3339'
  @headers['Content-Type']             = 'application/json'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oanda_api_v20/client.rb', line 26

def method_missing(name, *args, &block)
  case name
  when :show, :create, :update, :cancel, :close
    set_http_verb(name, last_action)
    api = Api.new(api_attributes)

    if api.respond_to?(last_action)
      response = last_arguments.nil? || last_arguments.empty? ? api.send(last_action, &block) : api.send(last_action, *last_arguments, &block)
      api_result = JSON.parse(response.body)
      set_last_transaction_id(api_result)
    end

    api_result
  when *api_methods
    set_last_action_and_arguments(name, args)
    (args.first) if name == :account
    self
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/oanda_api_v20/client.rb', line 10

def access_token
  @access_token
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



11
12
13
# File 'lib/oanda_api_v20/client.rb', line 11

def base_uri
  @base_uri
end

#headersObject (readonly)

Returns the value of attribute headers.



11
12
13
# File 'lib/oanda_api_v20/client.rb', line 11

def headers
  @headers
end