Class: PipelineToolkit::MessageCoder

Inherits:
Object
  • Object
show all
Defined in:
lib/pipeline_toolkit/message_coder.rb

Overview

Encode and decode messages using Marshal

Class Method Summary collapse

Class Method Details

.decode(str) ⇒ Object

Decode the binary string into a hash

Parameters:

  • str (String)

    Binary string



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pipeline_toolkit/message_coder.rb', line 27

def self.decode(str)
  str.chomp!
  obj = MessagePack.unpack(Base64.decode64(str))

  obj = case obj
    when Hash
      # makes code a little more readable
      IndifferentHash.new(obj)
    else
      obj
  end
end

.encode(message) ⇒ Object

Encode the hash message

Parameters:

  • message (Hash)

    The message as a hash



15
16
17
18
19
20
# File 'lib/pipeline_toolkit/message_coder.rb', line 15

def self.encode(message)
  # NB: Using Msgpack because it's 9-10x faster than altnatives
  # See http://gist.github.com/190849
  # Using Base64 because ASCII is friendly to pipes.
  Base64.encode64(MessagePack.pack(message)).delete("\r\n")
end