bitget.rb 0.4.0 → 0.6.0

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
  SHA256:
3
- metadata.gz: a9a76dd0384385ec874d2689053e2a9af7ed9cc4167e9980691bcca9b4855005
4
- data.tar.gz: 5d1f16940c82a9f072360fbecb5941be0df52d3161dab7db217814b5a5a2b00e
3
+ metadata.gz: b73e07027de34083f0b3733d967c45f9556436af137a9b22accb4c7ebccc4403
4
+ data.tar.gz: 59f22d1ae6e505a996d08e89923df0262d1dc65c9f898463d26d154370db1dce
5
5
  SHA512:
6
- metadata.gz: 975b7a27c4ab2834dbcdd907b65d0fe66b17bb7d8a8c2fddee5d4cc6d3f4f2431afd670beee84312d5ff828e7d88a1dcdabb20fa1cb1a32a72a55f8da809c2b5
7
- data.tar.gz: 3f030a15eaf44fd3ccba97ea216a11da4f377135c9c49127b5dbe713d4227357512950ec6ffa4e521bd2d73fed4df903b9a574cf7ea92054110eb2a366f08857
6
+ metadata.gz: 59530b4225ba03fa3c1d0070a4cb7595bc2ec3d679767f4287290beee42a42622a0859599d361ddae04630bbb0066f7dd642b83aec3a871af45fde1d7ea626c8
7
+ data.tar.gz: a6897b20c54c8d996f853d206c252d9c553fe31879d248a1dcfc6d553763b989684763f14bfbc7d87f19ae07bc63af839a57c5236b79284bc7715ee125a83191
data/README.md CHANGED
@@ -30,6 +30,65 @@ bitget_client = Bitget::Client.new(
30
30
  )
31
31
  ```
32
32
 
33
+ ### Configuration
34
+
35
+ Settings may be declared once, and every client built afterwards reads them as its defaults.
36
+
37
+ ```ruby
38
+ Bitget.configure do |config|
39
+ config.api_key = 'api_key0'
40
+ config.api_secret = 'api_secret0'
41
+ config.api_passphrase = 'api_passphrase0'
42
+ end
43
+
44
+ bitget_client = Bitget::Client.new
45
+ ```
46
+
47
+ The settings are `api_key`, `api_secret`, `api_passphrase`, `debug` and `logger`.
48
+
49
+ Any of them may still be given per client, which wins over the configured value. The
50
+ credentials are named arguments; the rest go under `options`.
51
+
52
+ ```ruby
53
+ bitget_client = Bitget::Client.new(
54
+ api_key: 'api_key1',
55
+ options: {logger: Logger.new('bitget.log', 'daily')}
56
+ )
57
+ ```
58
+
59
+ ### Logging
60
+
61
+ A client logs when it has somewhere to log to, and not at all when it does not. Hand it any
62
+ object answering to `#info` and `#error`; the standard library's `Logger` will do.
63
+
64
+ ```ruby
65
+ require 'logger'
66
+
67
+ Bitget.configure do |config|
68
+ config.logger = Logger.new($stdout)
69
+ end
70
+ ```
71
+
72
+ `Logger` rotates by itself, so a daily log file wants no more than its second argument. It
73
+ will not create the directory, so make that first.
74
+
75
+ ```ruby
76
+ require 'fileutils'
77
+ require 'logger'
78
+
79
+ log_file_path = File.expand_path(File.join(%w{~ log bitget log.txt}))
80
+ FileUtils.mkdir_p(File.dirname(log_file_path))
81
+
82
+ Bitget.configure do |config|
83
+ config.logger = Logger.new(log_file_path, 'daily')
84
+ end
85
+ ```
86
+
87
+ Before 0.6.0 the client chose the path, created the directory and rotated the file itself. It
88
+ no longer does any of that: what is logged is the client's business and where it goes is
89
+ yours, which is what lets a StringIO logger in a test, or a levelled one, or something which
90
+ is not a Logger at all, work as well as the above.
91
+
33
92
  ### Retrieve Info on All the Coins Traded
34
93
  ```ruby
35
94
  bitget_client.spot_public_coins
data/bitget.rb.gemspec CHANGED
@@ -4,7 +4,7 @@ Gem::Specification.new do |spec|
4
4
  spec.name = 'bitget.rb'
5
5
 
6
6
  spec.version = Bitget::VERSION
7
- spec.date = '2025-05-16'
7
+ spec.date = '2025-05-26'
8
8
 
9
9
  spec.summary = "Access the Bitget API with Ruby."
10
10
  spec.description = "Access the Bitget API with Ruby."
@@ -0,0 +1,32 @@
1
+ # Bitget/Configuration.rb
2
+ # Bitget::Configuration
3
+
4
+ module Bitget
5
+ class Configuration
6
+ attr_accessor :api_key, :api_secret, :api_passphrase, :debug, :logger
7
+
8
+ def initialize
9
+ @api_key = nil
10
+ @api_secret = nil
11
+ @api_passphrase = nil
12
+ @debug = false
13
+ @logger = nil
14
+ end
15
+ end
16
+
17
+ class << self
18
+ attr_writer :configuration
19
+
20
+ def configuration
21
+ @configuration ||= Configuration.new
22
+ end
23
+
24
+ def configure
25
+ yield(configuration)
26
+ end
27
+
28
+ def reset_configuration!
29
+ @configuration = Configuration.new
30
+ end
31
+ end
32
+ end
data/lib/Bitget/Error.rb CHANGED
@@ -1,9 +1,31 @@
1
1
  # Bitget/Error.rb
2
2
  # Bitget::Error
3
3
 
4
+ require 'json'
5
+
4
6
  module Bitget
5
7
  class Error < RuntimeError
6
- attr_reader :code, :message, :body
8
+ attr_reader\
9
+ :code,
10
+ :message,
11
+ :body
12
+
13
+ # Bitget answers most errors with a JSON body carrying its own code and
14
+ # message, which are more use than the HTTP ones. Not all of them: a 418
15
+ # arrives with an empty body and there is nothing to parse. Where the body
16
+ # says nothing the HTTP code and message answer instead, so that an error
17
+ # renders as something rather than as a pair of blanks.
18
+ def error_code
19
+ @parsed_body['code'] || @code
20
+ end
21
+
22
+ def error_message
23
+ @parsed_body['msg'] || @parsed_body['message'] || @message
24
+ end
25
+
26
+ def to_s
27
+ "Bitget::Error: #{error_code} - #{error_message}"
28
+ end
7
29
 
8
30
  private
9
31
 
@@ -11,6 +33,16 @@ module Bitget
11
33
  @code = code
12
34
  @message = message
13
35
  @body = body
36
+ @parsed_body = parsed(body)
37
+ end
38
+
39
+ # An error is raised in answer to something already having gone wrong, so it
40
+ # must construct from whatever arrived. Raising a JSON::ParserError from
41
+ # here loses the error being reported and replaces it with one about parsing.
42
+ def parsed(body)
43
+ JSON.parse(body)
44
+ rescue JSON::ParserError
45
+ {}
14
46
  end
15
47
  end
16
48
  end