Module: Kapellmeister::RequestsExtension

Defined in:
lib/kapellmeister/requests_extension.rb

Class Method Summary collapse

Class Method Details

.request_processing(klass, name, request_data) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kapellmeister/requests_extension.rb', line 2

def self.request_processing(klass, (name, request_data))
  mod = if Object.const_defined?("#{self}::#{klass}InstanceMethods")
          const_get("#{self}::#{klass}InstanceMethods")
        else
          const_set(:"#{klass}InstanceMethods", Module.new)
        end

  mod.module_eval do
    define_method name do |data = {}|
      data.deep_compact!

      proc { |method:, path: nil, body: {}, query_params: {}, mock: ''|
        if (Rails.try(:env) || ENV.fetch('APP_ENV', nil)) == 'test'
          return ::Kapellmeister::Base.routes_scheme_parse(mock)
        end

        data, query_keys = *parsed_query(query_params, data)
        valid_query?(data, query_keys)
        valid_body?(data, body)

        full_path = generate_full_path(path, data)

        connection_by(method, full_path, data)
      }.call(**request_data)
    end
  end

  mod
rescue NoMethodError
  raise "You need to define #{self} class with connection_by method"
end