Sha256: a99470881f873eaead1677074be365c7f25a6b4b97d0a84d8bf9d5ff788c1f0e

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

#require 'json'

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

    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
      end
    # end private
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cassette-rack-0.5.0 lib/cassette-rack/response.rb