Class: MLserver::Response

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

Constant Summary collapse

@@default_headers =
{
  Server: "MLserver #{MLserver.version}",
  "Content-Type": "text/plain"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: 200, headers: {}, data: "", content_type: nil, httpver: "HTTP/1.1") ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
# File 'lib/MLserver/response.rb', line 8

def initialize(status:200, headers:{}, data:"", content_type: nil, httpver: "HTTP/1.1")
  @status = status
  @data = data
  @httpver = httpver
  @headers = @@default_headers.merge(response_specific_headers).merge(headers)
  @headers[:"Content-Type"] = content_type if content_type
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



30
31
32
# File 'lib/MLserver/response.rb', line 30

def data
  @data
end

#headersObject

Returns the value of attribute headers.



30
31
32
# File 'lib/MLserver/response.rb', line 30

def headers
  @headers
end

#httpverObject

Returns the value of attribute httpver.



30
31
32
# File 'lib/MLserver/response.rb', line 30

def httpver
  @httpver
end

#statusObject

Returns the value of attribute status.



30
31
32
# File 'lib/MLserver/response.rb', line 30

def status
  @status
end

Instance Method Details

#to_s(array: false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/MLserver/response.rb', line 16

def to_s(array:false)
  status_line = "#{httpver} #{status}"
  headers = @headers.map { |header|
    k=header[0].to_s
    v=header[1].to_s
    header = "#{k}: #{v}"
  }
  headers = headers.join("\r\n") if !array
  data = @data

  return "#{status_line}\r\n#{headers}\r\n\r\n#{data}\r\n" if !array
  return [status_line, headers, "", data, ""] if array
end