fbe 0.23.0 → 0.23.1

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: 7faea911e89cea0abccc6d5f664a1ef5dd1ab8b6853ee0b2b6b97077c604f64d
4
- data.tar.gz: 88ddb605cd24f68b965aa8ff2200c75c6759d175d130fded4f66ebb02540b00d
3
+ metadata.gz: afcbfa40757e9ab45bbae7c083105904ebb9079aa821c256c68969c19e3e4e25
4
+ data.tar.gz: 0416c452b5e217f0fe35ed76676960e751f31229fdca52b5225f7d072c0f5350
5
5
  SHA512:
6
- metadata.gz: 283110a6a7bd12876e0f383f9d243309b0ff0e9495f0987e1e2dc3a84ce5e76e2aa44e86bafe6bcc992f1ec7eae8f36da7206d8f3b18b57320f9339fb7c781d1
7
- data.tar.gz: fd3ce178823ca8e9b830b32143b80babda13d0e5f28549728ecd8d8e7635febc177b8691bd189ef9549d01a2ecf036fc9306697ff31856827ec018268338eb60
6
+ metadata.gz: 3fa6f95c316fabbdfe259ef0b49622dbc8480125e3d3c280ef55878148a4b97646d7857f91c447fd592d46a09a59622347e1157c6193d513b820add777abfd78
7
+ data.tar.gz: 03d2f412f3983ce6e759d40858b2a03d7ba70de2907f6990bcd66d4bc86df77defc65e46e977e9830ba633281c5680a0276a7a8b4a24e59e48c4b33a2efffbe2
@@ -31,7 +31,6 @@ require_relative '../../fbe/middleware'
31
31
  # loog: logger,
32
32
  # maxsize: '50Mb'
33
33
  # )
34
- #
35
34
  # # Use with Faraday
36
35
  # Faraday.new do |builder|
37
36
  # builder.use Faraday::HttpCache, store: store
@@ -31,9 +31,11 @@ class Fbe::Middleware::Trace < Faraday::Middleware
31
31
  #
32
32
  # @param [Object] app The next middleware in the stack
33
33
  # @param [Array] trace The array to store trace entries
34
- def initialize(app, trace)
34
+ # @param [Boolean] all Print ALL requests, even very fast?
35
+ def initialize(app, trace, all: true)
35
36
  super(app)
36
37
  @trace = trace
38
+ @all = all
37
39
  end
38
40
 
39
41
  # Processes the HTTP request and records trace information.
@@ -41,18 +43,20 @@ class Fbe::Middleware::Trace < Faraday::Middleware
41
43
  # @param [Faraday::Env] env The request environment
42
44
  # @return [Faraday::Response] The response from the next middleware
43
45
  def call(env)
44
- started = Time.now
45
46
  entry = {
46
47
  method: env.method,
47
48
  url: env.url.to_s,
48
- started_at: started
49
+ started_at: Time.now
49
50
  }
50
51
  @app.call(env).on_complete do |response_env|
51
52
  finished = Time.now
52
- entry[:status] = response_env.status
53
- entry[:finished_at] = finished
54
- entry[:duration] = finished - started
55
- @trace << entry
53
+ duration = finished - entry[:started_at]
54
+ if duration > 0.01 || @all
55
+ entry[:status] = response_env.status
56
+ entry[:finished_at] = finished
57
+ entry[:duration] = duration
58
+ @trace << entry
59
+ end
56
60
  end
57
61
  end
58
62
  end
data/lib/fbe/octo.rb CHANGED
@@ -104,7 +104,7 @@ def Fbe.octo(options: $options, global: $global, loog: $loog)
104
104
  end
105
105
  builder.use(Octokit::Response::RaiseError)
106
106
  builder.use(Faraday::Response::Logger, loog, formatter: Fbe::Middleware::Formatter)
107
- builder.use(Fbe::Middleware::Trace, trace)
107
+ builder.use(Fbe::Middleware::Trace, trace, all: false)
108
108
  builder.adapter(Faraday.default_adapter)
109
109
  end
110
110
  o.middleware = stack
@@ -132,9 +132,12 @@ def Fbe.octo(options: $options, global: $global, loog: $loog)
132
132
  end
133
133
  message = grouped
134
134
  .sort_by { |_path, entries| -entries.count }
135
- .map { |path, entries| " #{path}: #{entries.count}" }
135
+ .map { |path, entries| " #{path.gsub(%r{^https://api.github.com/}, '/')}: #{entries.count}" }
136
136
  .join("\n")
