Class: HttpStore::Middleware::RequestLog

Inherits:
Object
  • Object
show all
Includes:
Helpers::Storable
Defined in:
lib/http_store/middleware/request_log.rb

Constant Summary collapse

STRING_LIMIT_SIZE =
30_000

Constants included from Helpers::Storable

Helpers::Storable::TEXT_LIMIT_SIZE

Instance Method Summary collapse

Methods included from Helpers::Storable

#gen_storable_meta, #json_safe_parse, #load_storeable_record, #storable, #storable_string, #store_class, #store_request, #storeable_record, #use_cache?

Constructor Details

#initialize(app) ⇒ RequestLog

Returns a new instance of RequestLog.



8
9
10
# File 'lib/http_store/middleware/request_log.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#build_meta(env, status, headers, body) ⇒ Object



20
21
22
23
24
25
# File 'lib/http_store/middleware/request_log.rb', line 20

def build_meta(env, status, headers, body)
  request = ActionDispatch::Request.new(env)

  @meta = Hashie::Mash.new(format_req(request))
  @meta.merge!(format_rsp(status, headers, body))
end

#call(env) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/http_store/middleware/request_log.rb', line 12

def call(env)
  status, headers, body = @app.call(env)
  [status, headers, body]
ensure
  build_meta(env, status, headers, body)
  store_request
end

#format_req(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/http_store/middleware/request_log.rb', line 27

def format_req(request)
  {
    url:          request.url,
    path:         request.path,
    http_method:  request.request_method,
    data:         request.params,
    client_type:  request.params[:client_type],
    is_system:    true,
    headers:      request.headers.select { |k, _v| k.start_with? 'HTTP_' }.to_h,
    query_params: request.query_parameters,
    force:        true,
  }
end

#format_rsp(status, headers, body) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/http_store/middleware/request_log.rb', line 41

def format_rsp(status, headers, body)
  {
    status_code:      status,
    response_headers: headers,
    response:         body.try(:body) || body
  }
end