Sha256: 835f17660e3da443761186ab7d8d20ac9d322b307cb83ea43032da08c2c802aa

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

module Esplanade
  class Request
    class Raw
      class Body
        def initialize(raw_request, env)
          @raw_request = raw_request
          @env = env
        end

        def to_string
          return @string if @string

          @string = @env['rack.input'].read
          @env['rack.input'].rewind
          @string
        end

        def to_hash
          @hash ||= JSON.parse(to_string.to_s)
        rescue JSON::ParserError
          raise BodyIsNotJson.new(**message)
        end

        def reduced_version
          @reduced_version ||= if to_string && to_string.size >= 1000
                                 "#{to_string[0..499]}...#{to_string[500..-1]}"
                               else
                                 to_string
                               end
        end

        private

        def message
          {
            method: @raw_request.method,
            path: @raw_request.path,
            raw_path: @raw_request.raw_path,
            content_type: @raw_request.content_type,
            body: reduced_version
          }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
esplanade-1.8.5 lib/esplanade/request/raw/body.rb
esplanade-1.8.4 lib/esplanade/request/raw/body.rb
esplanade-1.8.3 lib/esplanade/request/raw/body.rb
esplanade-1.8.2 lib/esplanade/request/raw/body.rb
esplanade-1.8.1 lib/esplanade/request/raw/body.rb
esplanade-1.8.0 lib/esplanade/request/raw/body.rb