Class: Protobuf::Rpc::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/protobuf/rpc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

initialize_logger, #log_exception, #logger, #sign_message

Constructor Details

#initialize(options = {}) ⇒ 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 })



28
29
30
31
32
# File 'lib/protobuf/rpc/client.rb', line 28

def initialize(options = {})
  fail "Invalid client configuration. Service must be defined." if options[:service].nil?
  @connector = ::Protobuf.connector_type_class.new(options)
  logger.debug { sign_message("Initialized with options: #{options.inspect}") }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *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



105
106
107
108
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
# File 'lib/protobuf/rpc/client.rb', line 105

def method_missing(method_name, *params)
  service = options[:service]
  if service.rpc_method?(method_name)
    logger.debug { sign_message("#{service.name}##{method_name}") }
    rpc = service.rpcs[method_name.to_sym]

    options[:request_type] = rpc.request_type
    logger.debug { sign_message("Request Type: #{options[:request_type].name}") }

    options[:response_type] = rpc.response_type
    logger.debug { sign_message("Response Type: #{options[:response_type].name}") }

    options[:method] = method_name.to_s
    options[:request] = params[0].is_a?(Hash) ? options[:request_type].new(params[0]) : params[0]
    logger.debug { sign_message("Request Data: #{options[:request].inspect}") }

    # Call client to setup on_success and on_failure event callbacks
    if block_given?
      logger.debug { sign_message("client setup callback given, invoking") }
      yield(self)
    else
      logger.debug { sign_message("no block given for callbacks") }
    end

    send_request
  else
    logger.error { sign_message("#{service.name}##{method_name} not rpc method, passing to super") }
    super(method_name, *params)
  end
end

Instance Attribute Details

#connectorObject (readonly)

Returns the value of attribute connector.



13
14
15
# File 'lib/protobuf/rpc/client.rb', line 13

def connector
  @connector
end

Instance Method Details

#log_signatureObject



34
35
36
# File 'lib/protobuf/rpc/client.rb', line 34

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).

client = Client.new(:service => WidgetService) client.on_complete {|obj| ... }



43
44
45
# File 'lib/protobuf/rpc/client.rb', line 43

def on_complete(&complete_cb)
  @connector.complete_cb = complete_cb
end

#on_complete=(callable) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/protobuf/rpc/client.rb', line 47

def on_complete=(callable)
  if !callable.nil? && !callable.respond_to?(:call) && callable.arity != 1
    fail "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.

client = Client.new(:service => WidgetService) client.on_failure {|err| ... }



62
63
64
# File 'lib/protobuf/rpc/client.rb', line 62

def on_failure(&failure_cb)
  @connector.failure_cb = failure_cb
end

#on_failure=(callable) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/protobuf/rpc/client.rb', line 66

def on_failure=(callable)
  if !callable.nil? && !callable.respond_to?(:call) && callable.arity != 1
    fail "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.

client = Client.new(:service => WidgetService) client.on_success {|res| ... }



81
82
83
# File 'lib/protobuf/rpc/client.rb', line 81

def on_success(&success_cb)
  @connector.success_cb = success_cb
end

#on_success=(callable) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/protobuf/rpc/client.rb', line 85

def on_success=(callable)
  if !callable.nil? && !callable.respond_to?(:call) && callable.arity != 1
    fail "Callable must take a single argument and respond to :call"
  end

  @connector.success_cb = callable
end