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.
- Uses persistant http connections.
- Uses
OpenSSL::SSL::VERIFY_PEERto always validate SSL certificates. - Uses compression if enabled (see OandaAPI::Configuration#use_compression).
- Uses request rate limiting if enabled (see OandaAPI::Configuration#use_request_throttling).
Defined Under Namespace
Classes: JsonParser, 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
- .last_request_at ⇒ Object
- .last_request_at=(value) ⇒ Object
-
.last_throttled_at ⇒ Time?
The local time of the most recently throttled request.
-
.map_method_to_http_verb(method) ⇒ Symbol
Maps An API action to a corresponding http verb.
-
.throttle_request_rate ⇒ void
Limits the execution rate of consecutive requests.
Instance Method Summary collapse
-
#api_uri(path) ⇒ String
Returns an absolute URI for a resource request.
-
#execute_request(method, path, conditions = {}) ⇒ OandaAPI::ResourceBase, OandaAPI::ResourceCollection
Executes an http request.
-
#initialize(options = {}) ⇒ OandaAPI::Client
Common initializations.
-
#load_persistent_connection_adapter(options = {}) ⇒ void
Binds a persistent connection adapter.
-
#proc ⇒ String
Camelizes keys and transforms array values into comma-delimited strings.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ NamespaceProxy (private)
Enables method-chaining.
212 213 214 |
# File 'lib/oanda_api/client/client.rb', line 212 def method_missing(sym, *args) NamespaceProxy.new self, sym, args.first end |
Class Method Details
.last_request_at ⇒ Object
142 143 144 |
# File 'lib/oanda_api/client/client.rb', line 142 def self.last_request_at @throttle_mutex.synchronize { @last_request_at } end |
.last_request_at=(value) ⇒ Object
146 147 148 |
# File 'lib/oanda_api/client/client.rb', line 146 def self.last_request_at=(value) @throttle_mutex.synchronize { @last_request_at = value } end |
.last_throttled_at ⇒ Time?
The local time of the most recently throttled request.
171 172 173 |
# File 'lib/oanda_api/client/client.rb', line 171 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.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/oanda_api/client/client.rb', line 129 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_rate ⇒ void
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.
156 157 158 159 160 161 162 |
# File 'lib/oanda_api/client/client.rb', line 156 def self.throttle_request_rate now = Time.now delta = now - (last_request_at || now) _throttle(delta, now) if delta < OandaAPI.configuration.min_request_interval && OandaAPI.configuration.use_request_throttling? last_request_at = Time.now end |
Instance Method Details
#api_uri(path) ⇒ String
Returns an absolute URI for a resource request.
64 65 66 67 |
# File 'lib/oanda_api/client/client.rb', line 64 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/oanda_api/client/client.rb', line 104 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. end |
#initialize(options = {}) ⇒ OandaAPI::Client
Common initializations
54 55 56 57 |
# File 'lib/oanda_api/client/client.rb', line 54 def initialize(={}) super() load_persistent_connection_adapter [: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.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/oanda_api/client/client.rb', line 74 def load_persistent_connection_adapter(={}) adapter_config = { name: "oanda_api", idle_timeout: 10, keep_alive: 30, warn_timeout: 2, pool_size: OandaAPI.configuration.connection_pool_size, }.merge Client.persistent_connection_adapter adapter_config end |
#proc ⇒ String
Camelizes keys and transforms array values into comma-delimited strings.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/oanda_api/client/client.rb', line 35 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("&") } |