Class: Tina4::TestResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/test_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rack_response) ⇒ TestResponse

Build from a Rack response tuple [status, headers, body_array]



21
22
23
24
25
26
27
# File 'lib/tina4/test_client.rb', line 21

def initialize(rack_response)
  @status = rack_response[0]
  @headers = rack_response[1] || {}
  @content_type = @headers["content-type"] || ""
  raw_body = rack_response[2]
  @body = raw_body.is_a?(Array) ? raw_body.join : raw_body.to_s
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



18
19
20
# File 'lib/tina4/test_client.rb', line 18

def body
  @body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



18
19
20
# File 'lib/tina4/test_client.rb', line 18

def content_type
  @content_type
end

#headersObject (readonly)

Returns the value of attribute headers.



18
19
20
# File 'lib/tina4/test_client.rb', line 18

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



18
19
20
# File 'lib/tina4/test_client.rb', line 18

def status
  @status
end

Instance Method Details

#inspectObject



42
43
44
# File 'lib/tina4/test_client.rb', line 42

def inspect
  "<TestResponse status=#{@status} content_type=#{@content_type.inspect}>"
end

#jsonObject

Parse body as JSON.



30
31
32
33
34
35
# File 'lib/tina4/test_client.rb', line 30

def json
  return nil if @body.nil? || @body.empty?
  JSON.parse(@body)
rescue JSON::ParserError
  nil
end

#textObject

Return body as a string.



38
39
40
# File 'lib/tina4/test_client.rb', line 38

def text
  @body.to_s
end