Sha256: 80fb6c4b2e7b59b7069accf84de685be5613422caee971356689c1e9bdf11a65

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module CassetteRack
  class Response
    attr_reader :status, :headers, :body, :content

    def initialize(res)
      @response = res
      @status = res.status
      @headers = res.headers
      @body = res.body
      parse_content
    end

    def status_code
      status
    end

    def response_headers
      headers
    end

    def success?
      @response.success?
    end

    def permit(*keys)
      case content
      when Hash
        content.select { |key| keys.include? key }
      when Array
        content.map { |item| item.select { |key| keys.include? key } }
      end
    end

    private
      def parse_content
        case body
        when nil, '', ' '
          @content = nil
        when 'true'
          @content = true
        when 'false'
          @content = false
        else
          if @response.env.request_headers['accept'] == 'application/json'
            @content = JSON.parse(body)
          else
            @content = body
          end
        end
      rescue
        @content = body
      end
    # end private
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cassette-rack-0.10.0 lib/cassette-rack/response.rb
cassette-rack-0.9.0 lib/cassette-rack/response.rb
cassette-rack-0.8.1 lib/cassette-rack/response.rb
cassette-rack-0.8.0 lib/cassette-rack/response.rb