Class: Protobuf::Rpc::Client
- Inherits:
-
Object
- Object
- Protobuf::Rpc::Client
- Extended by:
- Forwardable
- Includes:
- Logger::LogMethods
- Defined in:
- lib/protobuf/rpc/client.rb
Instance Attribute Summary collapse
-
#connector ⇒ Object
readonly
Returns the value of attribute connector.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Client
constructor
Create a new client with default options (defined in ClientConnection) See Service#client for a more convenient way to create a client, as well as Client#method_missing defined below.
- #log_signature ⇒ Object
-
#method_missing(method, *params) ⇒ Object
Provides a mechanism to call the service method against the client which will automatically setup the service_class and method_name in the wrapper protobuf request.
-
#on_complete(&complete_cb) ⇒ Object
Set a complete callback on the client to return the object (self).
- #on_complete=(callable) ⇒ Object
-
#on_failure(&failure_cb) ⇒ Object
Set a failure callback on the client to return the error returned by the service, if any.
- #on_failure=(callable) ⇒ Object
-
#on_success(&success_cb) ⇒ Object
Set a success callback on the client to return the successful response from the service when it is returned.
- #on_success=(callable) ⇒ Object
-
#send_request ⇒ Object
Send the request to the service through eventmachine.
Constructor Details
#initialize(opts = {}) ⇒ Client
Create a new client with default options (defined in ClientConnection) See Service#client for a more convenient way to create a client, as well as Client#method_missing defined below.
request = WidgetFindRequest.new client = Client.new({ :service => WidgetService, :method => "find", :request_type => "WidgetFindRequest", :response_type => "WidgetList", :request => request })
29 30 31 32 33 |
# File 'lib/protobuf/rpc/client.rb', line 29 def initialize(opts={}) raise "Invalid client configuration. Service must be defined." if opts[:service].nil? @connector = Connector.connector_for_client.new(opts) log_debug "[#{log_signature}] Initialized with options: %s" % opts.inspect end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *params) ⇒ Object
Provides a mechanism to call the service method against the client which will automatically setup the service_class and method_name in the wrapper protobuf request.
# The :find method is not defined by Client which will trigger method_missing
Client.new(:service => WidgetService).find do |c|
# This block will be invoked before the request is made
# c
in this case is the client object you created above
c.on_success {|res| ... }
c.on_failure {|err| ... }
end
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/protobuf/rpc/client.rb', line 109 def method_missing(method, *params) service = [:service] unless service.rpcs[service].keys.include?(method) log_error "[#{log_signature}] %s#%s not rpc method, passing to super" % [service.name, method.to_s] super(method, *params) else log_debug "[#{log_signature}] %s#%s" % [service.name, method.to_s] rpc = service.rpcs[service][method.to_sym] [:request_type] = rpc.request_type log_debug "[#{log_signature}] Request Type: %s" % [:request_type].name [:response_type] = rpc.response_type log_debug "[#{log_signature}] Response Type: %s" % [:response_type].name [:method] = method.to_s [:request] = params[0].is_a?(Hash) ? [:request_type].new(params[0]) : params[0] log_debug "[#{log_signature}] Request Data: %s" % [:request].inspect # Call client to setup on_success and on_failure event callbacks if block_given? log_debug "[#{log_signature}] client setup callback given, invoking" yield(self) else log_debug "[#{log_signature}] no block given for callbacks" end send_request end end |
Instance Attribute Details
#connector ⇒ Object (readonly)
Returns the value of attribute connector.
14 15 16 |
# File 'lib/protobuf/rpc/client.rb', line 14 def connector @connector end |
Instance Method Details
#log_signature ⇒ Object
35 36 37 |
# File 'lib/protobuf/rpc/client.rb', line 35 def log_signature @log_signature ||= "client-#{self.class}" end |
#on_complete(&complete_cb) ⇒ Object
Set a complete callback on the client to return the object (self). Callback is called regardless of :async setting.
client = Client.new(:service => WidgetService) client.on_complete {|obj| ... }
45 46 47 |
# File 'lib/protobuf/rpc/client.rb', line 45 def on_complete(&complete_cb) @connector.complete_cb = complete_cb end |
#on_complete=(callable) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/protobuf/rpc/client.rb', line 49 def on_complete=(callable) if callable != nil && !callable.respond_to?(:call) && callable.arity != 1 raise "callable must take a single argument and respond to :call" end @connector.complete_cb = callable end |
#on_failure(&failure_cb) ⇒ Object
Set a failure callback on the client to return the error returned by the service, if any. If this callback is called, success_cb will NOT be called. Callback is called regardless of :async setting.
client = Client.new(:service => WidgetService) client.on_failure {|err| ... }
65 66 67 |
# File 'lib/protobuf/rpc/client.rb', line 65 def on_failure(&failure_cb) @connector.failure_cb = failure_cb end |
#on_failure=(callable) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/protobuf/rpc/client.rb', line 69 def on_failure=(callable) if callable != nil && !callable.respond_to?(:call) && callable.arity != 1 raise "callable must take a single argument and respond to :call" end @connector.failure_cb = callable end |
#on_success(&success_cb) ⇒ Object
Set a success callback on the client to return the successful response from the service when it is returned. If this callback is called, failure_cb will NOT be called. Callback is called regardless of :async setting.
client = Client.new(:service => WidgetService) client.on_success {|res| ... }
85 86 87 |
# File 'lib/protobuf/rpc/client.rb', line 85 def on_success(&success_cb) @connector.success_cb = success_cb end |
#on_success=(callable) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/protobuf/rpc/client.rb', line 89 def on_success=(callable) if callable != nil && !callable.respond_to?(:call) && callable.arity != 1 raise "callable must take a single argument and respond to :call" end @connector.success_cb = callable end |
#send_request ⇒ Object
Send the request to the service through eventmachine. This method is usually never called directly but is invoked by method_missing (see docs above).
request = WidgetFindRequest.new client = Client.new({ :service => WidgetService, :method => "find", :request_type => "WidgetFindRequest", :response_type => "WidgetList", :request => request })
client.on_success do |res| res.widgets.each{|w| puts w.inspect } end
client.on_failure do |err| puts err.message end
client.send_request
160 161 162 |
# File 'lib/protobuf/rpc/client.rb', line 160 def send_request @connector.send_request end |