module SimpleRenderer

Public Instance Methods

default_renderer(status, content_type, error) click to toggle source
# File lib/rails_hush/middleware/simple_renderer.rb, line 25
def default_renderer(status, content_type, error)
  body = { status: status, error: error }
  format = "to_#{content_type.to_sym}" if content_type
  if format && body.respond_to?(format)
    body = body.public_send(format)
  else
    content_type = 'application/json'
    body = body.to_json
  end
  [status, { "Content-Type" => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}",
            "Content-Length" => body.bytesize.to_s }, [body]]
end
log_request(status, request) click to toggle source
# File lib/rails_hush/middleware/simple_renderer.rb, line 3
def log_request(status, request)
  payload = {
    params: (request.filtered_parameters rescue {}),
    headers: request.headers,
    format: (request.format.ref rescue :text),
    method: (request.request_method rescue 'INVALID'),
    path: request.fullpath,
    status: status
  }
  ActiveSupport::Notifications.instrument "process_action.action_controller", payload
end
render(status, request, error=nil) click to toggle source
# File lib/rails_hush/middleware/simple_renderer.rb, line 15
def render(status, request, error=nil)
  begin
    content_type = request.formats.first
  rescue Mime::Type::InvalidMimeType
    content_type = Mime[:text]
  end
  error ||= Rack::Utils::HTTP_STATUS_CODES.fetch(status, Rack::Utils::HTTP_STATUS_CODES[500])
  @renderer.call(status, content_type, error)
end