class Heavylog::LogSubscriber

Public Instance Methods

process_action(event) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 11
def process_action(event)
  data = extract_request(event)
  RequestStore.store[:heavylog_request_data] = data
end
redirect_to(event) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 16
def redirect_to(event)
  RequestStore.store[:heavylog_location] = event.payload[:location]
end
unpermitted_parameters(event) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 20
def unpermitted_parameters(event)
  RequestStore.store[:heavylog_unpermitted_params] ||= []
  RequestStore.store[:heavylog_unpermitted_params].concat(event.payload[:keys])
end

Private Instance Methods

custom_options(event) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 78
def custom_options(event)
  event.payload[:custom_payload] || {}
end
extract_format(payload) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 58
def extract_format(payload)
  payload[:format]
end
extract_location() click to toggle source
# File lib/heavylog/log_subscriber.rb, line 90
def extract_location
  location = RequestStore.store[:heavylog_location]
  return {} unless location

  RequestStore.store[:heavylog_location] = nil
  { location: strip_query_string(location) }
end
extract_path(payload) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 48
def extract_path(payload)
  path = payload[:path]
  strip_query_string(path)
end
extract_request(event) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 27
def extract_request(event)
  payload = event.payload
  data = initial_data(payload)
  data.merge!(extract_status(payload))
  data.merge!(extract_runtimes(event, payload))
  data.merge!(extract_location)
  data.merge!(extract_unpermitted_params)
  data.merge!(extract_request_data(payload))
  data.merge!(custom_options(event))
end
extract_request_data(payload) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 106
def extract_request_data(payload)
  if payload[:request].is_a?(ActionDispatch::Request)
    data = {}

    data[:referrer] = payload[:request].referrer if payload[:request].referrer
    data[:host] = payload[:request].host
    data[:user_agent] = payload[:request].user_agent

    return data
  end

  {}
end
extract_runtimes(event, payload) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 82
def extract_runtimes(event, payload)
  data = { duration: event.duration.to_f.round(2) }
  %i[view_runtime db_runtime].each do |key|
    data[key] = payload[key].to_f.round(2) if payload.key?(key)
  end
  data
end
extract_status(payload) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 62
def extract_status(payload)
  if (status = payload[:status])
    { status: status.to_i }
  elsif (error = payload[:exception])
    exception, message = error
    { status: get_error_status_code(exception), error: "#{exception}: #{message}" }
  else
    { status: 0 }
  end
end
extract_unpermitted_params() click to toggle source
# File lib/heavylog/log_subscriber.rb, line 98
def extract_unpermitted_params
  unpermitted_params = RequestStore.store[:heavylog_unpermitted_params]
  return {} unless unpermitted_params

  RequestStore.store[:heavylog_unpermitted_params] = nil
  { unpermitted_params: unpermitted_params }
end
get_error_status_code(exception) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 73
def get_error_status_code(exception)
  status = ActionDispatch::ExceptionWrapper.rescue_responses[exception]
  Rack::Utils.status_code(status)
end
initial_data(payload) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 38
def initial_data(payload)
  {
    method:     payload[:method],
    path:       extract_path(payload),
    format:     extract_format(payload),
    controller: payload[:controller],
    action:     payload[:action],
  }
end
strip_query_string(path) click to toggle source
# File lib/heavylog/log_subscriber.rb, line 53
def strip_query_string(path)
  index = path.index("?")
  index ? path[0, index] : path
end