Module: SimpleRenderer

Included in:
RailsHush::HushOne, RailsHush::HushTwo
Defined in:
lib/rails_hush/middleware/simple_renderer.rb

Instance Method Summary collapse

Instance Method Details

#default_renderer(status, content_type, error) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
# 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