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]



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

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.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def body
  @body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def content_type
  @content_type
end

#headersObject (readonly)

Returns the value of attribute headers.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def status
  @status
end

Instance Method Details

#inspectObject



44
45
46
# File 'lib/tina4/test_client.rb', line 44

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

#jsonObject

Parse body as JSON.



32
33
34
35
36
37
# File 'lib/tina4/test_client.rb', line 32

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

#textObject

Return body as a string.



40
41
42
# File 'lib/tina4/test_client.rb', line 40

def text
  @body.to_s
end