stripe 3.2.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc6007a4a7e4fe882d757b6d98c32a8c4ebe5cd0
4
- data.tar.gz: 11fd5cc602368b14af8e2026d7e4443b1ea5e8be
3
+ metadata.gz: 93c701571b0b8167fe9a2816d458715fe646e0fa
4
+ data.tar.gz: 27a4af6cf8afca19384f7d9901b2980ac8da0d93
5
5
  SHA512:
6
- metadata.gz: '0499ecd568b1dc51b3dba91ca087640d68201858de6d836cd0fbaf4ef8762e7264144ac45cd4e4e5c1c4795aa58bbc81f3b4b9d125006540549e08fe7c16fafb'
7
- data.tar.gz: 59c86405800248611f69e91a55bbf32d3885beb90ee06cd36e89bad93ee84bf0927624f292b694e99c94e286164136aff9da6eb64a33a5f9bfd47fca417444ec
6
+ metadata.gz: f8ebb06861148fe8e40b2737cf5f65f00e11921e6b6d4ddd790126da4d3dfa5be3ed01353a5538bfaccfc8d0fa2027d96b2ae40659e22f8b88a99f897928640f
7
+ data.tar.gz: 1971744e0adf6616d472967740b8bd321cd1dc6d639ec582d436560eca093446021435f702c8662632ece248eff119019322a7e327f8b79bb025877ca589bc89
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 3.3.0 2017-08-11
2
+
3
+ * Add support for standard library logger interface with `Stripe.logger`
4
+ * Error logs now go to stderr if using `Stripe.log_level`/`STRIPE_LOG`
5
+ * `Stripe.log_level`/`STRIPE_LOG` now support `Stipe::LEVEL_ERROR`
6
+
1
7
  === 3.2.0 2017-08-03
2
8
 
3
9
  * Add logging for request retry account and `Stripe-Account` header
data/README.md CHANGED
@@ -150,7 +150,7 @@ There are a few options for enabling it:
150
150
 
151
151
  2. Set `Stripe.log_level`:
152
152
  ``` ruby
153
- Stripe.log_level = "info"
153
+ Stripe.log_level = Stripe::LEVEL_INFO
154
154
  ```
155
155
 
156
156
  ### Writing a Plugin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.2.0
1
+ 3.3.0
data/lib/stripe.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # Stripe Ruby bindings
2
2
  # API spec at https://stripe.com/docs/api
3
3
  require 'cgi'
4
+ require 'logger'
4
5
  require 'openssl'
5
6
  require 'rbconfig'
6
7
  require 'set'
@@ -84,6 +85,7 @@ module Stripe
84
85
  @uploads_base = 'https://uploads.stripe.com'
85
86
 
86
87
  @log_level = nil
88
+ @logger = nil
87
89
 
88
90
  @max_network_retries = 0
89
91
  @max_network_retry_delay = 2
@@ -144,24 +146,51 @@ module Stripe
144
146
  end
145
147
  end
146
148
 
147
- LEVEL_DEBUG = "debug"
148
- LEVEL_INFO = "info"
149
+ # map to the same values as the standard library's logger
150
+ LEVEL_DEBUG = Logger::DEBUG
151
+ LEVEL_ERROR = Logger::ERROR
152
+ LEVEL_INFO = Logger::INFO
149
153
 
150
- # When set prompts the library to log some extra information to $stdout about
151
- # what it's doing. For example, it'll produce information about requests,
152
- # responses, and errors that are received. Valid log levels are `debug` and
153
- # `info`, with `debug` being a little more verbose in places.
154
+ # When set prompts the library to log some extra information to $stdout and
155
+ # $stderr about what it's doing. For example, it'll produce information about
156
+ # requests, responses, and errors that are received. Valid log levels are
157
+ # `debug` and `info`, with `debug` being a little more verbose in places.
158
+ #
159
+ # Use of this configuration is only useful when `.logger` is _not_ set. When
160
+ # it is, the decision what levels to print is entirely deferred to the logger.
154
161
  def self.log_level
155
162
  @log_level
156
163
  end
157
164
 
158
165
  def self.log_level=(val)
159
- if val != nil && ![LEVEL_DEBUG, LEVEL_INFO].include?(val)
166
+ # Backwards compatibility for values that we briefly allowed
167
+ if val == "debug"
168
+ val = LEVEL_DEBUG
169
+ elsif val == "info"
170
+ val = LEVEL_INFO
171
+ end
172
+
173
+ if val != nil && ![LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO].include?(val)
160
174
  raise ArgumentError, "log_level should only be set to `nil`, `debug` or `info`"
