class Protobuf::Rpc::Middleware::ResponseEncoder
Attributes
app[R]
env[R]
Public Class Methods
new(app)
click to toggle source
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 9 def initialize(app) @app = app end
Public Instance Methods
_call(env)
click to toggle source
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 17 def _call(env) @env = app.call(env) env.response = response env.encoded_response = encoded_response env end
call(env)
click to toggle source
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 13 def call(env) dup._call(env) end
log_signature()
click to toggle source
Calls superclass method
Protobuf::Logging#log_signature
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 25 def log_signature env.log_signature || super end
Private Instance Methods
encoded_response()
click to toggle source
Encode the response wrapper to return to the client
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 33 def encoded_response logger.debug { sign_message("Encoding response: #{response.inspect}") } env.encoded_response = wrapped_response.encode rescue => exception log_exception(exception) # Rescue encoding exceptions, re-wrap them as generic protobuf errors, # and re-raise them raise PbError, exception.message end
response()
click to toggle source
Prod the object to see if we can produce a proto object as a response candidate. Validate the candidate protos.
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 47 def response return @response unless @response.nil? candidate = env.response return @response = validate!(candidate) if candidate.is_a?(Message) return @response = validate!(candidate.to_proto) if candidate.respond_to?(:to_proto) return @response = env.response_type.new(candidate.to_hash) if candidate.respond_to?(:to_hash) return @response = candidate if candidate.is_a?(PbError) @response = validate!(candidate) end
validate!(candidate)
click to toggle source
Ensure that the response candidate we've been given is of the type we expect so that deserialization on the client side works.
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 62 def validate!(candidate) if candidate.class != env.response_type fail BadResponseProto, "Expected response to be of type #{env.response_type.name} but was #{candidate.class.name}" end candidate end
wrapped_response()
click to toggle source
The middleware stack returns either an error or response proto. Package it up so that it's in the correct spot in the response wrapper
# File lib/protobuf/rpc/middleware/response_encoder.rb, line 73 def wrapped_response if response.is_a?(::Protobuf::Rpc::PbError) ::Protobuf::Socketrpc::Response.new(:error => response.message, :error_reason => response.error_type, :server => env.server) else ::Protobuf::Socketrpc::Response.new(:response_proto => response.encode, :server => env.server) end end