Module: OandaAPI::Client

Includes:
HTTParty
Included in:
TokenClient, UsernameClient
Defined in:
lib/oanda_api/client/client.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: NamespaceProxy, ResourceDescriptor, TokenClient, UsernameClient

Constant Summary collapse

BASE_URI =

Resource URI templates

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

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.


171
172
173
# File 'lib/oanda_api/client/client.rb', line 171

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

Class Method Details

.last_throttled_atTime?

The local time of the most recently throttled request.


133
134
135
# File 'lib/oanda_api/client/client.rb', line 133

def self.last_throttled_at
  Thread.current[:oanda_api_throttled_at]
end

.map_method_to_http_verb(method) ⇒ Symbol

Maps An API action to a corresponding http verb.


98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/oanda_api/client/client.rb', line 98

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.


117
118
119
120
121
122
123
124
# File 'lib/oanda_api/client/client.rb', line 117

def self.throttle_request_rate
  now = Time.now
  Thread.current[:oanda_api_last_request_at] ||= now
  delta = now - Thread.current[:oanda_api_last_request_at]
  _throttle(now) if delta < OandaAPI.configuration.min_request_interval &&
                    OandaAPI.configuration.use_request_throttling?
  Thread.current[:oanda_api_last_request_at] = Time.now
end

Instance Method Details

#api_uri(path) ⇒ String

Returns an absolute URI for a resource request.


50
51
52
53
# File 'lib/oanda_api/client/client.rb', line 50

def api_uri(path)
  uri = "#{BASE_URI[domain]}#{path}"
  uri.sub "[API_VERSION]", OandaAPI.configuration.rest_api_version
end

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

Executes an http request.

Raises:


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

def execute_request(method, path, conditions = {})
  response = Http::Exceptions.wrap_and_check do
    method = Client.map_method_to_http_verb(method)
    params_key = [:post, :patch, :put].include?(method) ? :body : :query
    Client.throttle_request_rate
    Client.send method,
                api_uri(path),
                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
  end

  handle_response response, ResourceDescriptor.new(path, method)
  rescue Http::Exceptions::HttpException => e
    raise OandaAPI::RequestError, e.message
end

#procString

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


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/oanda_api/client/client.rb', line 31

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("&")
}