ac 1.0.2 → 1.1.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 +4 -4
- data/lib/ac/base.rb +36 -30
- data/lib/ac/version.rb +1 -1
- 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: b5a2d1fa03242bf80f9cded8b72e0bdcea929fd4bac989c6c4b151f252befa8b
|
4
|
+
data.tar.gz: 934887067b76c1d18bbeb06d1005ddce8b6e84e14ea1bf148760690a377d8060
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f04ef8d80ec85f3f7578055f9c557a3dcc9a92617edcc742a680d9ed29291d0658cbf19b4c9d6ab6383680e687345ce105bb34d5d09e9895c7877b7285519d49
|
7
|
+
data.tar.gz: 1d5b65ce852fee47b9e8f75b50d7d526161e4967964d0e024e4e96121f121809b8f02f0c5d62af06a96bb13d97531c18eaaa52171379217c45e4e65dc95b85d4
|
data/lib/ac/base.rb
CHANGED
@@ -26,55 +26,61 @@ module Ac
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
def default_options
|
30
|
+
{
|
31
|
+
headers: {
|
32
|
+
"Content-Type" => "application/json",
|
33
|
+
"x-idempotency-key" => SecureRandom.uuid
|
34
|
+
}
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def authenticate! options
|
30
39
|
if access_token
|
31
40
|
options[:headers] ||= {}
|
32
41
|
options[:headers]["Authorization"] = "Bearer #{access_token}"
|
33
42
|
end
|
34
|
-
|
43
|
+
end
|
35
44
|
|
36
45
|
%i[get post put patch delete].each do |http_verb|
|
37
46
|
define_method :"#{http_verb}_request" do |path, options = {}|
|
47
|
+
options.symbolize_keys!
|
38
48
|
options[:method] = http_verb
|
49
|
+
options = default_options.deep_merge(options) if default_options
|
39
50
|
authenticate!(options) unless options.delete(:skip_authentication)
|
51
|
+
if options[:body]
|
52
|
+
options[:body] = JSON.dump(options[:body]) if options[:body].is_a?(Hash) && options.dig(:headers, "Content-Type") == "application/json"
|
53
|
+
end
|
40
54
|
Typhoeus::Request.new url(path), options
|
41
55
|
end
|
42
56
|
|
43
57
|
define_method http_verb do |path, options = {}, &block|
|
44
|
-
|
45
|
-
|
46
|
-
response = send(:"#{http_verb}_request", path, options).run
|
47
|
-
Database.save_request response, class_name: self.class.name if self.class::SAVE_RESPONSES
|
58
|
+
options.symbolize_keys!
|
59
|
+
request = send(:"#{http_verb}_request", path, options)
|
48
60
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
else
|
60
|
-
unless response.success?
|
61
|
-
if can_retry?(response) && retries_count < self.class::MAX_RETRIES
|
62
|
-
sleep(2**retries_count)
|
63
|
-
retries_count += 1
|
64
|
-
redo
|
65
|
-
else
|
66
|
-
raise AcError.from_response(AcObject.from_response(response))
|
67
|
-
end
|
61
|
+
for retry_number in 0..MAX_RETRIES
|
62
|
+
response = request.run
|
63
|
+
Database.save_request response, class_name: self.class.name if self.class::SAVE_RESPONSES
|
64
|
+
ac_object = AcObject.from_response(response)
|
65
|
+
if validate(ac_object, block)
|
66
|
+
return ac_object
|
67
|
+
else
|
68
|
+
break unless block || can_retry?(ac_object.response)
|
69
|
+
sleep(2**retry_number)
|
68
70
|
end
|
69
71
|
end
|
70
|
-
|
72
|
+
|
73
|
+
raise AcError.from_response(ac_object)
|
74
|
+
|
71
75
|
end
|
72
76
|
end
|
73
77
|
|
74
|
-
def
|
75
|
-
block
|
76
|
-
|
77
|
-
|
78
|
+
def validate ac_object, block=nil
|
79
|
+
if block
|
80
|
+
block.call ac_object rescue false
|
81
|
+
else
|
82
|
+
ac_object.response.success?
|
83
|
+
end
|
78
84
|
end
|
79
85
|
|
80
86
|
def can_retry? response
|
data/lib/ac/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ac
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- felipedmesquita
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-03-
|
10
|
+
date: 2025-03-14 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: typhoeus
|