Class: HttpStreamFormat

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil, close = false) ⇒ HttpStreamFormat

Initialize with either the message content or a boolean indicating that the streaming connection should be closed. If neither the content nor the boolean flag is set then an error will be raised.



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

def initialize(content=nil, close=false)
  @content = content
  @close = close
  if !@close and @content.nil?
    raise 'Content not set'
  end
end

Instance Attribute Details

#closeObject

Returns the value of attribute close.



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

def close
  @close
end

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

Instance Method Details

#exportObject

Exports the message in the required format depending on whether the message content is binary or not, or whether the connection should be closed.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/httpstreamformat.rb', line 36

def export
  out = Hash.new
  if @close
    out['action'] = 'close'
  else
    if @content.clone.force_encoding("UTF-8").valid_encoding?
      out['content'] = @content
    else
      out['content-bin'] = Base64.encode64(@content)
    end
  end
  return out
end

#nameObject

The name used when publishing this format.



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

def name
  return 'http-stream'
end