Class: Restspec::Endpoints::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/restspec/endpoints/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, headers, raw_body) ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
# File 'lib/restspec/endpoints/response.rb', line 8

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

Instance Attribute Details

#codeObject

Returns the value of attribute code.



6
7
8
# File 'lib/restspec/endpoints/response.rb', line 6

def code
  @code
end

#endpointObject

Returns the value of attribute endpoint.



6
7
8
# File 'lib/restspec/endpoints/response.rb', line 6

def endpoint
  @endpoint
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/restspec/endpoints/response.rb', line 6

def headers
  @headers
end

#raw_bodyObject

Returns the value of attribute raw_body.



6
7
8
# File 'lib/restspec/endpoints/response.rb', line 6

def raw_body
  @raw_body
end

Instance Method Details

#bodyObject



48
49
50
# File 'lib/restspec/endpoints/response.rb', line 48

def body
  @body ||= read_body
end

#parsed_bodyObject



40
41
42
43
44
45
46
# File 'lib/restspec/endpoints/response.rb', line 40

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

#read_body(value = parsed_body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restspec/endpoints/response.rb', line 23

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_sObject



14
15
16
17
18
19
20
21
# File 'lib/restspec/endpoints/response.rb', line 14

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