Class: OandaApiV20::Client

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/oanda_api_v20/client.rb', line 19

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

  @mutex                   = Mutex.new
  @debug                   ||= false
  @connection_pool_size    ||= 2
  @max_requests_per_second ||= 100
  @last_api_request_at     = Array.new(max_requests_per_second)
  uris                     = options[:practice] == true ? BASE_URI[:practice] : BASE_URI[:live]
  @base_uri                = options[:stream] == true ? uris[:stream] : uris[:api]

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

  if proxy_url && uri = URI(proxy_url)
    Client.http_proxy(uri.hostname, uri.port, uri.user, uri.password)
  end

  persistent_connection_adapter_options = {
    name:         'oanda_api_v20',
    keep_alive:   30,
    idle_timeout: 10,
    warn_timeout: 2,
    pool_size:    connection_pool_size
  }

  persistent_connection_adapter_options.merge!(logger: ::Logger.new(STDOUT), debug_output: ::Logger.new(STDOUT)) if debug
  Client.persistent_connection_adapter(persistent_connection_adapter_options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/oanda_api_v20/client.rb', line 53

def method_missing(name, *args, &block)
  case name
  when *Api.api_methods
    api_attributes = {
      client:         self,
      last_action:    name,
      last_arguments: args
    }

    api_attributes.merge!(account_id: args.first) if name == :account
    api_attributes.merge!(instrument: args.first) if name == :instrument

    Api.new(api_attributes)
  else
    super
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



16
17
18
# File 'lib/oanda_api_v20/client.rb', line 16

def access_token
  @access_token
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



17
18
19
# File 'lib/oanda_api_v20/client.rb', line 17

def base_uri
  @base_uri
end

#connection_pool_sizeObject

Returns the value of attribute connection_pool_size.



16
17
18
# File 'lib/oanda_api_v20/client.rb', line 16

def connection_pool_size
  @connection_pool_size
end

#debugObject

Returns the value of attribute debug.



16
17
18
# File 'lib/oanda_api_v20/client.rb', line 16

def debug
  @debug
end

#headersObject (readonly)

Returns the value of attribute headers.



17
18
19
# File 'lib/oanda_api_v20/client.rb', line 17

def headers
  @headers
end

#max_requests_per_secondObject

Returns the value of attribute max_requests_per_second.



16
17
18
# File 'lib/oanda_api_v20/client.rb', line 16

def max_requests_per_second
  @max_requests_per_second
end

#proxy_urlObject

Returns the value of attribute proxy_url.



16
17
18
# File 'lib/oanda_api_v20/client.rb', line 16

def proxy_url
  @proxy_url
end

Instance Method Details

#govern_api_request_rateObject



71
72
73
74
75
# File 'lib/oanda_api_v20/client.rb', line 71

def govern_api_request_rate
  return unless last_api_request_at[0]
  halt = 1 - (last_api_request_at[max_requests_per_second - 1] - last_api_request_at[0])
  sleep halt if halt > 0
end

#update_last_api_request_atObject



77
78
79
80
81
# File 'lib/oanda_api_v20/client.rb', line 77

def update_last_api_request_at
  @mutex.synchronize do
    last_api_request_at.push(Time.now.utc).shift
  end
end