Class: JSONAPIonify::Api::Server::MockResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapionify/api/server/mock_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, headers, body) ⇒ MockResponse

Returns a new instance of MockResponse.



7
8
9
10
11
# File 'lib/jsonapionify/api/server/mock_response.rb', line 7

def initialize(status, headers, body)
  @status  = status
  @body    = body.is_a?(Rack::BodyProxy) ? body.body : body
  @headers = Rack::Utils::HeaderHash.new headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/jsonapionify/api/server/mock_response.rb', line 5

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/jsonapionify/api/server/mock_response.rb', line 5

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/jsonapionify/api/server/mock_response.rb', line 5

def status
  @status
end

Instance Method Details

#http_stringObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jsonapionify/api/server/mock_response.rb', line 20

def http_string
  # HTTP/1.1 200 OK
  # Date: Fri, 31 Dec 1999 23:59:59 GMT
  # Content-Type: text/html
  # Content-Length: 1354
  #
  # <body>
  [].tap do |lines|
    lines << "HTTP/1.1 #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}"
    headers.each do |k, v|
      lines << "#{k.split('-').map(&:capitalize).join('-')}: #{v}"
    end
    lines << ''
    lines << body if body.present?
  end.join("\n")
end