Class: UnivapayClientSdk::IdempotencyCallback

Inherits:
HttpCallBack
  • Object
show all
Defined in:
lib/univapay_client_sdk/extensions.rb

Overview

Injects an Idempotency-Key header into mutating requests if absent.

Instance Method Summary collapse

Constructor Details

#initialize(user_callback = nil) ⇒ IdempotencyCallback

Returns a new instance of IdempotencyCallback.



16
17
18
19
# File 'lib/univapay_client_sdk/extensions.rb', line 16

def initialize(user_callback = nil)
  @user_callback = user_callback
  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/univapay_client_sdk/extensions.rb', line 40

def method_missing(method_name, *args, &block)
  if @user_callback && @user_callback.respond_to?(method_name)
    @user_callback.send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#on_after_response(response) ⇒ Object



36
37
38
# File 'lib/univapay_client_sdk/extensions.rb', line 36

def on_after_response(response)
  @user_callback&.on_after_response(response)
end

#on_before_request(request) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/univapay_client_sdk/extensions.rb', line 21

def on_before_request(request)
  if request
    method = request.http_method.to_s.upcase
    if %w[POST PUT PATCH].include?(method)
      headers = request.headers || {}
      has_idempotency = headers.any? { |k, _| k.to_s.downcase == 'idempotency-key' }
      unless has_idempotency
        require 'securerandom'
        request.headers['Idempotency-Key'] = SecureRandom.uuid
      end
    end
  end
  @user_callback&.on_before_request(request)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/univapay_client_sdk/extensions.rb', line 48

def respond_to_missing?(method_name, include_private = false)
  (@user_callback && @user_callback.respond_to?(method_name, include_private)) || super
end