Module: OandaAPI::Client

Includes:
HTTParty
Included in:
TokenClient, UsernameClient
Defined in:
lib/oanda_api/client/client.rb,
lib/oanda_api/client/json_parser.rb,
lib/oanda_api/client/token_client.rb,
lib/oanda_api/client/namespace_proxy.rb,
lib/oanda_api/client/username_client.rb,
lib/oanda_api/client/resource_descriptor.rb

Overview

Provides everything needed for accessing the API.

Defined Under Namespace

Classes: JsonParser, NamespaceProxy, ResourceDescriptor, TokenClient, UsernameClient

Constant Summary collapse

BASE_URI =

Resource URI templates

{
  live:     "https://api-fxtrade.oanda.com/",
  practice: "https://api-fxpractice.oanda.com/",
  sandbox:  "http://api-sandbox.oanda.com/"
}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ NamespaceProxy (private)

Enables method-chaining.

Returns:



223
224
225
# File 'lib/oanda_api/client/client.rb', line 223

def method_missing(sym, *args)
  NamespaceProxy.new self, sym, args.first
end

Class Method Details

.last_request_atObject



147
148
149
# File 'lib/oanda_api/client/client.rb', line 147

def self.last_request_at
  @throttle_mutex.synchronize { @last_request_at }
end

.last_request_at=(value) ⇒ Object



151
152
153
# File 'lib/oanda_api/client/client.rb', line 151

def self.last_request_at=(value)
  @throttle_mutex.synchronize { @last_request_at = value }
end

.last_throttled_atTime?

The local time of the most recently throttled request.

Returns:

  • (Time)

    if any request has been throttled, the most recent time when one was temporarily suspended.

  • (nil)

    if a request has never been throttled.



181
182
183
# File 'lib/oanda_api/client/client.rb', line 181

def self.last_throttled_at
  @throttle_mutex.synchronize { @throttled_at }
end

.map_method_to_http_verb(method) ⇒ Symbol

Maps An API action to a corresponding http verb.

Parameters:

  • method (Symbol)

    an API action. Supported actions are: :create, :close, :delete, :get, :update.

Returns:

  • (Symbol)

    an http verb.



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/oanda_api/client/client.rb', line 134

def self.map_method_to_http_verb(method)
  case method
  when :create
    :post
  when :close
    :delete
  when :update
    :patch
  else
    method
  end
end

.throttle_request_ratevoid

This method returns an undefined value.

Limits the execution rate of consecutive requests. Specified by OandaAPI::Configuration#max_requests_per_second. Only enforced if OandaAPI::Configuration#use_request_throttling? is enabled.



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/oanda_api/client/client.rb', line 161

def self.throttle_request_rate
  last_request_time = last_request_at
  now = Time.now

  if last_request_time
    delta = now - last_request_time
    _throttle(delta, now) if delta < OandaAPI.configuration.min_request_interval &&
                                     OandaAPI.configuration.use_request_throttling?
  end

  self.last_request_at = Time.now
end

Instance Method Details

#api_uri(resource_descriptor) ⇒ String

Returns an absolute URI for a resource request.

Parameters:

Returns:

  • (String)

    a URI.



66
67
68
69
# File 'lib/oanda_api/client/client.rb', line 66

def api_uri(resource_descriptor)
  api_version = resource_descriptor.labs? ? OandaAPI.configuration.labs_api_version : OandaAPI.configuration.rest_api_version
  "#{BASE_URI[domain]}#{api_version}#{resource_descriptor.path}"
end

#execute_request(method, path, conditions = {}) ⇒ OandaAPI::ResourceBase, OandaAPI::ResourceCollection

Executes an http request.

Parameters:

  • method (Symbol)

    a request action. See map_method_to_http_verb.

  • path (String)

    the path of an Oanda resource request.

  • conditions (Hash) (defaults to: {})

    optional parameters that are converted into either a query string or url form encoded parameters.

Returns:

Raises:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/oanda_api/client/client.rb', line 107

def execute_request(method, path, conditions = {})
  method = Client.map_method_to_http_verb method
  resource_descriptor = ResourceDescriptor.new path, method

  response = Http::Exceptions.wrap_and_check do
    params_key = [:post, :patch, :put].include?(method) ? :body : :query
    Client.throttle_request_rate
    Client.send method,
                api_uri(resource_descriptor),
                params_key    => Utils.stringify_keys(conditions.merge(default_params)),
                :headers      => OandaAPI.configuration.headers.merge(headers),
                :open_timeout => OandaAPI.configuration.open_timeout,
                :read_timeout => OandaAPI.configuration.read_timeout,
                :timeout      => OandaAPI.configuration.open_timeout
  end
  handle_response response, resource_descriptor
  rescue Http::Exceptions::HttpException => e
    raise OandaAPI::RequestError, e.message
end

#initialize(options = {}) ⇒ OandaAPI::Client

Common initializations

Parameters:

  • options (Hash) (defaults to: {})

    Specifies overrides to default settings. Overrides for the persistent connection adapter are specified by including an :connection_adapter_options: {} hash.

Returns:



55
56
57
58
# File 'lib/oanda_api/client/client.rb', line 55

def initialize(options={})
  super()
  load_persistent_connection_adapter options[:connection_adapter_options] || {}
end

#load_persistent_connection_adapter(options = {}) ⇒ void

This method returns an undefined value.

Binds a persistent connection adapter. See documentation for the persistent_httparty gem for configuration details.

Parameters:

  • options (Hash) (defaults to: {})

    Specifies overrides for the connection adapter.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/oanda_api/client/client.rb', line 76

def load_persistent_connection_adapter(options={})
  adapter_config = {
    name:         "oanda_api",
    idle_timeout: 10,
    keep_alive:   30,
    warn_timeout: 2,
    pool_size:    OandaAPI.configuration.connection_pool_size,
    verify_mode:  OpenSSL::SSL::VERIFY_PEER
  }.merge options

  Client.persistent_connection_adapter adapter_config
end

#procString

Camelizes keys and transforms array values into comma-delimited strings.

Returns:

  • (String)

    a url encoded query string.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/oanda_api/client/client.rb', line 36

query_string_normalizer proc { |hash|
  Array(hash).sort_by { |key, _value| key.to_s }.map do |key, value|
    if value.nil?
      Utils.camelize(key.to_s)
    elsif value.respond_to?(:to_ary)
      serialized = URI.encode value.join(","),
                              Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
      "#{Utils.camelize(key)}=#{serialized}"
    else
      HashConversions.to_params Utils.camelize(key) => value
    end
  end.flatten.join("&")
}