Class: RJR::RequestMessage
Overview
Message sent from client to server to invoke a json-rpc method
Instance Attribute Summary collapse
-
#headers ⇒ Object
Optional headers to add to json outside of standard json-rpc request.
-
#jr_args ⇒ Object
Arguments source is passing to destination method.
-
#jr_method ⇒ Object
Method source is invoking on the destination.
-
#json_message ⇒ Object
Message string received from the source.
-
#msg_id ⇒ Object
ID of the message in accordance w/ json-rpc specification.
Class Method Summary collapse
-
.is_request_message?(message) ⇒ true, false
Class helper to determine if the specified string is a valid json-rpc method request.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ RequestMessage
constructor
RJR Request Message initializer.
-
#to_s ⇒ Object
Convert request message to string json format.
Constructor Details
#initialize(args = {}) ⇒ RequestMessage
RJR Request Message initializer
This should be invoked with one of two argument sets. If creating a new message to send to the server, specify :method, :args, and :headers to include in the message (message id will be autogenerated). If handling an new request message sent from the client, simply specify :message and optionally any additional headers (they will be merged with the headers contained in the message)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rjr/message.rb', line 47 def initialize(args = {}) if args.has_key?(:message) begin = args[:message] request = JSON.parse() @jr_method = request['method'] @jr_args = request['params'] @msg_id = request['id'] @headers = args.has_key?(:headers) ? {}.merge!(args[:headers]) : {} request.keys.select { |k| !['jsonrpc', 'id', 'method', 'params'].include?(k) }.each { |k| @headers[k] = request[k] } rescue Exception => e #puts "Exception Parsing Request #{e}" raise e end elsif args.has_key?(:method) @jr_method = args[:method] @jr_args = args[:args] @headers = args[:headers] @msg_id = gen_uuid end end |
Instance Attribute Details
#headers ⇒ Object
Optional headers to add to json outside of standard json-rpc request
32 33 34 |
# File 'lib/rjr/message.rb', line 32 def headers @headers end |
#jr_args ⇒ Object
Arguments source is passing to destination method
26 27 28 |
# File 'lib/rjr/message.rb', line 26 def jr_args @jr_args end |
#jr_method ⇒ Object
Method source is invoking on the destination
23 24 25 |
# File 'lib/rjr/message.rb', line 23 def jr_method @jr_method end |
#json_message ⇒ Object
Message string received from the source
20 21 22 |
# File 'lib/rjr/message.rb', line 20 def end |
#msg_id ⇒ Object
ID of the message in accordance w/ json-rpc specification
29 30 31 |
# File 'lib/rjr/message.rb', line 29 def msg_id @msg_id end |
Class Method Details
.is_request_message?(message) ⇒ true, false
Class helper to determine if the specified string is a valid json-rpc method request
79 80 81 82 83 84 85 86 87 |
# File 'lib/rjr/message.rb', line 79 def self.() begin # FIXME log error parsed = JSON.parse() parsed.has_key?('method') && parsed.has_key?('id') rescue Exception => e false end end |
Instance Method Details
#to_s ⇒ Object
Convert request message to string json format
90 91 92 93 94 95 96 97 |
# File 'lib/rjr/message.rb', line 90 def to_s request = { 'jsonrpc' => '2.0', 'method' => @jr_method, 'params' => @jr_args } request['id'] = @msg_id unless @msg_id.nil? request.merge!(@headers) unless @headers.nil? request.to_json.to_s end |