Class: RightScale::CommandSerializer
- Defined in:
- lib/right_agent/command/command_serializer.rb
Constant Summary collapse
- SEPARATOR =
String appended to serialized data to delimit between commands
"\n#GO\n"
Class Method Summary collapse
-
.dump(command) ⇒ Object
Serialize given command so it can be sent to command listener.
-
.load(data) ⇒ Object
Deserialize command that was previously serialized with
dump. -
.set_encoding(obj, encoding = "UTF-8") ⇒ Object
Force set encodings on ruby strings.
Class Method Details
.dump(command) ⇒ Object
Serialize given command so it can be sent to command listener
Parameters
- command(Object)
-
Command to serialize
Return
- data(String)
-
Corresponding serialized data
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/right_agent/command/command_serializer.rb', line 39 def self.dump(command) # Set the encoding before serialization otherwise YAML will serialize # UTF8 characters as binary data. This can cause some quirks we'd rather # avoid, such as the deserialized binary data having no encoding on # deserialization set_encoding(command) data = YAML::dump(command) data += SEPARATOR end |
.load(data) ⇒ Object
Deserialize command that was previously serialized with dump
Parameters
- data(String)
-
String containing serialized data
Return
- command(Object)
-
Deserialized command
Raise
(RightScale::Exceptions::IO): If serialized data is incorrect
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/right_agent/command/command_serializer.rb', line 60 def self.load(data) # Data coming from eventmachine is cleaned of it's encoding, so set it # to UTF-8 manually before deserializing or you'll throw transcode errors. set_encoding(data) command = YAML::load(data) raise RightScale::Exceptions::IO, "Invalid serialized command:\n#{data}" unless command command rescue RightScale::Exceptions::IO raise rescue Exception => e raise RightScale::Exceptions::IO, "Invalid serialized command: #{e.}\n#{data}" end |
.set_encoding(obj, encoding = "UTF-8") ⇒ Object
Force set encodings on ruby strings.
Parameters
- obj(Object)
-
String, or Hash/Array of Strings
Return
- (nil)
-
Returns nothing, edits object in place
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/right_agent/command/command_serializer.rb', line 83 def self.set_encoding(obj, encoding = "UTF-8") if obj.is_a?(Hash) obj.each do |k,v| set_encoding(v, encoding) end elsif obj.is_a?(Array) obj.each do |v| set_encoding(v, encoding) end elsif obj.is_a?(String) && !obj.frozen? obj.force_encoding(encoding) if obj.respond_to?(:force_encoding) end end |