fbe 0.0.41 → 0.0.43
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 +4 -4
- data/lib/fbe/middleware/quota.rb +11 -4
- data/lib/fbe/octo.rb +10 -5
- data/lib/fbe/overwrite.rb +9 -4
- data/lib/fbe.rb +1 -1
- data/test/fbe/middleware/test_quota.rb +3 -3
- data/test/fbe/test_overwrite.rb +25 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '081da62424351c5ee65fc8a949e74f16326905f3a0748d89ba47d13ee2a5e5df'
|
4
|
+
data.tar.gz: fd71a1f8d8fd721c992a46f16269357c7e784aac172b1909f09e97436190d971
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5752ff7c46fba04d57d6357209827994c45d693d9447a951ba6118dd56d9d2b96774bd141316ae9d17e1552ba98476be7ef1b8edb70538cf43ac6d80887b7e97
|
7
|
+
data.tar.gz: 0a8a030cfd4781ef70841db89a6e4db6114e83083ae111e5d80fc51093795ba1251f400b5cb0cf795db3f15baef105079101d74213d68637801127e15cda912e
|
data/lib/fbe/middleware/quota.rb
CHANGED
@@ -26,13 +26,20 @@ require 'faraday'
|
|
26
26
|
|
27
27
|
# Faraday Middleware that monitors GitHub API rate limits.
|
28
28
|
class Fbe::Middleware::Quota < Faraday::Middleware
|
29
|
-
def initialize(app,
|
29
|
+
def initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5)
|
30
30
|
super(app)
|
31
|
-
@limit = limit
|
32
31
|
@requests = 0
|
33
32
|
@app = app
|
34
|
-
|
33
|
+
raise 'The "loog" cannot be nil' if loog.nil?
|
34
|
+
@loog = loog
|
35
|
+
raise 'The "pause" cannot be nil' if pause.nil?
|
36
|
+
raise 'The "pause" must be a positive integer' unless pause.positive?
|
35
37
|
@pause = pause
|
38
|
+
raise 'The "limit" cannot be nil' if limit.nil?
|
39
|
+
raise 'The "limit" must be a positive integer' unless limit.positive?
|
40
|
+
@limit = limit
|
41
|
+
raise 'The "rate" cannot be nil' if rate.nil?
|
42
|
+
raise 'The "rate" must be a positive integer' unless rate.positive?
|
36
43
|
@rate = rate
|
37
44
|
end
|
38
45
|
|
@@ -40,7 +47,7 @@ class Fbe::Middleware::Quota < Faraday::Middleware
|
|
40
47
|
@requests += 1
|
41
48
|
response = @app.call(env)
|
42
49
|
if out_of_limit?(env)
|
43
|
-
@
|
50
|
+
@loog.info(
|
44
51
|
"Too much GitHub API quota consumed, pausing for #{@pause} seconds"
|
45
52
|
)
|
46
53
|
sleep(@pause)
|
data/lib/fbe/octo.rb
CHANGED
@@ -33,6 +33,15 @@ require_relative '../fbe'
|
|
33
33
|
require_relative 'middleware'
|
34
34
|
require_relative 'middleware/quota'
|
35
35
|
|
36
|
+
# Interface to GitHub API.
|
37
|
+
#
|
38
|
+
# It is supposed to be used instead of Octokit client, because it
|
39
|
+
# is pre-configured and enables additional fearues, such as retrying,
|
40
|
+
# logging, and caching.
|
41
|
+
#
|
42
|
+
# @param [Judges::Options] options The options available globally
|
43
|
+
# @param [Hash] global Hash of global options
|
44
|
+
# @param [Loog] loog Logging facility
|
36
45
|
def Fbe.octo(options: $options, global: $global, loog: $loog)
|
37
46
|
raise 'The $global is not set' if global.nil?
|
38
47
|
global[:octo] ||=
|
@@ -79,11 +88,7 @@ def Fbe.octo(options: $options, global: $global, loog: $loog)
|
|
79
88
|
methods: [:get],
|
80
89
|
backoff_factor: 2
|
81
90
|
)
|
82
|
-
builder.use(
|
83
|
-
Fbe::Middleware::Quota,
|
84
|
-
logger: loog,
|
85
|
-
pause: options.github_api_pause
|
86
|
-
)
|
91
|
+
builder.use(Fbe::Middleware::Quota, loog:, pause: options.github_api_pause || 60)
|
87
92
|
builder.use(Faraday::HttpCache, serializer: Marshal, shared_cache: false, logger: Loog::NULL)
|
88
93
|
builder.use(Octokit::Response::RaiseError)
|
89
94
|
builder.use(Faraday::Response::Logger, Loog::NULL)
|
data/lib/fbe/overwrite.rb
CHANGED
@@ -31,20 +31,25 @@ require_relative 'fb'
|
|
31
31
|
# exist, it will be removed (the entire fact will be destroyed, new fact
|
32
32
|
# created, and property set).
|
33
33
|
#
|
34
|
+
# It is important that the fact has +_id+ property. If it doesn't, an exception
|
35
|
+
# will be raised.
|
36
|
+
#
|
34
37
|
# @param [Factbase::Fact] fact The fact to modify
|
35
38
|
# @param [String] property The name of the property to set
|
36
39
|
# @param [Any] vqlue The value to set
|
37
40
|
def Fbe.overwrite(fact, property, value, fb: Fbe.fb)
|
41
|
+
raise 'The fact is nil' if fact.nil?
|
38
42
|
before = {}
|
39
43
|
fact.all_properties.each do |prop|
|
40
44
|
before[prop.to_s] = fact[prop]
|
41
45
|
end
|
42
|
-
|
46
|
+
id = fact['_id']&.first
|
47
|
+
raise 'There is no _id in the fact, cannot use Fbe.overwrite' if id.nil?
|
48
|
+
raise "No facts by _id = #{id}" if fb.query("(eq _id #{id})").delete!.zero?
|
43
49
|
n = fb.insert
|
44
|
-
before[property.to_s] = value
|
50
|
+
before[property.to_s] = [value]
|
45
51
|
before.each do |k, vv|
|
46
|
-
next
|
47
|
-
vv = [vv] unless vv.is_a?(Array)
|
52
|
+
next unless n[k].nil?
|
48
53
|
vv.each do |v|
|
49
54
|
n.send("#{k}=", v)
|
50
55
|
end
|
data/lib/fbe.rb
CHANGED
@@ -45,9 +45,9 @@ class QuotaTest < Minitest::Test
|
|
45
45
|
|
46
46
|
def test_quota_middleware_pauses_when_quota_low
|
47
47
|
loog = Loog::NULL
|
48
|
-
pause =
|
48
|
+
pause = 1
|
49
49
|
app = FakeApp.new
|
50
|
-
middleware = Fbe::Middleware::Quota.new(app,
|
50
|
+
middleware = Fbe::Middleware::Quota.new(app, loog:, pause:)
|
51
51
|
start_time = Time.now
|
52
52
|
105.times do
|
53
53
|
env = Judges::Options.new(
|
@@ -66,7 +66,7 @@ class QuotaTest < Minitest::Test
|
|
66
66
|
log_output = StringIO.new
|
67
67
|
loog = Logger.new(log_output)
|
68
68
|
app = FakeApp.new
|
69
|
-
middleware = Fbe::Middleware::Quota.new(app,
|
69
|
+
middleware = Fbe::Middleware::Quota.new(app, loog:, pause:)
|
70
70
|
105.times do
|
71
71
|
env = Judges::Options.new(
|
72
72
|
'method' => :get,
|
data/test/fbe/test_overwrite.rb
CHANGED
@@ -35,20 +35,43 @@ class TestOverwrite < Minitest::Test
|
|
35
35
|
def test_simple_overwrite
|
36
36
|
fb = Factbase.new
|
37
37
|
f = fb.insert
|
38
|
+
f._id = 1
|
38
39
|
f.foo = 42
|
39
|
-
f.bar = 'hey'
|
40
|
+
f.bar = 'hey you друг'
|
40
41
|
f.many = 3
|
41
42
|
f.many = 3.14
|
42
43
|
Fbe.overwrite(f, :foo, 55, fb:)
|
43
44
|
assert_equal(55, fb.query('(always)').each.to_a.first['foo'].first)
|
44
|
-
assert_equal('hey', fb.query('(always)').each.to_a.first['bar'].first)
|
45
|
+
assert_equal('hey you друг', fb.query('(always)').each.to_a.first['bar'].first)
|
45
46
|
assert_equal(2, fb.query('(always)').each.to_a.first['many'].size)
|
46
47
|
end
|
47
48
|
|
48
49
|
def test_simple_insert
|
49
50
|
fb = Factbase.new
|
50
51
|
f = fb.insert
|
52
|
+
f._id = 1
|
51
53
|
Fbe.overwrite(f, :foo, 42, fb:)
|
52
54
|
assert_equal(42, fb.query('(always)').each.to_a.first['foo'].first)
|
53
55
|
end
|
56
|
+
|
57
|
+
def test_without_id
|
58
|
+
fb = Factbase.new
|
59
|
+
f = fb.insert
|
60
|
+
assert_raises do
|
61
|
+
Fbe.overwrite(f, :foo, 42, fb:)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_safe_insert
|
66
|
+
fb = Factbase.new
|
67
|
+
f1 = fb.insert
|
68
|
+
f1.bar = 'a'
|
69
|
+
f2 = fb.insert
|
70
|
+
f2.bar = 'b'
|
71
|
+
f2._id = 2
|
72
|
+
f3 = fb.insert
|
73
|
+
f3._id = 1
|
74
|
+
Fbe.overwrite(f3, :foo, 42, fb:)
|
75
|
+
assert_equal(3, fb.size)
|
76
|
+
end
|
54
77
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fbe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.43
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-08-
|
11
|
+
date: 2024-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: backtrace
|