Class: MarilynRPC::Gentleman
- Inherits:
-
Object
- Object
- MarilynRPC::Gentleman
- Defined in:
- lib/marilyn-rpc/gentleman.rb
Overview
The gentleman is a proxy onject that should help to create async responses on the server (service) side. There are two ways to use the gentleman. See the examples.
The Gentleman has to be returned by the service method.
Instance Attribute Summary collapse
-
#callback ⇒ Proc
the callback that will be called when the deferable was successful.
-
#connection ⇒ Object
the connection where the response should be send.
-
#tag ⇒ Object
the tag that should be used for the response.
Class Method Summary collapse
-
.proxy(&block) ⇒ Object
Creates a anonymous Gentleman proxy where the helper is exposed to, be able to use the Gentleman in situations where only a callback can be passed.
Instance Method Summary collapse
-
#handle(*args) ⇒ Object
private
The handler that will send the response to the remote system.
-
#helper ⇒ Object
private
The helper that will be called by the deferable to call #handle later.
-
#initialize(deferable = nil, &callback) ⇒ Gentleman
constructor
create a new proxy object using a deferable or the passed block.
Constructor Details
#initialize(deferable = nil, &callback) ⇒ Gentleman
create a new proxy object using a deferable or the passed block.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/marilyn-rpc/gentleman.rb', line 32 def initialize(deferable = nil, &callback) @callback = callback if deferable unless deferable.respond_to? :callback raise ArgumentError.new("Wrong type, expected object that responds to #callback!") end gentleman = self deferable.callback { |*args| gentleman.handle(*args) } end end |
Instance Attribute Details
#callback ⇒ Proc
the callback that will be called when the deferable was successful
27 28 29 |
# File 'lib/marilyn-rpc/gentleman.rb', line 27 def callback @callback end |
#connection ⇒ Object
the connection where the response should be send
27 28 29 |
# File 'lib/marilyn-rpc/gentleman.rb', line 27 def connection @connection end |
#tag ⇒ Object
the tag that should be used for the response
27 28 29 |
# File 'lib/marilyn-rpc/gentleman.rb', line 27 def tag @tag end |
Class Method Details
.proxy(&block) ⇒ Object
Creates a anonymous Gentleman proxy where the helper is exposed to, be able to use the Gentleman in situations where only a callback can be passed.
45 46 47 48 49 |
# File 'lib/marilyn-rpc/gentleman.rb', line 45 def self.proxy(&block) gentleman = MarilynRPC::Gentleman.new gentleman.callback = block.call(gentleman.helper) gentleman end |
Instance Method Details
#handle(*args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The handler that will send the response to the remote system
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/marilyn-rpc/gentleman.rb', line 55 def handle(*args) mail = MarilynRPC::CallResponseMail.new(self.tag, self.callback.call(*args)) data = MarilynRPC::Envelope.new(mail.encode, MarilynRPC::CallResponseMail::TYPE).encode connection.send_data(data) rescue Exception => exception mail = MarilynRPC::ExceptionMail.new(self.tag, exception) data = MarilynRPC::Envelope.new(mail.encode, MarilynRPC::ExceptionMail::TYPE).encode connection.send_data(data) end |
#helper ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The helper that will be called by the deferable to call #handle later
70 71 72 73 |
# File 'lib/marilyn-rpc/gentleman.rb', line 70 def helper gentleman = self lambda { |*args| gentleman.handle(*args) } end |