Class: OandaApiV20::Client
- Inherits:
-
Object
- Object
- OandaApiV20::Client
- 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
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#connection_pool_size ⇒ Object
Returns the value of attribute connection_pool_size.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#max_requests_per_second ⇒ Object
Returns the value of attribute max_requests_per_second.
-
#proxy_url ⇒ Object
Returns the value of attribute proxy_url.
Instance Method Summary collapse
- #govern_api_request_rate ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, *args, &block) ⇒ Object
- #update_last_api_request_at ⇒ Object
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( = {}) .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 = [:practice] == true ? BASE_URI[:practice] : BASE_URI[:live] @base_uri = [: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 = { name: 'oanda_api_v20', keep_alive: 30, idle_timeout: 10, warn_timeout: 2, pool_size: connection_pool_size } .merge!(logger: ::Logger.new(STDOUT), debug_output: ::Logger.new(STDOUT)) if debug Client.persistent_connection_adapter() 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_token ⇒ Object
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_uri ⇒ Object (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_size ⇒ Object
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 |
#debug ⇒ Object
Returns the value of attribute debug.
16 17 18 |
# File 'lib/oanda_api_v20/client.rb', line 16 def debug @debug end |
#headers ⇒ Object (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_second ⇒ Object
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_url ⇒ Object
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_rate ⇒ Object
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_at ⇒ Object
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 |