poloniex_api 0.0.1 → 0.0.2

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: b9f6cc08ad5faec147135e5469ba13690c9e1a4d
4
- data.tar.gz: 40139a798ac2ac7a4cc247ebf7fd7ce5f69fc102
3
+ metadata.gz: b9dc6f92ad54feb3b6fd28fc8c68514010a1f20a
4
+ data.tar.gz: 2d0c918530cbac989d065417f35d2c563c840c60
5
5
  SHA512:
6
- metadata.gz: 1fc969743b1352410943254c242ce36c712ba2b436aeb2591795c91685506647a1edeb725a8cfb178d57c7038534ac20a23e89a1acf81c23baa9a08c92b0e57e
7
- data.tar.gz: db528b1d2fcfd611ddb682beb4e6a081f52d4f8e154226c67220798b1d67ff638dbe651ebf185c7a0a4e383087ad00a77efbd91b1ec866eabe615e0ab94c9cb0
6
+ metadata.gz: 4eb532356fc687cfa06ec7dd5261b7f5940d6daf6b8e1a2c35f7ee01f7ab006e6897f4f33c14b105e4d175e49a1d6c29c64ef3ef328721ff9e129bfc6f168a68
7
+ data.tar.gz: fd8c5aa587ff4a7a67b0327ddba0eb125692c9b36cf12b09a23e34c5e0bb8944a107920cba8d0eb4be0d760dff722d822bb44ceb10b8e5fcb63600ceb3c2c446
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## [Unreleased]
5
+
6
+ ## [0.0.2] - 2017-08-29
7
+
8
+ ### Added
9
+ - Wrap get and post calls in retry loops
10
+ - Add timeout values to requests
11
+
12
+ ### Changed
13
+ - Updated `market_trade_hist` to follow the call pattern
14
+
15
+ ## 0.0.1 - 2017-08-27
16
+ ### Added
17
+ - Public and private API commands return JSON response
18
+
19
+ [Unreleased]: https://github.com/brianmcmichael/poloniex_api/compare/v0.0.2...HEAD
20
+ [0.0.2]: https://github.com/brianmcmichael/poloniex_api/compare/v0.0.1...v0.0.2
@@ -1,3 +1,3 @@
1
1
  module Poloniex
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/poloniex.rb CHANGED
@@ -24,10 +24,8 @@
24
24
 
25
25
 
26
26
  # TODO
27
- # [ ] Add retries functionality
28
27
  # [ ] Test withdraw method
29
28
  # [ ] Test margin_buy method
30
- # [ ] Better mapping of _get method
31
29
  # [ ] Clean up the post request
32
30
  # [ ] Refactor market_trade_hist
33
31
  # [ ] Add examples
@@ -41,10 +39,11 @@ require 'logger'
41
39
  require 'uri'
42
40
  require 'base64'
43
41
  require 'digest'
42
+ require 'timeout'
44
43
 
45
44
  module Poloniex
46
45
 
47
- RETRY_DELAYS = [0, 2, 5, 30]
46
+ RETRY_DELAYS = [0, 2, 5, 30, nil]
48
47
 
49
48
  # Possible Commands
50
49
  PUBLIC_COMMANDS = %w(returnTicker return24hVolume returnOrderBook marketTradeHist returnChartData returnCurrencies returnLoanOrders)
@@ -75,9 +74,8 @@ module Poloniex
75
74
  # @param [String] key api key supplied by Poloniex
76
75
  # @param [String] secret hash supplied by Poloniex
77
76
  # @param [int] timeout time in sec to wait for an api response
78
- # (otherwise 'requests.exceptions.Timeout' is raised)
79
77
  # @param [datatype] json_nums to use when parsing json ints and floats
80
- def initialize(key = false, secret = false, timeout = nil, json_nums = false)
78
+ def initialize(key = false, secret = false, timeout = 3, json_nums = false)
81
79
  self.logger = Logger.new(STDOUT)
82
80
 
83
81
  # create nonce
@@ -88,28 +86,6 @@ module Poloniex
88
86
  self.timeout = timeout
89
87
  end
90
88
 
91
- # FIXME make this a decorator
92
- def retry(func)
93
- end
94
-
95
- def retrying(*args, **kwargs)
96
- problems = []
97
- RETRY_DELAYS.each do |delay|
98
- begin
99
- # attempt call
100
- return func(*args, **kwargs)
101
- rescue Poloniex::RequestException.new => problem
102
- problems.push problem
103
- if delay == RETRY_DELAYS.last
104
- raise Poloniex::RetryException.new "Retry delays exhausted #{problem}"
105
- end
106
- end
107
- if problems.any?
108
- LOGGER.debug problems.join("\n")
109
- end
110
- end
111
- end
112
-
113
89
  # """ Main Api Function
114
90
  # - encodes and sends <command> with optional [args] to Poloniex api
115
91
  # - raises 'poloniex.PoloniexError' if an api key or secret is missing
@@ -120,6 +96,8 @@ module Poloniex
120
96
  # Get command type
121
97
  cmd_type = self.check_command(command)
122
98
 
99
+ problems = []
100
+
123
101
  # Pass the command
124
102
  args['command'] = command
125
103
  payload = {}
