oanda_api_v20 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c9ce33c83a1acdd9dc8bde4d5a20f6114d5504d
4
+ data.tar.gz: 4a16b74d5edf031d265b7290ac3767e06051fc2f
5
+ SHA512:
6
+ metadata.gz: a7ded07fe6f08a1973aab921ee162274b8a320d55d8610d3dcccfd664f4f015d037f371f8dac13d0d4b291e578f182d45e102964c6d94cd34118e8000e4f6a15
7
+ data.tar.gz: 226e9282b316e6479c0a0ba91bd635471b5d1dd63cc5b49ec880902156cf42adaad47ffb605f43370242058ea1e0170badf4caf5abe46e25505e779d51edaccd
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .byebug_history
2
+ oanda_api_v20-0.0.0.gem
@@ -0,0 +1,35 @@
1
+ # @see http://developer.oanda.com/rest-live-v20/account-ep/
2
+ module OandaApiV20
3
+ module Accounts
4
+ # GET /v3/accounts/:account_id
5
+ def account(id)
6
+ Client.send(http_verb, "#{base_uri}/accounts/#{id}", headers: headers)
7
+ end
8
+
9
+ # GET /v3/accounts
10
+ def accounts
11
+ Client.send(http_verb, "#{base_uri}/accounts", headers: headers)
12
+ end
13
+
14
+ # GET /v3/accounts/:account_id/summary
15
+ def summary
16
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/summary", headers: headers)
17
+ end
18
+
19
+ # GET /v3/accounts/:account_id/instruments
20
+ def instruments
21
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/instruments", headers: headers)
22
+ end
23
+
24
+ # GET /v3/accounts/:account_id/changes
25
+ def changes
26
+ options = { 'sinceTransactionID' => last_transaction_id }
27
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/changes", headers: headers, query: options)
28
+ end
29
+
30
+ # PATCH /v3/accounts/:account_id/configuration
31
+ def configuration(options = {})
32
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/configuration", headers: headers, body: options.to_json)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ module OandaApiV20
2
+ class Api
3
+ include Accounts
4
+ include Orders
5
+ include Trades
6
+ include Positions
7
+ include Transactions
8
+ include Pricing
9
+
10
+ attr_accessor :http_verb, :base_uri, :headers, :account_id, :last_transaction_id
11
+
12
+ def initialize(options = {})
13
+ options.each do |key, value|
14
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,98 @@
1
+ module OandaApiV20
2
+ class Client
3
+ include HTTParty
4
+
5
+ BASE_URI = {
6
+ live: 'https://api-fxtrade.oanda.com/v3',
7
+ practice: 'https://api-fxpractice.oanda.com/v3'
8
+ }
9
+
10
+ attr_accessor :access_token
11
+ attr_reader :base_uri, :headers
12
+
13
+ def initialize(options = {})
14
+ options.each do |key, value|
15
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
16
+ end
17
+
18
+ @base_uri = options[:practice] == true ? BASE_URI[:practice] : BASE_URI[:live]
19
+
20
+ @headers = {}
21
+ @headers['Authorization'] = "Bearer #{access_token}"
22
+ @headers['X-Accept-Datetime-Format'] = 'RFC3339'
23
+ @headers['Content-Type'] = 'application/json'
24
+ end
25
+
26
+ def method_missing(name, *args, &block)
27
+ case name
28
+ when :show, :create, :update, :cancel, :close
29
+ set_http_verb(name, last_action)
30
+ api = Api.new(api_attributes)
31
+
32
+ if api.respond_to?(last_action)
33
+ response = last_arguments.nil? || last_arguments.empty? ? api.send(last_action, &block) : api.send(last_action, *last_arguments, &block)
34
+ api_result = JSON.parse(response.body)
35
+ set_last_transaction_id(api_result)
36
+ end
37
+
38
+ api_result
39
+ when *api_methods
40
+ set_last_action_and_arguments(name, args)
41
+ set_account_id(args.first) if name == :account
42
+ self
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ attr_accessor :http_verb, :account_id, :last_transaction_id, :last_action, :last_arguments
49
+
50
+ def api_methods
51
+ Accounts.instance_methods + Orders.instance_methods + Trades.instance_methods + Positions.instance_methods + Transactions.instance_methods + Pricing.instance_methods
52
+ end
53
+
54
+ def set_last_action_and_arguments(action, args)
55
+ set_last_action(action)
56
+ set_last_arguments(args)
57
+ end
58
+
59
+ def set_last_action(action)
60
+ self.last_action = action
61
+ end
62
+
63
+ def set_last_arguments(args)
64
+ self.last_arguments = args.nil? || args.empty? ? nil : args.flatten
65
+ end
66
+
67
+ def set_account_id(id)
68
+ self.account_id = id
69
+ end
70
+
71
+ def set_last_transaction_id(api_result)
72
+ self.last_transaction_id = api_result['lastTransactionID'] if api_result['lastTransactionID']
73
+ end
74
+
75
+ def set_http_verb(action, last_action)
76
+ case action
77
+ when :show
78
+ self.http_verb = :get
79
+ when :update, :cancel, :close
80
+ [:configuration].include?(last_action) ? self.http_verb = :patch : self.http_verb = :put
81
+ when :create
82
+ self.http_verb = :post
83
+ else
84
+ self.http_verb = nil
85
+ end
86
+ end
87
+
88
+ def api_attributes
89
+ {
90
+ http_verb: http_verb,
91
+ base_uri: base_uri,
92
+ headers: headers,
93
+ account_id: account_id,
94
+ last_transaction_id: last_transaction_id
95
+ }
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,7 @@
1
+ module OandaApiV20
2
+ class << self
3
+ def new(options = {})
4
+ Client.new(options)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ # @see http://developer.oanda.com/rest-live-v20/orders-ep/
2
+ module OandaApiV20
3
+ module Orders
4
+ # POST /v3/accounts/:account_id/orders
5
+ # GET /v3/accounts/:account_id/orders/:order_id
6
+ # PUT /v3/accounts/:account_id/orders/:order_id
7
+ # PUT /v3/accounts/:account_id/orders/:order_id/clientExtensions
8
+ # PUT /v3/accounts/:account_id/orders/:order_id/cancel
9
+ def order(*args)
10
+ id_or_options = args.shift
11
+ id_or_options.is_a?(Hash) ? options = id_or_options : id = id_or_options
12
+ options = args.shift unless args.nil? || args.empty?
13
+
14
+ url = id ? "#{base_uri}/accounts/#{account_id}/orders/#{id}" : "#{base_uri}/accounts/#{account_id}/orders"
15
+ url = order_url_for_put(url, options) if http_verb == :put
16
+
17
+ options ? Client.send(http_verb, url, headers: headers, body: options.to_json) : Client.send(http_verb, url, headers: headers)
18
+ end
19
+
20
+ # GET /v3/accounts/:account_id/orders
21
+ def orders
22
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/orders", headers: headers)
23
+ end
24
+
25
+ # GET /v3/accounts/:account_id/pendingOrders
26
+ def pending_orders
27
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/pendingOrders", headers: headers)
28
+ end
29
+
30
+ private
31
+
32
+ def order_url_for_put(url, options = nil)
33
+ return "#{url}/cancel" unless options
34
+ return "#{url}/clientExtensions" if options['clientExtensions'] || options['tradeClientExtensions']
35
+ return url if options['order']
36
+ return url
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,33 @@
1
+ # @see http://developer.oanda.com/rest-live-v20/positions-ep/
2
+ module OandaApiV20
3
+ module Positions
4
+ # GET /v3/accounts/:account_id/positions/:instrument
5
+ def position(*args)
6
+ instrument = args.shift
7
+ options = args.shift unless args.nil? || args.empty?
8
+
9
+ url = "#{base_uri}/accounts/#{account_id}/positions/#{instrument}"
10
+ url = position_url_for_put(url, options) if http_verb == :put
11
+
12
+ options ? Client.send(http_verb, url, headers: headers, body: options.to_json) : Client.send(http_verb, url, headers: headers)
13
+ end
14
+
15
+ # GET /v3/accounts/:account_id/positions
16
+ def positions
17
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/positions", headers: headers)
18
+ end
19
+
20
+ # GET /v3/accounts/:account_id/openPositions
21
+ def open_positions
22
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/openPositions", headers: headers)
23
+ end
24
+
25
+ private
26
+
27
+ def position_url_for_put(url, options = nil)
28
+ return "#{url}/close" unless options
29
+ return "#{url}/close" if options['longUnits'] || options['longClientExtensions'] || options['shortUnits'] || options['shortClientExtensions']
30
+ return url
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ # @see http://developer.oanda.com/rest-live-v20/pricing-ep/
2
+ module OandaApiV20
3
+ module Pricing
4
+ # GET /v3/accounts/:account_id/pricing
5
+ def pricing(options)
6
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/pricing", headers: headers, query: options)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,38 @@
1
+ # @see http://developer.oanda.com/rest-live-v20/trades-ep/
2
+ module OandaApiV20
3
+ module Trades
4
+ # GET /v3/accounts/:account_id/trades/:trade_id
5
+ # PUT /v3/accounts/:account_id/trades/:trade_id/orders
6
+ # PUT /v3/accounts/:account_id/trades/:trade_id/clientExtensions
7
+ # PUT /v3/accounts/:account_id/trades/:trade_id/close
8
+ def trade(*args)
9
+ id = args.shift
10
+ options = args.shift unless args.nil? || args.empty?
11
+
12
+ url = "#{base_uri}/accounts/#{account_id}/trades/#{id}"
13
+ url = trade_url_for_put(url, options) if http_verb == :put
14
+
15
+ options ? Client.send(http_verb, url, headers: headers, body: options.to_json) : Client.send(http_verb, url, headers: headers)
16
+ end
17
+
18
+ # GET /v3/accounts/:account_id/trades
19
+ def trades(options)
20
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/trades", headers: headers, query: options)
21
+ end
22
+
23
+ # GET /v3/accounts/:account_id/openTrades
24
+ def open_trades
25
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/openTrades", headers: headers)
26
+ end
27
+
28
+ private
29
+
30
+ def trade_url_for_put(url, options = nil)
31
+ return "#{url}/close" unless options
32
+ return "#{url}/clientExtensions" if options['clientExtensions']
33
+ return "#{url}/orders" if options['takeProfit'] || options['stopLoss'] || options['trailingStopLoss']
34
+ return "#{url}/close" if options['units']
35
+ return url
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,24 @@
1
+ # @see http://developer.oanda.com/rest-live-v20/transactions-ep/
2
+ module OandaApiV20
3
+ module Transactions
4
+ # GET /v3/accounts/:account_id/transactions/:transaction_id
5
+ def transaction(id)
6
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/transactions/#{id}", headers: headers)
7
+ end
8
+
9
+ # GET /v3/accounts/:account_id/transactions
10
+ def transactions(options = {})
11
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/transactions", headers: headers, query: options)
12
+ end
13
+
14
+ # GET /v3/accounts/:account_id/transactions/idrange
15
+ def transactions_id_range(options)
16
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/transactions/idrange", headers: headers, query: options)
17
+ end
18
+
19
+ # GET /v3/accounts/:account_id/transactions/sinceid
20
+ def transactions_since_id(options)
21
+ Client.send(http_verb, "#{base_uri}/accounts/#{account_id}/transactions/sinceid", headers: headers, query: options)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module OandaApiV20
2
+ VERSION = '0.0.0'
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'httparty'
2
+ require 'persistent_httparty'
3
+
4
+ require 'oanda_api_v20/version'
5
+ require 'oanda_api_v20/accounts'
6
+ require 'oanda_api_v20/orders'
7
+ require 'oanda_api_v20/trades'
8
+ require 'oanda_api_v20/positions'
9
+ require 'oanda_api_v20/transactions'
10
+ require 'oanda_api_v20/pricing'
11
+ require 'oanda_api_v20/client'
12
+ require 'oanda_api_v20/api'
13
+ require 'oanda_api_v20/oanda_api_v20'
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'oanda_api_v20/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'oanda_api_v20'
7
+ s.version = OandaApiV20::VERSION
8
+ s.date = '2016-06-16'
9
+ s.summary = %q{Ruby Oanda REST API V20}
10
+ s.description = %q{Ruby client that supports the Oanda REST API V20 methods.}
11
+ s.authors = ['Kobus Joubert']
12
+ s.email = 'kobus@translate3d.com'
13
+ s.homepage = 'http://rubygems.org/gems/oanda_api_v20'
14
+ s.license = 'MIT'
15
+
16
+ s.required_ruby_version = '>= 2.3.0'
17
+
18
+ s.add_dependency 'httparty', '~> 0.13'
19
+ s.add_dependency 'persistent_httparty', '~> 0.1'
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.require_paths = ['lib']
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oanda_api_v20
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kobus Joubert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: persistent_httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ description: Ruby client that supports the Oanda REST API V20 methods.
42
+ email: kobus@translate3d.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - lib/oanda_api_v20.rb
49
+ - lib/oanda_api_v20/accounts.rb
50
+ - lib/oanda_api_v20/api.rb
51
+ - lib/oanda_api_v20/client.rb
52
+ - lib/oanda_api_v20/oanda_api_v20.rb
53
+ - lib/oanda_api_v20/orders.rb
54
+ - lib/oanda_api_v20/positions.rb
55
+ - lib/oanda_api_v20/pricing.rb
56
+ - lib/oanda_api_v20/trades.rb
57
+ - lib/oanda_api_v20/transactions.rb
58
+ - lib/oanda_api_v20/version.rb
59
+ - oanda_api_v20.gemspec
60
+ homepage: http://rubygems.org/gems/oanda_api_v20
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.3.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Ruby Oanda REST API V20
84
+ test_files: []