Class: HttpResponseFormat

Inherits:
Format
  • Object
show all
Defined in:
lib/httpresponseformat.rb

Overview

The HttpResponseFormat class is the format used to publish messages to HTTP response clients connected to a GRIP proxy.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = nil, reason = nil, headers = nil, body = nil) ⇒ HttpResponseFormat

Initialize with the message code, reason, headers, and body to send to the client when the message is publishing.



21
22
23
24
25
26
# File 'lib/httpresponseformat.rb', line 21

def initialize(code=nil, reason=nil, headers=nil, body=nil)
  @code = code
  @reason = reason
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



17
18
19
# File 'lib/httpresponseformat.rb', line 17

def body
  @body
end

#codeObject

Returns the value of attribute code.



14
15
16
# File 'lib/httpresponseformat.rb', line 14

def code
  @code
end

#headersObject

Returns the value of attribute headers.



16
17
18
# File 'lib/httpresponseformat.rb', line 16

def headers
  @headers
end

#reasonObject

Returns the value of attribute reason.



15
16
17
# File 'lib/httpresponseformat.rb', line 15

def reason
  @reason
end

Instance Method Details

#exportObject

Export the message into the required format and include only the fields that are set. The body is exported as base64 if the text is encoded as binary.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/httpresponseformat.rb', line 36

def export
  out = Hash.new
  if !@code.nil?
    out['code'] = @code
  end
  if !@reason.nil?
    out['reason'] = @reason
  end
  if !@headers.nil? and @headers.length > 0
    out['headers'] = @headers
  end
  if !@body.nil?
    if @body.clone.force_encoding("UTF-8").valid_encoding?   
      out['body'] = @body
    else
      out['body-bin'] = Base64.encode64(@body)
    end
  end
  return out
end

#nameObject

The name used when publishing this format.



29
30
31
# File 'lib/httpresponseformat.rb', line 29

def name
  return 'http-response'
end