137
- @loog.info("GitHub API trace (#{grouped.count} URLs vs #{@trace.count} requests):\n#{message}")
137
+ @loog.info(
138
+ "GitHub API trace (#{grouped.count} URLs vs #{@trace.count} requests, " \
139
+ "#{@origin.rate_limit.remaining} quota left):\n#{message}"
140
+ )
138
141
  @trace.clear
139
142
  end
140
143
  end
data/lib/fbe.rb CHANGED
@@ -10,5 +10,5 @@
10
10
  # License:: MIT
11
11
  module Fbe
12
12
  # Current version of the gem (changed by +.rultor.yml+ on every release)
13
- VERSION = '0.23.0' unless const_defined?(:VERSION)
13
+ VERSION = '0.23.1' unless const_defined?(:VERSION)
14
14
  end
@@ -366,30 +366,66 @@ class TestOcto < Fbe::Test
366
366
  stub_request(:get, 'https://api.github.com/rate_limit').to_return(
367
367
  { body: '{}', headers: { 'X-RateLimit-Remaining' => '222' } }
368
368
  )
369
- stub_request(:get, 'https://api.github.com/user/123').to_return(
370
- status: 200,
371
- body: '{"id":123,"login":"test"}',
372
- headers: { 'X-RateLimit-Remaining' => '222' }
373
- )
374
- stub_request(:get, 'https://api.github.com/repos/foo/bar').to_return(
375
- status: 200,
376
- body: '{"id":456,"full_name":"foo/bar"}',
377
- headers: { 'X-RateLimit-Remaining' => '222' }
378
- )
369
+ stub_request(:get, 'https://api.github.com/user/123').to_return do
370
+ sleep(0.02)
371
+ {
372
+ status: 200,
373
+ body: '{"id":123,"login":"test"}',
374
+ headers: { 'X-RateLimit-Remaining' => '222' }
375
+ }
376
+ end
377
+ stub_request(:get, 'https://api.github.com/repos/foo/bar').to_return do
378
+ sleep(0.02)
379
+ {
380
+ status: 200,
381
+ body: '{"id":456,"full_name":"foo/bar"}',
382
+ headers: { 'X-RateLimit-Remaining' => '222' }
383
+ }
384
+ end
379
385
  octo = Fbe.octo(loog:, global: {}, options: Judges::Options.new)
380
386
  octo.user(123)
381
387
  octo.repository('foo/bar')
382
388
  octo.repository('foo/bar')
383
389
  octo.print_trace!
384
390
  output = loog.to_s
385
- assert_includes output, 'GitHub API trace (3 URLs vs 4 requests)'
386
- assert_includes output, 'https://api.github.com/user/123: 1'
387
- assert_includes output, 'https://api.github.com/repos/foo/bar: 2'
388
- repo_index = output.index('https://api.github.com/repos/foo/bar: 2')
389
- user_index = output.index('https://api.github.com/user/123: 1')
391
+ assert_includes output, '2 URLs vs 3 requests'
392
+ assert_includes output, '222 quota left'
393
+ assert_includes output, '/user/123: 1'
394
+ assert_includes output, '/repos/foo/bar: 2'
395
+ repo_index = output.index('/repos/foo/bar: 2')
396
+ user_index = output.index('/user/123: 1')
390
397
  assert_operator repo_index, :<, user_index, 'URLs should be sorted by request count (highest first)'
391
398
  end
392
399
 
400
+ def test_prints_only_real_requests
401
+ WebMock.disable_net_connect!
402
+ stub_request(:get, 'https://api.github.com/rate_limit').to_return(
403
+ { body: '{}', headers: { 'X-RateLimit-Remaining' => '222' } }
404
+ )
405
+ stub = stub_request(:get, 'https://api.github.com/user/123').to_return(
406
+ status: 200,
407
+ body: '{"id":123,"login":"test"}',
408
+ headers: {
409
+ 'X-RateLimit-Remaining' => '222',
410
+ 'Content-Type' => 'application/json',
411
+ 'Cache-Control' => 'public, max-age=60, s-maxage=60',
412
+ 'Etag' => 'W/"2ff9dd4c3153f006830b2b8b721f6a4bb400a1eb81a2e1fa0a3b846ad349b9ec"',
413
+ 'Last-Modified' => 'Wed, 01 May 2025 20:00:00 GMT'
414
+ }
415
+ )
416
+ Dir.mktmpdir do |dir|
417
+ fcache = File.expand_path('test.db', dir)
418
+ octo = Fbe.octo(loog: Loog::NULL, global: {}, options: Judges::Options.new({ 'sqlite_cache' => fcache }))
419
+ octo.user(123)
420
+ loog = Loog::Buffer.new
421
+ octo = Fbe.octo(loog: fake_loog, global: {}, options: Judges::Options.new({ 'sqlite_cache' => fcache }))
422
+ WebMock.remove_request_stub(stub)
423
+ octo.user(123)
424
+ octo.print_trace!
425
+ assert_empty(loog.to_s)
426
+ end
427
+ end
428
+
393
429
  def test_trace_gets_cleared_after_print
