Class: SneakersPacker::MessagePacker

Inherits:
Object
  • Object
show all
Defined in:
lib/sneakers_packer/message_packer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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_nameObject (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

Examples:

param message = "{\"from\":\"boohee\", \"data\":12}"
return array [12, "boohee"]

Parameters:

  • body

    response raw data

Returns:

  • hash



36
37
38
39
# File 'lib/sneakers_packer/message_packer.rb', line 36

def unpack_request(message)
  hash = unpack_message(message)
  [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

Examples:

param message = "{\"status\":200, \"data\":2}"
return array [2, 200]

Parameters:

  • body

    response raw data

Returns:

  • hash



53
54
55
56
# File 'lib/sneakers_packer/message_packer.rb', line 53

def unpack_response(message)
  hash = unpack_message(message)
  [hash["data"], hash["from"], hash["status"]]
end