class Watchdocs::Rails::Middleware

Attributes

app[R]
report[R]

Public Class Methods

new(app) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 9
def initialize(app)
  @app = app
  @report = {}
end

Public Instance Methods

call(env) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 14
def call(env)
  app.call(env).tap do |response|
    begin
      if record_response?(response)
        clear_report
        catch_request(env)
        catch_response(response)
        match_endpoint_pattern
        record_call
      end
    rescue StandardError => e
      $stderr.puts "Watchdocs Middleware Error: #{e.message}"
    end
  end
end

Private Instance Methods

catch_request(env) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 49
def catch_request(env)
  @report[:request] = {
    method: env['REQUEST_METHOD'],
    url: env['PATH_INFO'],
    query_string_params: parse_query_string(env['QUERY_STRING']),
    body: parse_request_body(env['rack.input'].read)
  }
end
catch_response(response) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 58
def catch_response(response)
  status, headers, body = *response
  @report[:response] = {
    status: status,
    body: parse_response_body(body_string(body))
  }
end
clear_report() click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 45
def clear_report
  @report = {}
end
filter_data(data) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 84
def filter_data(data)
  data.is_a?(Enumerable) ? data.filter_data : data
end
from_specs?() click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 80
def from_specs?
  ::Rails.env.test?
end
json_response?(headers) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 41
def json_response?(headers)
  headers['Content-Type'] && headers['Content-Type'].include?('json')
end
match_endpoint_pattern() click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 66
def match_endpoint_pattern
  @report[:endpoint] = begin
    ::Rails.application.routes.router.routes
           .simulator.memos(report[:request][:url])
           .last.path.spec.to_s.sub('(.:format)', '')
  end
end
no_content_response?(status) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 37
def no_content_response?(status)
  status.to_i == 204
end
record_call() click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 74
def record_call
  Rails::Recordings.record(
    report, from_specs: from_specs?
  )
end
record_response?(response) click to toggle source
# File lib/watchdocs/rails/middleware.rb, line 32
def record_response?(response)
  status, headers, _body = *response
  no_content_response?(status) || json_response?(headers)
end