394
430
  WebMock.disable_net_connect!
395
431
  stub_request(:get, 'https://api.github.com/rate_limit').to_return(
@@ -414,9 +450,8 @@ class TestOcto < Fbe::Test
414
450
  { body: '{}', headers: { 'X-RateLimit-Remaining' => '222' } }
415
451
  )
416
452
  Dir.mktmpdir do |dir|
417
- global = {}
418
453
  sqlite_cache = File.expand_path('test.db', dir)
419
- o = Fbe.octo(loog: Loog::NULL, global:, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
454
+ o = Fbe.octo(loog: Loog::NULL, global: {}, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
420
455
  stub = stub_request(:get, 'https://api.github.com/user/42').to_return(
421
456
  status: 200,
422
457
  body: { login: 'user1' }.to_json,
@@ -429,8 +464,7 @@ class TestOcto < Fbe::Test
429
464
  )
430
465
  assert_equal('user1', o.user_name_by_id(42))
431
466
  WebMock.remove_request_stub(stub)
432
- global = {}
433
- o = Fbe.octo(loog: Loog::NULL, global:, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
467
+ o = Fbe.octo(loog: Loog::NULL, global: {}, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
434
468
  assert_equal('user1', o.user_name_by_id(42))
435
469
  end
436
470
  end
@@ -441,10 +475,9 @@ class TestOcto < Fbe::Test
441
475
  { body: '{}', headers: { 'X-RateLimit-Remaining' => '222' } }
442
476
  )
443
477
  Dir.mktmpdir do |dir|
444
- global = {}
445
478
  file = File.expand_path('test.db', dir)
446
479
  stub_request(:get, 'https://api.github.com/user/4242').to_return(status: 401)
447
- o = Fbe.octo(loog: Loog::NULL, global:, options: Judges::Options.new({ 'sqlite_cache' => file }))
480
+ o = Fbe.octo(loog: Loog::NULL, global: {}, options: Judges::Options.new({ 'sqlite_cache' => file }))
448
481
  assert_raises(StandardError) do
449
482
  assert_equal('user1', o.user_name_by_id(4242))
450
483
  end
@@ -458,7 +491,6 @@ class TestOcto < Fbe::Test
458
491
  { body: '{}', headers: { 'X-RateLimit-Remaining' => '222' } }
459
492
  )
460
493
  Dir.mktmpdir do |dir|
461
- global = {}
462
494
  stub =
463
495
  stub_request(:get, 'https://api.github.com/user/42')
464
496
  .to_return(
@@ -473,7 +505,7 @@ class TestOcto < Fbe::Test
473
505
  )
474
506
  sqlite_cache = File.expand_path('test.db', dir)
475
507
  Fbe.stub_const(:VERSION, '0.0.1') do
476
- o = Fbe.octo(loog: Loog::NULL, global:, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
508
+ o = Fbe.octo(loog: Loog::NULL, global: {}, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
477
509
  assert_equal('user1', o.user_name_by_id(42))
478
510
  end
479
511
  WebMock.remove_request_stub(stub)
@@ -488,9 +520,8 @@ class TestOcto < Fbe::Test
488
520
  'Last-Modified' => 'Wed, 01 May 2025 20:00:00 GMT'
489
521
  }
490
522
  )
491
- global = {}
492
523
  Fbe.stub_const(:VERSION, '0.0.2') do
493
- o = Fbe.octo(loog: Loog::NULL, global:, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
524
+ o = Fbe.octo(loog: Loog::NULL, global: {}, options: Judges::Options.new({ 'sqlite_cache' => sqlite_cache }))
494
525
  assert_equal('user2', o.user_name_by_id(42))
495
526
  end
496
527
  end
data/test/test__helper.rb CHANGED
@@ -40,6 +40,6 @@ require_relative '../lib/fbe'
40
40
  # Parent class for all tests.
41
41
  class Fbe::Test < Minitest::Test
42
42
  def fake_loog
43
- Loog::VERBOSE
43
+ Loog::NULL
44
44
  end
45
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fbe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.0
4
+ version: 0.23.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko