Class: SneakersPacker::MessagePacker
- Inherits:
-
Object
- Object
- SneakersPacker::MessagePacker
- Defined in:
- lib/sneakers_packer/message_packer.rb
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#host_name ⇒ Object
readonly
Returns the value of attribute host_name.
Instance Method Summary collapse
-
#initialize(app_name = nil) ⇒ MessagePacker
constructor
A new instance of MessagePacker.
-
#pack_request(data) ⇒ Object
Pack request data with standart json format It should include from_info and data @param data payload for request @return [string] json string body @example param data = 12 return {“from” : “one boohee-tiger 6354”, “data” : 12 } @example param data = [“ok”, name: “vincent”] return “: "one boohee-tiger 6354", "data" : ["1", {"name" : "vincent"]}”.
- #pack_response(data, status) ⇒ Object
-
#unpack_request(message) ⇒ Object
Unpack request data which is standart json format It should include status, message(optional) and data.
-
#unpack_response(message) ⇒ Object
Unpack response data which is standart json format It should include status, message(optional) and data.
Constructor Details
#initialize(app_name = nil) ⇒ MessagePacker
Returns a new instance of MessagePacker.
8 9 10 11 |
# File 'lib/sneakers_packer/message_packer.rb', line 8 def initialize(app_name = nil) @app_name = app_name || 'unknown' @host_name = Socket.gethostname end |
Instance Attribute Details
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
6 7 8 |
# File 'lib/sneakers_packer/message_packer.rb', line 6 def app_name @app_name end |
#host_name ⇒ Object (readonly)
Returns the value of attribute host_name.
6 7 8 |
# File 'lib/sneakers_packer/message_packer.rb', line 6 def host_name @host_name end |
Instance Method Details
#pack_request(data) ⇒ Object
Pack request data with standart json format It should include from_info and data
@param data payload for request
@return [string] json string body
@example
param data = 12
return {"from" : "one boohee-tiger 6354", "data" : 12 }
@example
param data = ["ok", name: "vincent"]
return "{\"from\" : \"one boohee-tiger 6354\", \"data\" : [\"1\", {\"name\" : \"vincent\"}]}"
24 25 26 |
# File 'lib/sneakers_packer/message_packer.rb', line 24 def pack_request(data) MultiJson.dump "data" => data, "from" => from_info end |
#pack_response(data, status) ⇒ Object
41 42 43 |
# File 'lib/sneakers_packer/message_packer.rb', line 41 def pack_response(data, status) MultiJson.dump "data" => data, "from" => from_info, "status" => status end |
#unpack_request(message) ⇒ Object
Unpack request data which is standart json format It should include status, message(optional) and data
36 37 38 39 |
# File 'lib/sneakers_packer/message_packer.rb', line 36 def unpack_request() hash = () [hash["data"], hash["from"]] end |
#unpack_response(message) ⇒ Object
Unpack response data which is standart json format It should include status, message(optional) and data
53 54 55 56 |
# File 'lib/sneakers_packer/message_packer.rb', line 53 def unpack_response() hash = () [hash["data"], hash["from"], hash["status"]] end |