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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d91a0bc77820fc802d1e7d4228c334f76dafd306b8d827acb0efb31d5fb22c56
4
- data.tar.gz: 3ffde7cc5ca9c2fb1041a95c0e3d4a4b7aae953d23713bb446a2e2b3ddabcbe4
3
+ metadata.gz: b5a2d1fa03242bf80f9cded8b72e0bdcea929fd4bac989c6c4b151f252befa8b
4
+ data.tar.gz: 934887067b76c1d18bbeb06d1005ddce8b6e84e14ea1bf148760690a377d8060
5
5
  SHA512:
6
- metadata.gz: fe75a02d340f7c3e785eebf6fc4a190910b9fb36b8ebf5a90e4d13fbbde36f33e383b49b9d63d6ea6cd7078d1965fe268aab609b6512cda5c731e67a27332a28
7
- data.tar.gz: b5472513e94d0b463ada98ba4639471baf125e9a34dd234eb7bf045738b5367b5f83ddb7656531d338b097ea557974a5dee0cecbc70af6d19a9f0a26ecfd64ee
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, response: response)
7
+ ac_object = new(parsed_json)
8
+ ac_object.instance_variable_set("@response", response)
9
+ ac_object
8
10
  end
9
11
 
10
- def initialize(parsed_json, response: nil)
11
- @response = response
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
- case value
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 respond_to?(:refresh_token) && (@access_token.blank? || @token_expires_at&.past?)
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
- def authenticate! options
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
- end
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
- retries_count ||= 0
58
+ options.symbolize_keys!
59
+ request = send(:"#{http_verb}_request", path, options)
40
60
 
41
- response = send(:"#{http_verb}_request", path, options).run
42
- Database.save_request response, class_name: self.class.name if self.class::SAVE_RESPONSES
43
-
44
- if block
45
- unless run_block_validation(AcObject.from_response(response), block)
46
- if retries_count < self.class::MAX_RETRIES
47
- sleep(2**retries_count)
48
- retries_count += 1
49
- redo
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
- return AcObject.from_response(response)
72
+
73
+ raise AcError.from_response(ac_object)
74
+
66
75
  end
67
76
  end
68
77
 
69
- def run_block_validation response, block
70
- block.call response
71
- rescue StandardError => e
72
- false
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ac
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
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.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-11 00:00:00.000000000 Z
10
+ date: 2025-03-14 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: typhoeus