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.



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

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



60
61
62
63
64
65
66
# File 'lib/univapay_client_sdk/extensions.rb', line 60

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



56
57
58
# File 'lib/univapay_client_sdk/extensions.rb', line 56

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

#on_before_request(request) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/univapay_client_sdk/extensions.rb', line 41

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)


68
69
70
# File 'lib/univapay_client_sdk/extensions.rb', line 68

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