Module: Stomper::Support::Ruby1_9::FrameSerializer
- Defined in:
- lib/stomper/support/1.9/frame_serializer.rb
Overview
Implementation of FrameSerializer methods for Ruby 1.9
Instance Method Summary collapse
-
#determine_content_length(frame) ⇒ Fixnum
Determines the content-length of a frame being sent to a broker.
-
#determine_content_type(frame) ⇒ String
Determines the content-type of a frame being sent to a broker.
-
#encode_body(body, ct_header) ⇒ String
Return the body with the specified encoding applied.
-
#get_body_byte ⇒ String?
Reads a single byte from the body portion of a frame.
-
#get_header_line ⇒ String
Reads a line of text from the underlying IO.
Instance Method Details
#determine_content_length(frame) ⇒ Fixnum
Determines the content-length of a frame being sent to a broker. For Ruby 1.9, this is the bytesize
of the frame’s body.
52 53 54 |
# File 'lib/stomper/support/1.9/frame_serializer.rb', line 52 def determine_content_length(frame) frame.body.bytesize end |
#determine_content_type(frame) ⇒ String
Determines the content-type of a frame being sent to a broker. This version of the method is used by Ruby 1.9, and will use the encoding of the frame’s body to help determine the appropriate charset. If the body’s encoding is binary, no charset parameter will be included (even if one was manually set.) Otherwise, if the content-type is ommitted, a value of ‘text/plain’ is assumed and the encoding of the body is used as the charset parameter.
40 41 42 43 44 45 46 47 |
# File 'lib/stomper/support/1.9/frame_serializer.rb', line 40 def determine_content_type(frame) ct = frame[:'content-type'] ct &&= ct.gsub(/\;\s*charset=\"?[a-zA-Z0-9!\#$&.+\-^_]+\"?/i, '') enc = frame.body.encoding.name text = (enc != 'ASCII-8BIT') || ct =~ /^text\// ct = 'text/plain' if ct.nil? && text ct && text ? "#{ct};charset=#{enc}" : ct end |
#encode_body(body, ct_header) ⇒ String
Return the body with the specified encoding applied. An encoding is specified in the ‘content-type’ header of a frame by the charset
parameter. If no encoding was explicitly specified, a UTF-8 encoding is used when the frame’s content type begins with text/
, otherwise an encoding of US-ASCII is used.
13 14 15 16 17 18 19 20 21 |
# File 'lib/stomper/support/1.9/frame_serializer.rb', line 13 def encode_body(body, ct_header) body.tap do |b| charset = ct_header ? (ct_header =~ /\;\s*charset=\"?([\w\-]+)\"?/i) ? $1 : (ct_header =~ /^text\//) ? 'UTF-8' : 'ASCII-8BIT' : 'ASCII-8BIT' b.force_encoding(charset) end end |
#get_body_byte ⇒ String?
Reads a single byte from the body portion of a frame. Returns nil
if the character read is a frame terminator.
26 27 28 29 30 |
# File 'lib/stomper/support/1.9/frame_serializer.rb', line 26 def get_body_byte #raise "Implementation varies by Ruby version" c = @io.getc c == ::Stomper::FrameSerializer::FRAME_TERMINATOR ? nil : c end |
#get_header_line ⇒ String
Reads a line of text from the underlying IO. As per the Stomp 1.1 specification, all header strings are encoded with UTF-8.
59 60 61 |
# File 'lib/stomper/support/1.9/frame_serializer.rb', line 59 def get_header_line (@io.gets || '').tap { |line| line.force_encoding('UTF-8') } end |