Module: Roda::RodaPlugins::MethodRoute::RequestMethods

Includes:
SemanticLogger::Loggable
Defined in:
lib/zleb/plugins/method_route.rb

Instance Method Summary collapse

Instance Method Details

#method_to(mod) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zleb/plugins/method_route.rb', line 22

def method_to(mod)
  r_path = remaining_path
  method_name = nil
  result = nil
  if r_path != ""
    segments = r_path.split("/")
    segments.shift
    first_seg = segments.shift
    first_seg[0] = first_seg[0].upcase
    begin
      klass = mod.const_get(first_seg, false)
    rescue NameError => e
      return
    end
    segments.unshift(request_method.downcase)
    method_name = segments.join("_").to_sym

    start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

    begin
      processer = klass.new({request: self, response: response, headers: headers, params: params, scope: scope})
      if processer.respond_to?(:init)
        processer.init
      end
      if processer.respond_to?(method_name)
        result = processer.send(method_name)
      else
        return
      end
      block_result(result)
    rescue => e
      duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(2)
      logger.error("API #{request_method} #{r_path}",
        controller: klass.name,
        action: method_name,
        params: filter_sensitive_params(params),
        duration_ms: duration_ms,
        exception: e
      )
      raise
    end

    duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(2)
    status = response.status || 200

    logger.info("API #{request_method} #{r_path}",
      controller: klass.name,
      action: method_name,
      params: filter_sensitive_params(params),
      status: status,
      duration_ms: duration_ms
    )

    halt
  end
end