Class: MessagePack::RPCOverHTTP::Server::ResponsePacker

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/rpc_over_http/server/response_packer.rb

Overview

Rack Middleware that packs a result of a handler method and create a HTTP Responce.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ ResponsePacker

Returns a new instance of ResponsePacker.



8
9
10
# File 'lib/msgpack/rpc_over_http/server/response_packer.rb', line 8

def initialize(dispatcher)
  @dispatcher = dispatcher
end

Class Method Details

.pack(msgid, error, result) ⇒ Object



35
36
37
# File 'lib/msgpack/rpc_over_http/server/response_packer.rb', line 35

def self.pack(msgid, error, result)
  return MessagePack.pack([RESPONSE, msgid, error, result])
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/msgpack/rpc_over_http/server/response_packer.rb', line 12

def call(env)
  res = Rack::Response.new
  res["Content-type"] = "text/plain"
  msgid = env['msgpack-rpc.msgid']

  body = @dispatcher.call(env)
  if body.is_a?(Streamer::Body)
    body.msgid = msgid
    res.body = body
    return [res.status.to_i, res.header, body]
  else
    res.write self.class.pack(msgid, nil, body)
    return res.finish
  end
rescue RPCOverHTTP::RemoteError => ex
  res.write self.class.pack(msgid, ex.class.name, ex.message)
  return res.finish
rescue ::RuntimeError => ex
  res.write(self.class.pack(
      msgid, RPCOverHTTP::RuntimeError.name, ex.message))
  return res.finish
end