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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +0 -3
- data/lib/oanda_api_v20/client.rb +17 -3
- data/lib/oanda_api_v20/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 895ede3c8740f60d446789c78936ce3b57520b44
|
4
|
+
data.tar.gz: 1656c6021eb792b24ec0f8f004dec60f908d5dd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2577d9f7a3c9a4e44c22134a3642486f8d4a94d66df5aa57a68c4fffe7707f7369d8e6b6b1048176f29923e66d8513541203468488b6699ab16eb7ebcaa7d182
|
7
|
+
data.tar.gz: 8dabf1dd99dfa19fce1aedffdc943123259a2f61392866fc33ebf4eb9c0afc7e7430a35b9a546f8ef50d9a700a2b899b6927e673c04af214557bb94a758a8923
|
data/CHANGELOG.md
CHANGED
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
|
- ...
|
data/lib/oanda_api_v20/client.rb
CHANGED
@@ -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)
|