lib/active_remote/rpc.rb in active_remote-2.0.2 vs lib/active_remote/rpc.rb in active_remote-2.1.0.beta1
- old
+ new
@@ -1,72 +1,123 @@
+require 'active_remote/rpc_adapters/protobuf_adapter'
require 'active_remote/serializers/protobuf'
module ActiveRemote
module RPC
extend ActiveSupport::Concern
included do
- include Serializers::Protobuf
+ include Embedded
end
- module ClassMethods
+ # TODO: Deprecate this pattern of executing RPC calls
+ #
+ module Embedded
+ extend ActiveSupport::Concern
- # Execute an RPC call to the remote service and return the raw response.
- #
- def remote_call(rpc_method, request_args)
- remote = self.new
- remote.execute(rpc_method, request_args)
- remote.last_response
+ included do
+ include Serializers::Protobuf
end
- # Return a protobuf request object for the given rpc request.
- #
- def request(rpc_method, request_args)
- return request_args unless request_args.is_a?(Hash)
+ module ClassMethods
+ # Return a protobuf request object for the given rpc request.
+ #
+ # TODO: Add deprecation warning
+ #
+ def request(rpc_method, request_args)
+ return request_args unless request_args.is_a?(Hash)
- message_class = request_type(rpc_method)
- fields = fields_from_attributes(message_class, request_args)
- message_class.new(fields)
+ message_class = request_type(rpc_method)
+ fields = fields_from_attributes(message_class, request_args)
+ message_class.new(fields)
+ end
+
+ # Return the class applicable to the request for the given rpc method.
+ #
+ # TODO: Add deprecation warning
+ #
+ def request_type(rpc_method)
+ service_class.rpcs[rpc_method].request_type
+ end
end
- # Return the class applicable to the request for the given rpc method.
+ # Invoke an RPC call to the service for the given rpc method.
#
- def request_type(rpc_method)
- service_class.rpcs[rpc_method].request_type
- end
- end
+ # TODO: Add deprecation warning
+ #
+ def execute(rpc_method, request_args)
+ @last_request = request(rpc_method, request_args)
- # Invoke an RPC call to the service for the given rpc method.
- #
- def execute(rpc_method, request_args)
- @last_request = request(rpc_method, request_args)
+ _service_class.client.__send__(rpc_method, @last_request) do |c|
- _service_class.client.__send__(rpc_method, @last_request) do |c|
+ # In the event of service failure, raise the error.
+ c.on_failure do |error|
+ raise ActiveRemoteError, error.message
+ end
- # In the event of service failure, raise the error.
- c.on_failure do |error|
- raise ActiveRemoteError, error.message
+ # In the event of service success, assign the response.
+ c.on_success do |response|
+ @last_response = response
+ end
end
- # In the event of service success, assign the response.
- c.on_success do |response|
- @last_response = response
- end
+ @last_response
end
- end
- # Execute an RPC call to the remote service and return the raw response.
- #
- def remote_call(rpc_method, request_args)
- self.execute(rpc_method, request_args)
- self.last_response
+
+ # Execute an RPC call to the remote service and return the raw response.
+ #
+ # TODO: Add deprecation warning
+ #
+ def remote_call(rpc_method, request_args)
+ rpc.execute(rpc_method, request_args)
+ end
+
+ private
+
+ # Return a protobuf request object for the given rpc call.
+ #
+ # TODO: Add deprecation warning
+ #
+ def request(rpc_method, attributes)
+ self.class.request(rpc_method, attributes)
+ end
end
- private
+ module ClassMethods
- # Return a protobuf request object for the given rpc call.
- #
- def request(rpc_method, attributes)
- self.class.request(rpc_method, attributes)
+ # Execute an RPC call to the remote service and return the raw response.
+ #
+ def remote_call(rpc_method, request_args)
+ rpc.execute(rpc_method, request_args)
+ end
+
+ def rpc
+ @rpc ||= rpc_adapter.new(service_class)
+ end
+
+ def rpc_adapter
+ # TODO: Make this pluggable
+ #
+ # raise(AdapterNotSpecified, "configuration does not specify adapter") unless adapter.present?
+ #
+ # path_to_adapter = "active_remote/rpc_adapters/#{adapter}_adapter"
+ #
+ # begin
+ # require path_to_adapter
+ # rescue Gem::LoadError => e
+ # raise Gem::LoadError, "Specified '#{adapter]}' for RPC adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRemote)."
+ # rescue LoadError => e
+ # raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter is valid. If you use an adapter other than 'protobuf' add the necessary adapter gem to the Gemfile.", e.backtrace
+ # end
+ #
+ # path_to_adapter.classify.constantize
+
+ RPCAdapters::ProtobufAdapter
+ end
+ end
+
+ def rpc
+ self.class.rpc
end
end
end