pipeline_toolkit 1.4.1 → 1.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pipeline_toolkit/message_coder.rb +5 -16
- metadata +2 -2
@@ -1,13 +1,11 @@
|
|
1
1
|
require 'msgpack'
|
2
|
+
require 'base64'
|
2
3
|
|
3
4
|
module PipelineToolkit
|
4
5
|
##
|
5
6
|
# Encode and decode messages using Marshal
|
6
7
|
#
|
7
8
|
class MessageCoder
|
8
|
-
|
9
|
-
UNESCAPE_MAP = {"@@" => "@","@r" => "\r","@n" => "\n"}
|
10
|
-
REGEX = UNESCAPE_MAP.keys.join "|"
|
11
9
|
|
12
10
|
##
|
13
11
|
# Encode the hash message
|
@@ -17,10 +15,8 @@ module PipelineToolkit
|
|
17
15
|
def self.encode(message)
|
18
16
|
# NB: Using Msgpack because it's 9-10x faster than altnatives
|
19
17
|
# See http://gist.github.com/190849
|
20
|
-
|
21
|
-
|
22
|
-
# escape newlines. order is important.
|
23
|
-
str.gsub(/@/, "@@").gsub(/\n/, "@n").gsub(/\r/, "@r")
|
18
|
+
# Using Base64 because ASCII is friendly to pipes.
|
19
|
+
Base64.encode64(MessagePack.pack(message)).delete("\r\n")
|
24
20
|
end
|
25
21
|
|
26
22
|
##
|
@@ -30,14 +26,7 @@ module PipelineToolkit
|
|
30
26
|
#
|
31
27
|
def self.decode(str)
|
32
28
|
str.chomp!
|
33
|
-
|
34
|
-
# unescape newlines
|
35
|
-
|
36
|
-
str.gsub(/#{REGEX}/) do |m|
|
37
|
-
UNESCAPE_MAP[m]
|
38
|
-
end
|
39
|
-
|
40
|
-
obj = MessagePack.unpack(str)
|
29
|
+
obj = MessagePack.unpack(Base64.decode64(str))
|
41
30
|
|
42
31
|
obj = case obj
|
43
32
|
when Hash
|
@@ -50,4 +39,4 @@ module PipelineToolkit
|
|
50
39
|
|
51
40
|
end
|
52
41
|
|
53
|
-
end
|
42
|
+
end
|