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



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

def initialize(args = {})
  if args.has_key?(:message)
    begin
      @json_message = args[:message]
      notification = RJR.parse_json(@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



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

def headers
  @headers
end

#jr_argsObject

Arguments source is passing to destination method



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

def jr_args
  @jr_args
end

#jr_methodObject

Method source is invoking on the destination



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

def jr_method
  @jr_method
end

#json_messageObject

Message string received from the source



201
202
203
# File 'lib/rjr/message.rb', line 201

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



258
259
260
261
262
263
264
265
266
# File 'lib/rjr/message.rb', line 258

def self.is_notification_message?(message)
  begin
     # FIXME log error
     parsed = RJR.parse_json(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



269
270
271
272
273
274
275
# File 'lib/rjr/message.rb', line 269

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