oanda_api_v20 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb4df9b0234d22ffa2e3949f0f2753a268d7ddcc
4
- data.tar.gz: 84480ffb98305fd91f832f453472c80f6a94d508
3
+ metadata.gz: 895ede3c8740f60d446789c78936ce3b57520b44
4
+ data.tar.gz: 1656c6021eb792b24ec0f8f004dec60f908d5dd2
5
5
  SHA512:
6
- metadata.gz: b1850b8f4cda83248d8aec0f68bd44a0974c7784f9865ac776bc61c5e32495f51ca686d95fd71bc7ded214c17b2db69a982d1b38b1c8d6278e25a4ad93ebf358
7
- data.tar.gz: aad4901a5dd0c6260a80c6b4e498db00e9ce98b2cc18e6043f493c363504b54a3dc80eb8037021e28ac73428cdd66a4be2a54cf5fbac6659d14af4a37854dd48
6
+ metadata.gz: 2577d9f7a3c9a4e44c22134a3642486f8d4a94d66df5aa57a68c4fffe7707f7369d8e6b6b1048176f29923e66d8513541203468488b6699ab16eb7ebcaa7d182
7
+ data.tar.gz: 8dabf1dd99dfa19fce1aedffdc943123259a2f61392866fc33ebf4eb9c0afc7e7430a35b9a546f8ef50d9a700a2b899b6927e673c04af214557bb94a758a8923
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.0.4
4
+ #### 2016-08-16
5
+ * Limit Oanda API requests to 30 per second.
6
+
3
7
  ## 0.0.3
4
8
  #### 2016-08-14
5
9
  * Persistent HTTP connections using the persistent_httparty Gem.
data/README.md CHANGED
@@ -265,7 +265,4 @@ client.account('account_id').pricing(options).show
265
265
  There are still a lot to be added to this gem:
266
266
 
267
267
  - Unit tests using RSpec.
268
- - Persistent connections using persistent_httparty.
269
- - No more than 2 connections per second.
270
- - Limit to 30 requests per second.
271
268
  - ...
@@ -2,6 +2,8 @@ module OandaApiV20
2
2
  class Client
3
3
  include HTTParty
4
4
 
5
+ MAX_REQUESTS_PER_SECOND_ALLOWED = 30
6
+
5
7
  BASE_URI = {
6
8
  live: 'https://api-fxtrade.oanda.com/v3',
7
9
  practice: 'https://api-fxpractice.oanda.com/v3'
@@ -15,6 +17,8 @@ module OandaApiV20
15
17
  self.send("#{key}=", value) if self.respond_to?("#{key}=")
16
18
  end
17
19
 
20
+ @debug = options[:debug] || false
21
+ @last_api_request_at = Array.new(MAX_REQUESTS_PER_SECOND_ALLOWED)
18
22
  @base_uri = options[:practice] == true ? BASE_URI[:practice] : BASE_URI[:live]
19
23
 
20
24
  @headers = {}
@@ -22,8 +26,6 @@ module OandaApiV20
22
26
  @headers['X-Accept-Datetime-Format'] = 'RFC3339'
23
27
  @headers['Content-Type'] = 'application/json'
24
28
 
25
- @debug = options[:debug] || false
26
-
27
29
  persistent_connection_adapter_options = {
28
30
  name: 'oanda_api_v20',
29
31
  keep_alive: 30,
@@ -42,6 +44,8 @@ module OandaApiV20
42
44
  api = Api.new(api_attributes)
43
45
 
44
46
  if api.respond_to?(last_action)
47
+ set_last_api_request_at
48
+ govern_api_request_rate
45
49
  response = last_arguments.nil? || last_arguments.empty? ? api.send(last_action, &block) : api.send(last_action, *last_arguments, &block)
46
50
  api_result = JSON.parse(response.body)
47
51
  set_last_transaction_id(api_result)
@@ -57,12 +61,22 @@ module OandaApiV20
57
61
 
58
62
  private
59
63
 
60
- attr_accessor :http_verb, :account_id, :last_transaction_id, :last_action, :last_arguments
64
+ attr_accessor :http_verb, :account_id, :last_transaction_id, :last_action, :last_arguments, :last_api_request_at
61
65
 
62
66
  def api_methods
63
67
  Accounts.instance_methods + Orders.instance_methods + Trades.instance_methods + Positions.instance_methods + Transactions.instance_methods + Pricing.instance_methods
64
68
  end
65
69
 
70
+ def govern_api_request_rate
71
+ return unless last_api_request_at[0]
72
+ halt = 60 - (last_api_request_at[MAX_REQUESTS_PER_SECOND_ALLOWED - 1] - last_api_request_at[0])
73
+ sleep halt if halt > 0
74
+ end
75
+
76
+ def set_last_api_request_at
77
+ last_api_request_at.push(Time.now.utc).shift
78
+ end
79
+
66
80
  def set_last_action_and_arguments(action, args)
67
81
  set_last_action(action)
68
82
  set_last_arguments(args)
@@ -1,3 +1,3 @@
1
1
  module OandaApiV20
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oanda_api_v20
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kobus Joubert