class Restspec::Endpoints::Response

A response is a representation of the triad returned by the API calls. They represent the status code, the headers and the response's body.

Attributes

code[RW]
endpoint[RW]
headers[RW]
raw_body[RW]

Public Class Methods

new(code, headers, raw_body) click to toggle source
# File lib/restspec/endpoints/response.rb, line 11
def initialize(code, headers, raw_body)
  self.headers = headers
  self.code = code
  self.raw_body = raw_body
end

Public Instance Methods

body() click to toggle source
# File lib/restspec/endpoints/response.rb, line 51
def body
  @body ||= read_body
end
parsed_body() click to toggle source
# File lib/restspec/endpoints/response.rb, line 43
def parsed_body
  @parsed_body ||= begin
    JSON.parse(raw_body)
  rescue JSON::ParserError => e
    raw_body
  end
end
read_body(value = parsed_body) click to toggle source
# File lib/restspec/endpoints/response.rb, line 26
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
to_s() click to toggle source
# File lib/restspec/endpoints/response.rb, line 17
def to_s
  if endpoint.present?
    url = endpoint.executed_url || endpoint.full_path
    "[#{endpoint.method.upcase} to #{url}]"
  else
    raw_body
  end
end