Sha256: e3e061579b4c165ae011939e692f775fd94371cb1b33b768352ca7f527c7f124

Contents?: true

Size: 863 Bytes

Versions: 1

Compression:

Stored size: 863 Bytes

Contents

require 'multi_json'

module Esplanade
  class Response
    class Raw
      class Body
        def initialize(request, raw_response, raw_body)
          @request = request
          @raw_response = raw_response
          @raw_body = raw_body
        end

        def to_string
          @to_string ||= @raw_body.body rescue nil
          @to_string ||= @raw_body.first rescue nil
        end

        def to_hash
          @hash ||= MultiJson.load(to_string)
        rescue MultiJson::ParseError
          raise BodyIsNotJson.new(**message)
        end

        private

        def message
          {
            request: {
              method: @request.raw.method,
              path: @request.raw.path
            },
            status: @raw_response.status,
            body: @raw_response.body.to_string
          }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
esplanade-1.5.0 lib/esplanade/response/raw/body.rb