Sha256: 47d25ba6e52c02e9cab5c3b32d53edaa2f64497ae225f182e993a97a793b2163

Contents?: true

Size: 873 Bytes

Versions: 1

Compression:

Stored size: 873 Bytes

Contents

# -*- encoding : utf-8 -*-
module Pacto
  class PactoResponse
    # FIXME: Need case insensitive header lookup, but case-sensitive storage
    attr_accessor :headers, :body, :status, :parsed_body
    attr_reader :parsed_body

    def initialize(data)
      mash = Hashie::Mash.new data
      @headers = mash.headers.nil? ? {} : mash.headers
      @body = mash.body
      @status  = mash.status.to_i
    end

    def to_hash
      {
        status: status,
        headers: headers,
        body: body
      }
    end

    def to_s
      string = "STATUS: #{status}"
      string << " with body (#{body.bytesize} bytes)" if body
      string
    end

    def parsed_body
      if body.is_a?(String) && content_type == 'application/json'
        JSON.parse(body)
      else
        body
      end
    end

    def content_type
      headers['Content-Type']
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pacto-0.4.0.rc1 lib/pacto/core/pacto_response.rb