161
175
  end
162
176
  @log_level = val
163
177
  end
164
178
 
179
+ # Sets a logger to which logging output will be sent. The logger should
180
+ # support the same interface as the `Logger` class that's part of Ruby's
181
+ # standard library (hint, anything in `Rails.logger` will likely be
182
+ # suitable).
183
+ #
184
+ # If `.logger` is set, the value of `.log_level` is ignored. The decision on
185
+ # what levels to print is entirely deferred to the logger.
186
+ def self.logger
187
+ @logger
188
+ end
189
+
190
+ def self.logger=(val)
191
+ @logger = val
192
+ end
193
+
165
194
  def self.max_network_retries
166
195
  @max_network_retries
167
196
  end
@@ -279,7 +279,7 @@ module Stripe
279
279
  end
280
280
 
281
281
  def specific_api_error(resp, error_data, context)
282
- Util.log_info('Stripe API error',
282
+ Util.log_error('Stripe API error',
283
283
  status: resp.http_status,
284
284
  error_code: error_data['code'],
285
285
  error_message: error_data['message'],
@@ -336,7 +336,7 @@ module Stripe
336
336
  def specific_oauth_error(resp, error_code, context)
337
337
  description = resp.data[:error_description] || error_code
338
338
 
339
- Util.log_info('Stripe OAuth error',
339
+ Util.log_error('Stripe OAuth error',
340
340
  status: resp.http_status,
341
341
  error_code: error_code,
342
342
  error_description: description,
@@ -364,7 +364,7 @@ module Stripe
364
364
  end
365
365
 
366
366
  def handle_network_error(e, context, num_retries, api_base=nil)
367
- Util.log_info('Stripe OAuth error',
367
+ Util.log_error('Stripe network error',
368
368
  error_message: e.message,
369
369
  idempotency_key: context.idempotency_key,
370
370
  request_id: context.request_id
@@ -481,7 +481,7 @@ module Stripe
481
481
  private :log_response
482
482
 
483
483
  def log_response_error(context, request_start, e)
484
- Util.log_info("Request error",
484
+ Util.log_error("Request error",
485
485
  elapsed: Time.now - request_start,
486
486
  error_message: e.message,
487
487
  idempotency_key: context.idempotency_key,
data/lib/stripe/util.rb CHANGED
@@ -89,17 +89,27 @@ module Stripe
89
89
  end
90
90
  end
91
91
 
92
+ def self.log_error(message, data = {})
93
+ if !Stripe.logger.nil? ||
94
+ !Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_ERROR
95
+ log_internal(message, data, color: :cyan,
96
+ level: Stripe::LEVEL_ERROR, logger: Stripe.logger, out: $stderr)
97
+ end
98
+ end
99
+
92
100
  def self.log_info(message, data = {})
93
- if Stripe.log_level == Stripe::LEVEL_DEBUG ||Stripe.log_level == Stripe::LEVEL_INFO
101
+ if !Stripe.logger.nil? ||
102
+ !Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_INFO
94
103
  log_internal(message, data, color: :cyan,
95
- level: Stripe::LEVEL_INFO, out: $stdout)
104
+ level: Stripe::LEVEL_INFO, logger: Stripe.logger, out: $stdout)
96
105
  end
97
106
  end
98
107
 
99
108
  def self.log_debug(message, data = {})
100
- if Stripe.log_level == Stripe::LEVEL_DEBUG
109
+ if !Stripe.logger.nil? ||
110
+ !Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_DEBUG
101
111
  log_internal(message, data, color: :blue,
102
- level: Stripe::LEVEL_DEBUG, out: $stdout)
112
+ level: Stripe::LEVEL_DEBUG, logger: Stripe.logger, out: $stdout)
103
113
  end
104
114
  end
105
115
 
@@ -338,23 +348,39 @@ module Stripe
338
348
  end
339
349
  private_class_method :colorize
340
350
 
351
+ # Turns an integer log level into a printable name.
352
+ def self.level_name(level)
353
+ case level
354
+ when LEVEL_DEBUG then "debug"
355
+ when LEVEL_ERROR then "error"
356
+ when LEVEL_INFO then "info"
357
+ else level
358
+ end
359
+ end
360
+ private_class_method :level_name
361
+
341
362
  # TODO: Make these named required arguments when we drop support for Ruby
342
363
  # 2.0.
343
- def self.log_internal(message, data = {}, color: nil, level: nil, out: nil)
364
+ def self.log_internal(message, data = {}, color: nil, level: nil, logger: nil, out: nil)
344
365
  data_str = data.select { |k,v| !v.nil? }.
345
366
  map { |(k,v)|
346
367
  "%s=%s" % [
347
- colorize(k, color, out.isatty),
368
+ colorize(k, color, !out.nil? && out.isatty),
348
369
  wrap_logfmt_value(v)
349
370
  ]
350
371
  }.join(" ")
351
372
 
352
- if out.isatty
373
+ if !logger.nil?
374
+ # the library's log levels are mapped to the same values as the
375
+ # standard library's logger
376
+ logger.log(level,
377
+ "message=%s %s" % [wrap_logfmt_value(message), data_str])
378
+ elsif out.isatty
353
379
  out.puts "%s %s %s" %
354
- [colorize(level[0, 4].upcase, color, out.isatty), message, data_str]
380
+ [colorize(level_name(level)[0, 4].upcase, color, out.isatty), message, data_str]
355
381
  else
356
382
  out.puts "message=%s level=%s %s" %
357
- [wrap_logfmt_value(message), level, data_str]
383
+ [wrap_logfmt_value(message), level_name(level), data_str]
358
384
  end
359
385
  end
360
386
  private_class_method :log_internal
@@ -1,3 +1,3 @@
1
1
  module Stripe
2
- VERSION = '3.2.0'
2
+ VERSION = '3.3.0'
3
3
  end
@@ -240,7 +240,7 @@ module Stripe
240
240
  param: 'param',
241
241
  type: 'type',
242
242
  }
243
- Util.expects(:log_info).with('Stripe API error',
243
+ Util.expects(:log_error).with('Stripe API error',
244
244
  status: 500,
245
245
  error_code: error['code'],
246
246
  error_message: error['message'],
@@ -282,7 +282,7 @@ module Stripe
282
282
  status: 400
283
283
  )
284
284
 
285
- Util.expects(:log_info).with('Stripe OAuth error',
285
+ Util.expects(:log_error).with('Stripe OAuth error',
286
286
  status: 400,
287
287
  error_code: "invalid_request",
288
288
  error_description: "No grant type specified",
@@ -168,12 +168,16 @@ module Stripe
168
168
  @old_log_level = Stripe.log_level
169
169
  Stripe.log_level = nil
170
170
 
171
+ @old_stderr = $stderr
172
+ $stderr = StringIO.new
173
+
171
174
  @old_stdout = $stdout
172
175
  $stdout = StringIO.new
173
176
  end
174
177
 
175
178
  teardown do
176
179
  Stripe.log_level = @old_log_level
180
+ $stderr = @old_stderr
177
181
  $stdout = @old_stdout
178
182
  end
179
183
 
@@ -189,6 +193,12 @@ module Stripe
189
193
  assert_equal "message=foo level=debug \n", $stdout.string
190
194
  end
191
195
 
196
+ should "not log if level set to error" do
197
+ Stripe.log_level = Stripe::LEVEL_ERROR
198
+ Util.log_debug("foo")
199
+ assert_equal "", $stdout.string
200
+ end
201
+
192
202
  should "not log if level set to info" do
193
203
  Stripe.log_level = Stripe::LEVEL_INFO
194
204
  Util.log_debug("foo")
@@ -196,6 +206,31 @@ module Stripe
196
206
  end
197
207
  end
198
208
 
209
+ context ".log_error" do
210
+ should "not log if logging is disabled" do
211
+ Util.log_error("foo")
212
+ assert_equal "", $stdout.string
213
+ end
214
+
215
+ should "log if level set to debug" do
216
+ Stripe.log_level = Stripe::LEVEL_DEBUG
217
+ Util.log_error("foo")
218
+ assert_equal "message=foo level=error \n", $stderr.string
219
+ end
220
+
221
+ should "log if level set to error" do
222
+ Stripe.log_level = Stripe::LEVEL_ERROR
223
+ Util.log_error("foo")
224
+ assert_equal "message=foo level=error \n", $stderr.string
225
+ end
226
+
227
+ should "log if level set to info" do
228
+ Stripe.log_level = Stripe::LEVEL_INFO
229
+ Util.log_error("foo")
230
+ assert_equal "message=foo level=error \n", $stderr.string
231
+ end
232
+ end
233
+
199
234
  context ".log_info" do
200
235
  should "not log if logging is disabled" do
201
236
  Util.log_info("foo")
@@ -208,6 +243,12 @@ module Stripe
208
243
  assert_equal "message=foo level=info \n", $stdout.string
209
244
  end
210
245
 
246
+ should "not log if level set to error" do
247
+ Stripe.log_level = Stripe::LEVEL_ERROR
248
+ Util.log_debug("foo")
249
+ assert_equal "", $stdout.string
250
+ end
251
+
211
252
  should "log if level set to info" do
212
253
  Stripe.log_level = Stripe::LEVEL_INFO
213
254
  Util.log_info("foo")
@@ -216,6 +257,42 @@ module Stripe
216
257
  end
217
258
  end
218
259
 
260
+ context ".log_* with a logger" do
261
+ setup do
262
+ @out = StringIO.new
263
+ logger = ::Logger.new(@out)
264
+
265
+ # Set a really simple formatter to make matching output as easy as
266
+ # possible.
267
+ logger.formatter = proc { |_severity, _datetime, _progname, message|
268
+ message
269
+ }
270
+
271
+ Stripe.logger = logger
272
+ end
273
+
274
+ context ".log_debug" do
275
+ should "log to the logger" do
276
+ Util.log_debug("foo")
277
+ assert_equal "message=foo ", @out.string
278
+ end
279
+ end
280
+
281
+ context ".log_error" do
282
+ should "log to the logger" do
283
+ Util.log_error("foo")
284
+ assert_equal "message=foo ", @out.string
285
+ end
286
+ end
287
+
288
+ context ".log_info" do
289
+ should "log to the logger" do
290
+ Util.log_info("foo")
291
+ assert_equal "message=foo ", @out.string
292
+ end
293
+ end
294
+ end
295
+
219
296
  context ".normalize_headers" do
220
297
  should "normalize the format of a header key" do
221
298
  assert_equal({ "Request-Id" => nil },
@@ -255,6 +332,14 @@ module Stripe
255
332
  end
256
333
  end
257
334
 
335
+ context ".level_name" do
336
+ should "convert levels to names" do
337
+ assert_equal "debug", Util.send(:level_name, LEVEL_DEBUG)
338
+ assert_equal "error", Util.send(:level_name, LEVEL_ERROR)
339
+ assert_equal "info", Util.send(:level_name, LEVEL_INFO)
340
+ end
341
+ end
342
+
258
343
  context ".log_internal" do
259
344
  should "log in a terminal friendly way" do
260
345
  out = StringIO.new
@@ -267,7 +352,7 @@ module Stripe
267
352
  end
268
353
 
269
354
  Util.send(:log_internal, "message", { foo: "bar" },
270
- color: :green, level: Stripe::LEVEL_DEBUG, out: out)
355
+ color: :green, level: Stripe::LEVEL_DEBUG, logger: nil, out: out)
271
356
  assert_equal "\e[0;32;49mDEBU\e[0m message \e[0;32;49mfoo\e[0m=bar\n",
272
357
  out.string
273
358
  end
@@ -275,10 +360,26 @@ module Stripe
275
360
  should "log in a data friendly way" do
276
361
  out = StringIO.new
277
362
  Util.send(:log_internal, "message", { foo: "bar" },
278
- color: :green, level: Stripe::LEVEL_DEBUG, out: out)
363
+ color: :green, level: Stripe::LEVEL_DEBUG, logger: nil, out: out)
279
364
  assert_equal "message=message level=debug foo=bar\n",
280
365
  out.string
281
366
  end
367
+
368
+ should "log to a logger if set" do
369
+ out = StringIO.new
370
+ logger = ::Logger.new(out)
371
+
372
+ # Set a really simple formatter to make matching output as easy as
373
+ # possible.
374
+ logger.formatter = proc { |_severity, _datetime, _progname, message|
375
+ message
376
+ }
377
+
378
+ Util.send(:log_internal, "message", { foo: "bar" },
379
+ color: :green, level: Stripe::LEVEL_DEBUG, logger: logger, out: nil)
380
+ assert_equal "message=message foo=bar",
381
+ out.string
382
+ end
282
383
  end
283
384
 
284
385
  context ".wrap_logfmt_value" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stripe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-03 00:00:00.000000000 Z
11
+ date: 2017-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday