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_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_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.
-
#to_json(*a) ⇒ Object
Convert request to json representation and return it.
Constructor Details
#initialize(args = {}) ⇒ Request
RJR Request initializer
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rjr/dispatcher.rb', line 117 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] @result = nil end |
Instance Attribute Details
#result ⇒ Object
Result of the request operation, set by dispatcher
88 89 90 |
# File 'lib/rjr/dispatcher.rb', line 88 def result @result end |
#rjr_headers ⇒ Object
Headers which came w/ request
97 98 99 |
# File 'lib/rjr/dispatcher.rb', line 97 def rjr_headers @rjr_headers end |
#rjr_method ⇒ Object
Method which request is for
91 92 93 |
# File 'lib/rjr/dispatcher.rb', line 91 def rjr_method @rjr_method end |
#rjr_method_args ⇒ Object
Arguments be passed to method
94 95 96 |
# File 'lib/rjr/dispatcher.rb', line 94 def rjr_method_args @rjr_method_args end |
#rjr_node_id ⇒ Object
ID of node which request came in on
103 104 105 |
# File 'lib/rjr/dispatcher.rb', line 103 def rjr_node_id @rjr_node_id end |
#rjr_node_type ⇒ Object
Type of node which request came in on
100 101 102 |
# File 'lib/rjr/dispatcher.rb', line 100 def rjr_node_type @rjr_node_type end |
Class Method Details
.json_create(o) ⇒ Object
Create new request from json representation
170 171 172 173 174 175 |
# File 'lib/rjr/dispatcher.rb', line 170 def self.json_create(o) result = Result.new(o['data']['result']) request = Request.new(o['data']['request']) request.result = result return request end |
Instance Method Details
#handle ⇒ Object
Invoke the request by calling the registered handler with the registered method parameters in the local scope
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rjr/dispatcher.rb', line 137 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}" retval = instance_exec(*@rjr_method_args, &@rjr_handler) RJR::Logger.info \ "#{node_sig}<-#{method_sig}<-#{retval.nil? ? "nil" : retval}" return retval end |
#to_json(*a) ⇒ Object
Convert request to json representation and return it
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rjr/dispatcher.rb', line 152 def to_json(*a) { 'json_class' => self.class.name, 'data' => {: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 }, :result => { :result => @result.result, :error_code => @result.error_code, :error_msg => @result.error_msg, :error_class => @result.error_class } } }.to_json(*a) end |