Class: RJR::Request
Overview
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.
Instance Attribute Summary collapse
-
#result ⇒ Object
Result of the request operation, set by dispatcher.
-
#rjr_args ⇒ Object
Argument object encapsulating arguments.
-
#rjr_callback ⇒ Object
RJR callback which may be used to push data to client.
-
#rjr_client_ip ⇒ Object
Client IP which request came in on (only for direct nodes).
-
#rjr_client_port ⇒ Object
Port which request came in on (only for direct nodes).
-
#rjr_handler ⇒ Object
Actual proc registered to handle request.
-
#rjr_headers ⇒ Object
Headers which came w/ request.
-
#rjr_method ⇒ Object
Method which request is for.
-
#rjr_method_args ⇒ Object
Arguments be passed to method.
-
#rjr_node ⇒ Object
Node which the request came in on.
-
#rjr_node_id ⇒ Object
ID of node which request came in on.
-
#rjr_node_type ⇒ Object
Type of node which request came in on.
Class Method Summary collapse
-
.json_create(o) ⇒ Object
Create new request from json representation.
Instance Method Summary collapse
-
#handle ⇒ Object
Invoke the request by calling the registered handler with the registered method parameters in the local scope.
-
#initialize(args = {}) ⇒ Request
constructor
RJR Request initializer.
- #request_json ⇒ Object
- #result_json ⇒ Object
-
#to_json(*a) ⇒ Object
Convert request to json representation and return it.
Constructor Details
#initialize(args = {}) ⇒ Request
RJR Request initializer
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rjr/request.rb', line 60 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 @result = nil end |
Instance Attribute Details
#result ⇒ Object
Result of the request operation, set by dispatcher
21 22 23 |
# File 'lib/rjr/request.rb', line 21 def result @result end |
#rjr_args ⇒ Object
Argument object encapsulating arguments
30 31 32 |
# File 'lib/rjr/request.rb', line 30 def rjr_args @rjr_args end |
#rjr_callback ⇒ Object
RJR callback which may be used to push data to client
42 43 44 |
# File 'lib/rjr/request.rb', line 42 def rjr_callback @rjr_callback end |
#rjr_client_ip ⇒ Object
Client IP which request came in on (only for direct nodes)
36 37 38 |
# File 'lib/rjr/request.rb', line 36 def rjr_client_ip @rjr_client_ip end |
#rjr_client_port ⇒ Object
Port which request came in on (only for direct nodes)
39 40 41 |
# File 'lib/rjr/request.rb', line 39 def rjr_client_port @rjr_client_port end |
#rjr_handler ⇒ Object
Actual proc registered to handle request
54 55 56 |
# File 'lib/rjr/request.rb', line 54 def rjr_handler @rjr_handler end |
#rjr_headers ⇒ Object
Headers which came w/ request
33 34 35 |
# File 'lib/rjr/request.rb', line 33 def rjr_headers @rjr_headers end |
#rjr_method ⇒ Object
Method which request is for
24 25 26 |
# File 'lib/rjr/request.rb', line 24 def rjr_method @rjr_method end |
#rjr_method_args ⇒ Object
Arguments be passed to method
27 28 29 |
# File 'lib/rjr/request.rb', line 27 def rjr_method_args @rjr_method_args end |
#rjr_node ⇒ Object
Node which the request came in on
45 46 47 |
# File 'lib/rjr/request.rb', line 45 def rjr_node @rjr_node end |
#rjr_node_id ⇒ Object
ID of node which request came in on
51 52 53 |
# File 'lib/rjr/request.rb', line 51 def rjr_node_id @rjr_node_id end |
#rjr_node_type ⇒ Object
Type of node which request came in on
48 49 50 |
# File 'lib/rjr/request.rb', line 48 def rjr_node_type @rjr_node_type end |
Class Method Details
Instance Method Details
#handle ⇒ Object
Invoke the request by calling the registered handler with the registered method parameters in the local scope
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rjr/request.rb', line 81 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 ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/rjr/request.rb', line 97 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 ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/rjr/request.rb', line 105 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 |
#to_json(*a) ⇒ Object
Convert request to json representation and return it
114 115 116 117 |
# File 'lib/rjr/request.rb', line 114 def to_json(*a) {'json_class' => self.class.name, 'data' => request_json.merge(result_json)}.to_json(*a) end |