class RJR::Request

JSON-RPC request representation.

Registered request handlers will be invoked in the context of instances of this class, meaning all member variables will be available for use in the handler.

Attributes

result[RW]

Result of the request operation, set by dispatcher

rjr_args[RW]

Argument object encapsulating arguments

rjr_callback[RW]

RJR callback which may be used to push data to client

rjr_client_ip[RW]

Client IP which request came in on (only for direct nodes)

rjr_client_port[RW]

Port which request came in on (only for direct nodes)

rjr_env[RW]

Environment handler will be run in

rjr_handler[RW]

Actual proc registered to handle request

rjr_headers[RW]

Headers which came w/ request

rjr_method[RW]

Method which request is for

rjr_method_args[RW]

Arguments be passed to method

rjr_node[RW]

Node which the request came in on

rjr_node_id[RW]

ID of node which request came in on

rjr_node_type[RW]

Type of node which request came in on

Public Class Methods

json_create(o) click to toggle source

Create new request from json representation

# File lib/rjr/request.rb, line 130
def self.json_create(o)
  result  = Result.new(o['data']['result'])
  request = Request.new(o['data']['request'])
  request.result = result
  return request
end
new(args = {}) click to toggle source

RJR Request initializer

@param [Hash] args options to set on request,

see Request accessors for valid keys
# File lib/rjr/request.rb, line 63
def initialize(args = {})
  @rjr_method      = args[:rjr_method]      || args['rjr_method']
  @rjr_method_args = args[:rjr_method_args] || args['rjr_method_args'] || []
  @rjr_headers     = args[:rjr_headers]     || args['rjr_headers']

  @rjr_client_ip   = args[:rjr_client_ip]
  @rjr_client_port = args[:rjr_client_port]

  @rjr_callback    = args[:rjr_callback]
  @rjr_node        = args[:rjr_node]
  @rjr_node_id     = args[:rjr_node_id]     || args['rjr_node_id']
  @rjr_node_type   = args[:rjr_node_type]   || args['rjr_node_type']

  @rjr_handler     = args[:rjr_handler]

  @rjr_args        = Arguments.new :args => @rjr_method_args
  @rjr_env         = nil
  @result          = nil
end

Public Instance Methods

handle() click to toggle source

Invoke the request by calling the registered handler with the registered method parameters in the local scope

# File lib/rjr/request.rb, line 91
def handle
  node_sig   = "#{@rjr_node_id}(#{@rjr_node_type})"
  method_sig = "#{@rjr_method}(#{@rjr_method_args.join(',')})"

  RJR::Logger.info "#{node_sig}->#{method_sig}"

  # TODO option to compare arity of handler to number
  # of method_args passed in ?
  retval = instance_exec(*@rjr_method_args, &@rjr_handler)

  RJR::Logger.info \
    "#{node_sig}<-#{method_sig}<-#{retval.nil? ? "nil" : retval}"

  return retval
end
request_json() click to toggle source
# File lib/rjr/request.rb, line 107
def request_json
  {:request => { :rjr_method      => @rjr_method,
                 :rjr_method_args => @rjr_method_args,
                 :rjr_headers     => @rjr_headers,
                 :rjr_node_type   => @rjr_node_type,
                 :rjr_node_id     => @rjr_node_id }}
end
result_json() click to toggle source
# File lib/rjr/request.rb, line 115
def result_json
  return {} unless !!@result
  {:result  => { :result          => @result.result,
                 :error_code      => @result.error_code,
                 :error_msg       => @result.error_msg,
                 :error_class     => @result.error_class }}
end
set_env(env) click to toggle source

Set the environment by extending Request instance with the specified module

# File lib/rjr/request.rb, line 84
def set_env(env)
  @rjr_env = env
  self.extend(env)
end
to_json(*a) click to toggle source

Convert request to json representation and return it

# File lib/rjr/request.rb, line 124
def to_json(*a)
  {'json_class' => self.class.name,
   'data'       => request_json.merge(result_json)}.to_json(*a)
end