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)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rjr/message.rb', line 43 def initialize(args = {}) if args.has_key?(:message) begin = args[:message] request = RJR.parse_json() @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
28 29 30 |
# File 'lib/rjr/message.rb', line 28 def headers @headers end |
#jr_args ⇒ Object
Arguments source is passing to destination method
22 23 24 |
# File 'lib/rjr/message.rb', line 22 def jr_args @jr_args end |
#jr_method ⇒ Object
Method source is invoking on the destination
19 20 21 |
# File 'lib/rjr/message.rb', line 19 def jr_method @jr_method end |
#json_message ⇒ Object
Message string received from the source
16 17 18 |
# File 'lib/rjr/message.rb', line 16 def end |
#msg_id ⇒ Object
ID of the message in accordance w/ json-rpc specification
25 26 27 |
# File 'lib/rjr/message.rb', line 25 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
75 76 77 78 79 80 81 82 83 |
# File 'lib/rjr/message.rb', line 75 def self.() begin # FIXME log error parsed = RJR.parse_json() 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
86 87 88 89 90 91 92 93 |
# File 'lib/rjr/message.rb', line 86 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 |