Sha256: d1aefce3eeaa674f71f7102cc0a6d61d88e1e885e017af3c9d0ced9d8cbfa199

Contents?: true

Size: 1.92 KB

Versions: 23

Compression:

Stored size: 1.92 KB

Contents

require 'active_remote/serializers/protobuf'

module ActiveRemote
  module RPC
    def self.included(klass)
      klass.class_eval do
        extend ::ActiveRemote::RPC::ClassMethods
        include ::ActiveRemote::Serializers::Protobuf
      end
    end

    module ClassMethods
      # 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
      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)

        message_class = request_type(rpc_method)
        build_message(message_class, request_args)
      end

      # Return the class applicable to the request for the given rpc method.
      #
      def request_type(rpc_method)
        service_class.rpcs[rpc_method].request_type
      end
    end

    # 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|

        # In the event of service failure, raise the error.
        c.on_failure do |error|
          raise ActiveRemoteError, error.message
        end

        # In the event of service success, assign the response.
        c.on_success do |response|
          @last_response = response
        end
      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
    end

  private

    # Return a protobuf request object for the given rpc call.
    #
    def request(rpc_method, attributes)
      self.class.request(rpc_method, attributes)
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
active_remote-1.8.1 lib/active_remote/rpc.rb
active_remote-1.8.0 lib/active_remote/rpc.rb
active_remote-1.8.0.rc1 lib/active_remote/rpc.rb
active_remote-1.7.1 lib/active_remote/rpc.rb
active_remote-1.7.0 lib/active_remote/rpc.rb
active_remote-1.6.1 lib/active_remote/rpc.rb
active_remote-1.6.0 lib/active_remote/rpc.rb
active_remote-1.5.9 lib/active_remote/rpc.rb
active_remote-1.5.8 lib/active_remote/rpc.rb
active_remote-1.5.7 lib/active_remote/rpc.rb
active_remote-1.5.6 lib/active_remote/rpc.rb
active_remote-1.5.5 lib/active_remote/rpc.rb
active_remote-1.5.4 lib/active_remote/rpc.rb
active_remote-1.5.2 lib/active_remote/rpc.rb
active_remote-1.5.1 lib/active_remote/rpc.rb
active_remote-1.5.0 lib/active_remote/rpc.rb
active_remote-1.4.1 lib/active_remote/rpc.rb
active_remote-1.4.0 lib/active_remote/rpc.rb
active_remote-1.3.3 lib/active_remote/rpc.rb
active_remote-1.3.2 lib/active_remote/rpc.rb