RText::MessageHelper

Public Instance Methods

each_json_object_string(object, &block) click to toggle source
# File lib/rtext/message_helper.rb, line 58
def each_json_object_string(object, &block)
  if object.is_a?(Hash)
    object.each_pair do |k, v|
      # don't call block for hash keys
      each_json_object_string(v, &block)
    end
  elsif object.is_a?(Array)
    object.each do |v|
      each_json_object_string(v, &block)
    end
  elsif object.is_a?(String)
    yield(object)
  else
    # ignore
  end
end
extract_message(data) click to toggle source
# File lib/rtext/message_helper.rb, line 29
def extract_message(data)
  # interpret input data as binary
  data.force_encoding("binary")
  obj = nil
  if data =~ /^(\d+)\{/
    length_length = $1.size
    length = $1.to_i
    if data.size >= length_length + length
      data.slice!(0..(length_length-1))
      json = data.slice!(0..length-1)
      # there shouldn't be any non ascii-7-bit characters, transcode just to be sure
      # encode from binary to utf-8 with :undef => :replace turns all non-ascii-7-bit bytes
      # into the replacement character (\uFFFD)
      json.encode!("utf-8", :undef => :replace)
      obj = JSON(json)
    end
  end
  if obj
    each_json_object_string(obj) do |s|
      # change encoding back to binary 
      # there could still be replacement characters (\uFFFD), turn them into "?"
      s.encode!("binary", :undef => :replace)
      s.gsub!(/%[0-9a-fA-F][0-9a-fA-F]/){|m| m[1..2].to_i(16).chr("binary")}
      s.force_encoding("binary")
    end
  end
  obj
end
serialize_message(obj) click to toggle source
# File lib/rtext/message_helper.rb, line 7
def serialize_message(obj)
  each_json_object_string(obj) do |s|
    s.force_encoding("binary")
    bytes = s.bytes.to_a
    s.clear
    bytes.each do |b|
      if b >= 128 || b == 0x25 # %
        s << "%#{b.to_s(16)}".force_encoding("binary")
      else
        s << b.chr("binary")
      end
    end
    # there are no non ascii-7-bit characters left
    s.force_encoding("utf-8")
  end
  json = JSON(obj)
  # the JSON method outputs data in UTF-8 encoding
  # the RText protocol expects message lengths measured in bytes
  # there shouldn't be any non-ascii-7-bit characters, though, so json.size would also be ok
  "#{json.bytesize}#{json}"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.