ac 1.0.0 → 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/ac_object.rb +16 -11
- data/lib/ac/base.rb +42 -31
- 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/ac_object.rb
CHANGED
@@ -4,11 +4,23 @@ module Ac
|
|
4
4
|
|
5
5
|
def self.from_response(response)
|
6
6
|
parsed_json = JSON.parse(response.body) rescue {}
|
7
|
-
new(parsed_json
|
7
|
+
ac_object = new(parsed_json)
|
8
|
+
ac_object.instance_variable_set("@response", response)
|
9
|
+
ac_object
|
8
10
|
end
|
9
11
|
|
10
|
-
def
|
11
|
-
|
12
|
+
def self.new value
|
13
|
+
case value
|
14
|
+
when Hash
|
15
|
+
super(value)
|
16
|
+
when Array
|
17
|
+
value.map { new(_1) }
|
18
|
+
else
|
19
|
+
value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(parsed_json)
|
12
24
|
@values = parsed_json.transform_keys(&:to_s)
|
13
25
|
@values.keys.each do |key|
|
14
26
|
define_singleton_method(key) { self.[](key) } unless respond_to? key
|
@@ -53,14 +65,7 @@ module Ac
|
|
53
65
|
private
|
54
66
|
|
55
67
|
def wrap(value)
|
56
|
-
|
57
|
-
when Hash
|
58
|
-
AcObject.new(value)
|
59
|
-
when Array
|
60
|
-
value.map { wrap(_1) }
|
61
|
-
else
|
62
|
-
value
|
63
|
-
end
|
68
|
+
AcObject.new value
|
64
69
|
end
|
65
70
|
end
|
66
71
|
end
|
data/lib/ac/base.rb
CHANGED
@@ -9,10 +9,15 @@ module Ac
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def access_token
|
12
|
-
refresh_token if
|
12
|
+
refresh_token if should_refresh?
|
13
13
|
@access_token
|
14
14
|
end
|
15
15
|
|
16
|
+
def should_refresh?
|
17
|
+
return false unless respond_to?(:refresh_token)
|
18
|
+
@access_token.blank? || @token_expires_at&.past?
|
19
|
+
end
|
20
|
+
|
16
21
|
def url path
|
17
22
|
if path.start_with?("http://") || path.start_with?("https://")
|
18
23
|
path
|
@@ -21,55 +26,61 @@ module Ac
|
|
21
26
|
end
|
22
27
|
end
|
23
28
|
|
24
|
-
|
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
|
25
39
|
if access_token
|
26
40
|
options[:headers] ||= {}
|
27
41
|
options[:headers]["Authorization"] = "Bearer #{access_token}"
|
28
42
|
end
|
29
|
-
|
43
|
+
end
|
30
44
|
|
31
45
|
%i[get post put patch delete].each do |http_verb|
|
32
46
|
define_method :"#{http_verb}_request" do |path, options = {}|
|
47
|
+
options.symbolize_keys!
|
33
48
|
options[:method] = http_verb
|
49
|
+
options = default_options.deep_merge(options) if default_options
|
34
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
|
35
54
|
Typhoeus::Request.new url(path), options
|
36
55
|
end
|
37
56
|
|
38
57
|
define_method http_verb do |path, options = {}, &block|
|
39
|
-
|
58
|
+
options.symbolize_keys!
|
59
|
+
request = send(:"#{http_verb}_request", path, options)
|
40
60
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
else
|
51
|
-
raise BlockValidationError.new(AcObject.from_response(response))
|
52
|
-
end
|
53
|
-
end
|
54
|
-
else
|
55
|
-
unless response.success?
|
56
|
-
if can_retry?(response) && retries_count < self.class::MAX_RETRIES
|
57
|
-
sleep(2**retries_count)
|
58
|
-
retries_count += 1
|
59
|
-
redo
|
60
|
-
else
|
61
|
-
raise AcError.from_response(AcObject.from_response(response))
|
62
|
-
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)
|
63
70
|
end
|
64
71
|
end
|
65
|
-
|
72
|
+
|
73
|
+
raise AcError.from_response(ac_object)
|
74
|
+
|
66
75
|
end
|
67
76
|
end
|
68
77
|
|
69
|
-
def
|
70
|
-
block
|
71
|
-
|
72
|
-
|
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
|
73
84
|
end
|
74
85
|
|
75
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.
|
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
|