@@ -150,22 +128,52 @@ module Poloniex
150
128
  'Key' => key
151
129
  }
152
130
 
153
- # Send the call
154
- # FIXME
155
- ret = _post(PRIVATE_API_BASE, args, payload['headers'])
131
+ RETRY_DELAYS.each do |delay|
132
+ begin
133
+ # attempt call
134
+ # Send the call
135
+ ret = _post(PRIVATE_API_BASE, args, payload['headers'])
136
+
137
+ # Return the data
138
+ return self.handle_returned(ret.body)
139
+ rescue Poloniex::RequestException => problem
140
+ problems.push problem
141
+ if delay == RETRY_DELAYS.last
142
+ raise Poloniex::RetryException.new "Retry delays exhausted #{problem}"
143
+ else
144
+ LOGGER.debug(problem)
145
+ LOGGER.info("-- delaying for #{delay} seconds")
146
+ sleep(delay)
147
+ end
148
+ rescue => e
149
+ puts e
150
+ end
151
+
156
152
 
157
- # Return the data
158
- return self.handle_returned(ret.body)
153
+ end
159
154
  end
160
155
  if cmd_type == 'Public'
161
156
 
162
157
  # Encode URL
163
158
  payload['url'] = PUBLIC_API_BASE + URI.encode_www_form(args)
164
159
 
165
- # Send the call
166
- ret = _get(payload['url'])
167
-
168
- return self.handle_returned(ret)
160
+ RETRY_DELAYS.each do |delay|
161
+ begin
162
+ # Send the call
163
+ ret = _get(payload['url'])
164
+
165
+ return self.handle_returned(ret)
166
+ rescue Poloniex::RequestException => problem
167
+ problems.push problem
168
+ if delay == RETRY_DELAYS.last
169
+ raise Poloniex::RetryException.new "Retry delays exhausted #{problem}"
170
+ else
171
+ LOGGER.debug(problem)
172
+ LOGGER.info("-- delaying for #{delay} seconds")
173
+ sleep(delay)
174
+ end
175
+ end
176
+ end
169
177
  end
170
178
  end
171
179
 
@@ -243,11 +251,8 @@ module Poloniex
243
251
  # Returns the past 200 trades for a given market, or up to 50,000
244
252
  # trades between a range specified in UNIX timestamps by the "start" and
245
253
  # "end" parameters.
246
- #
247
- # FIXME: Why is this performing it's own GET? Can this use self.call()?
248
254
  def market_trade_hist(currency_pair, _start: false, _end: false)
249
- args = {
250
- "command" => 'returnTradeHistory',
255
+ args = {
251
256
  "currencyPair" => currency_pair.to_s.upcase
252
257
  }
253
258
  if _start
@@ -256,11 +261,8 @@ module Poloniex
256
261
  if _end
257
262
  args['end'] = _end
258
263
  end
259
- url = URI.parse(PUBLIC_API_BASE)
260
- url.query = URI.encode_www_form(args)
261
- ret = _get(url.to_s, timeout: self.timeout)
262
264
 
263
- self.handle_returned(ret)
265
+ self.call('returnTradeHistory', args)
264
266
  end
265
267
 
266
268
  # Returns candlestick chart data. Parameters are "currencyPair",
@@ -683,14 +685,22 @@ module Poloniex
683
685
  "#{'%.6f' % Time.now.to_f}".gsub('.', '').to_i
684
686
  end
685
687
 
686
- # TODO utilize path and port
687
- def _get(uri, path = nil, port = nil)
688
- address = URI.parse(uri)
689
- Net::HTTP.get(address)
688
+ # Perform the HTTP GET
689
+ def _get(path)
690
+ address = URI.parse(path)
691
+ begin
692
+ Timeout::timeout(self.timeout) {
693
+ Net::HTTP.get(address)
694
+ }
695
+ rescue Timeout::Error
696
+ raise Poloniex::RequestException.new "Request took longer than #{self.timeout} seconds!"
697
+ end
698
+
690
699
  end
691
700
 
692
- def _post(url, data = {}, initheader = nil, dest = nil)
693
- address = URI.parse(url)
701
+ # Perform the HTTP POST
702
+ def _post(path, data = {}, initheader = nil, dest = nil)
703
+ address = URI.parse(path)
694
704
  form_data = data
695
705
  headers = initheader
696
706
 
@@ -698,10 +708,17 @@ module Poloniex
698
708
  http.use_ssl = true
699
709
 
700
710
  request = Net::HTTP::Post.new(address.request_uri, headers)
711
+
701
712
  request.body = URI.encode_www_form(form_data).encode(UTF_8)
702
713
 
703
- response = http.request(request)
704
- response
714
+ begin
715
+ Timeout::timeout(self.timeout) {
716
+ http.request(request)
717
+ }
718
+ rescue Timeout::Error
719
+ raise Poloniex::RequestException.new "Request took longer than #{self.timeout} seconds!"
720
+ end
721
+
705
722
  end
706
723
 
707
724
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poloniex_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian McMichael
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - ".gitignore"
20
+ - CHANGELOG.md
20
21
  - CONTRIBUTING.md
21
22
  - LICENSE
22
23
  - README.md