File: response.rb

package info (click to toggle)
ruby-grape-logging 1.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 244 kB
  • sloc: ruby: 845; makefile: 6; sh: 3
file content (29 lines) | stat: -rw-r--r-- 831 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module GrapeLogging
  module Loggers
    class Response < GrapeLogging::Loggers::Base
      def parameters(_, response)
        response ? { response: serialized_response_body(response) } : {}
      end

      private

      # In some cases, response.body is not parseable by JSON.
      # For example, if you POST on a PUT endpoint, response.body is egal to """".
      # It's strange but it's the Grape behavior...
      def serialized_response_body(response)

        if response.respond_to?(:body)
          # Rack responses
          begin
            response.body.map{ |body| JSON.parse(body.to_s) }
          rescue # No reason to have "=> e" here when we don't use it..
            response.body
          end
        else
          # Error & Exception responses
          response
        end
      end
    end
  end
end