Class: RJR::NotificationMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/rjr/message.rb

Overview

Message sent to a jsonrpc node to invoke a rpc method but indicate the result should not be returned

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ NotificationMessage

RJR Notification 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 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)

No message id will be generated in accordance w/ the jsonrpc standard

Parameters:

  • args (Hash) (defaults to: {})

    options to set on request

Options Hash (args):

  • :message (String)

    json string received from sender

  • :headers (Hash)

    optional headers to set in request and subsequent messages

  • :method (String)

    method to invoke on server

  • :args (Array<Object>)

    to pass to server method, all must be convertable to/from json



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/rjr/message.rb', line 231

def initialize(args = {})
  if args.has_key?(:message)
    begin
      @json_message = args[:message]
      notification = JSON.parse(@json_message)
      @jr_method = notification['method']
      @jr_args   = notification['params']
      @headers   = args.has_key?(:headers) ? {}.merge!(args[:headers]) : {}

      notification.keys.select { |k|
        !['jsonrpc', 'method', 'params'].include?(k)
      }.each { |k| @headers[k] = notification[k] }

    rescue Exception => e
      #puts "Exception Parsing Notification #{e}"
      raise e
    end

  elsif args.has_key?(:method)
    @jr_method = args[:method]
    @jr_args   = args[:args]
    @headers   = args[:headers]

  end
end

Instance Attribute Details

#headersObject

Optional headers to add to json outside of standard json-rpc request



214
215
216
# File 'lib/rjr/message.rb', line 214

def headers
  @headers
end

#jr_argsObject

Arguments source is passing to destination method



211
212
213
# File 'lib/rjr/message.rb', line 211

def jr_args
  @jr_args
end

#jr_methodObject

Method source is invoking on the destination



208
209
210
# File 'lib/rjr/message.rb', line 208

def jr_method
  @jr_method
end

#json_messageObject

Message string received from the source



205
206
207
# File 'lib/rjr/message.rb', line 205

def json_message
  @json_message
end

Class Method Details

.is_notification_message?(message) ⇒ true, false

Class helper to determine if the specified string is a valid json-rpc notification

Parameters:

  • message (String)

    string message to check

Returns:

  • (true, false)

    indicating if message is a notification message



262
263
264
265
266
267
268
269
270
# File 'lib/rjr/message.rb', line 262

def self.is_notification_message?(message)
  begin
     # FIXME log error
     parsed = JSON.parse(message)
     parsed.has_key?('method') && !parsed.has_key?('id')
  rescue Exception => e
    false
  end
end

Instance Method Details

#to_sObject

Convert notification message to string json format



273
274
275
276
277
278
279
# File 'lib/rjr/message.rb', line 273

def to_s
  notification = { 'jsonrpc' => '2.0',
                   'method' => @jr_method,
                   'params' => @jr_args }
  notification.merge!(@headers) unless @headers.nil?
  notification.to_json.to_s
end