Module: Toolx::Core::Operation::ResponseWrapper

Defined in:
lib/toolx/core/operation/response_wrapper.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ResponseArgumentError =
Class.new(::Toolx::Core::Errors::NestedStandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



30
31
32
33
34
# File 'lib/toolx/core/operation/response_wrapper.rb', line 30

def self.prepended(base)
  class << base
    prepend ClassMethods
  end
end

Instance Method Details

#perform(*args, **params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/toolx/core/operation/response_wrapper.rb', line 7

def perform(*args, **params)
  response = if args.present? || params.present?
    super
  else
    super()
  end

  if self.class.constants.include?(:Response) && !response.is_a?(self.class.const_get(:Response))
    begin
      response = self.class.const_get(:Response).new(response.to_h.deep_symbolize_keys)
    rescue Dry::Struct::Error => e
      message = "        Expected \#{self.class.name} keyword args: \#{self.class.const_get(:Response, false)}\n        Got: \#{params.inspect}\n        Message: \#{e.message}\n      MSG\n      raise ResponseArgumentError.new(message, e)\n    end\n  end\n\n  response.is_a?(Hash) ? ::Hashie::Mash.new(response) : response\nend\n"