Class: Hobix::WebApp::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/hobix/webapp/message.rb

Direct Known Subclasses

Request, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header = {}, body = '') ⇒ Message

Returns a new instance of Message.

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hobix/webapp/message.rb', line 79

def initialize(header={}, body='')
  @header_object = Header.new
  case header
  when Hash
    header.each_pair {|k, v|
      raise ArgumentError, "unexpected header field name: #{k.inspect}" unless k.respond_to? :to_str
      raise ArgumentError, "unexpected header field body: #{v.inspect}" unless v.respond_to? :to_str
      @header_object.add k.to_str, v.to_str
    }
  when Array
    header.each {|k, v|
      raise ArgumentError, "unexpected header field name: #{k.inspect}" unless k.respond_to? :to_str
      raise ArgumentError, "unexpected header field body: #{v.inspect}" unless v.respond_to? :to_str
      @header_object.add k.to_str, v.to_str
    }
  else
    raise ArgumentError, "unexpected header argument: #{header.inspect}"
  end
  raise ArgumentError, "unexpected body: #{body.inspect}" unless body.respond_to? :to_str
  @body_object = StringIO.new(body.to_str)
end

Instance Attribute Details

#body_objectObject (readonly)

Returns the value of attribute body_object.



100
101
102
# File 'lib/hobix/webapp/message.rb', line 100

def body_object
  @body_object
end

#header_objectObject (readonly)

Returns the value of attribute header_object.



100
101
102
# File 'lib/hobix/webapp/message.rb', line 100

def header_object
  @header_object
end

Instance Method Details

#freezeObject



102
103
104
105
106
# File 'lib/hobix/webapp/message.rb', line 102

def freeze
  @header_object.freeze
  @body_object.string.freeze
  super
end

#output_body(out) ⇒ Object



114
115
116
# File 'lib/hobix/webapp/message.rb', line 114

def output_body(out)
  out << @body_object.string
end

#output_header(out) ⇒ Object



108
109
110
111
112
# File 'lib/hobix/webapp/message.rb', line 108

def output_header(out)
  @header_object.each {|k, v|
    out << "#{k}: #{v}\n"
  }
end

#output_message(out) ⇒ Object



118
119
120
121
122
# File 'lib/hobix/webapp/message.rb', line 118

def output_message(out)
  output_header(out)
  out << "\n"
  output_body(out)
end