Sha256: 27daa391ed01cef9b9f460e4cf725ab9f58490e8cbe518c36a0bbf591e6b8db6

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require 'delegate'

module Restspec
  module Endpoints
    class Response
      attr_accessor :endpoint, :headers, :code, :raw_body

      def initialize(code, headers, raw_body)
        self.headers = headers
        self.code = code
        self.raw_body = raw_body
      end

      def to_s
        if endpoint.present?
          url = endpoint.executed_url || endpoint.full_path
          "[#{endpoint.method.upcase} to #{url}]"
        else
          raw_body
        end
      end

      def read_body(value = parsed_body)
        case value
        when Array
          value.map { |item| read_body(item) }
        when Hash
          Values::SuperHash.new(value).tap do |super_hash|
            super_hash.find do |key, value|
              if value.class == Array
                super_hash[key] = read_body(value)
              end
            end
          end
        else
          value
        end
      end

      def parsed_body
        @parsed_body ||= begin
          JSON.parse(raw_body)
        rescue JSON::ParserError => e
          raw_body
        end
      end

      def body
        @body ||= read_body
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
restspec-0.0.4 lib/restspec/endpoints/response.rb
restspec-0.0.3 lib/restspec/endpoints/response.rb
restspec-0.0.2 lib/restspec/endpoints/response.rb
restspec-0.0.1 lib/restspec/endpoints/response.rb