class RJR::Messages::Notification

Message sent to a JSON-RPC server to invoke a rpc method but indicate the result should not be returned

Attributes

headers[RW]

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

jr_args[RW]

Arguments source is passing to destination method

jr_method[RW]

Method source is invoking on the destination

message[RW]

Message string received from the source

Public Class Methods

is_notification_message?(message) click to toggle source

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

@param [String] message string message to check @return [true,false] indicating if message is a notification message

# File lib/rjr/messages/notification.rb, line 71
def self.is_notification_message?(message)
  message.has?('method') && !message.has?('id')
end
new(args = {}) click to toggle source

RJR Notification Message initializer

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

@param [Hash] args options to set on request @option args [String] :message json string received from sender @option args [Hash] :headers optional headers to set in request and subsequent messages @option args [String] :method method to invoke on server @option args [Array<Object>] :args to pass to server method, all must be convertable to/from json

# File lib/rjr/messages/notification.rb, line 36
def initialize(args = {})
  parse_args(args)
end

Public Instance Methods

to_json(*a) click to toggle source

Convert notification message to json

# File lib/rjr/messages/notification.rb, line 76
def to_json(*a)
  {'jsonrpc' => '2.0',
   'method'  => @jr_method,
   'params'  => @jr_args}.merge(@headers).to_json(*a)
end
to_s() click to toggle source

Convert request to string format

# File lib/rjr/messages/notification.rb, line 83
def to_s
  to_json.to_s
end

Private Instance Methods

parse_args(args) click to toggle source
# File lib/rjr/messages/notification.rb, line 42
def parse_args(args)
  @jr_method = args[:method]
  @jr_args   = args[:args]    || []
  @headers   = args[:headers] || {}

  parse_message(args[:message]) if args.has_key?(:message)
end
parse_headers(message) click to toggle source
# File lib/rjr/messages/notification.rb, line 58
def parse_headers(message)
  message.keys.select { |k|
    !['jsonrpc', 'method', 'params'].include?(k)
  }.each { |k| @headers[k] = message[k] }
end
parse_message(message) click to toggle source
# File lib/rjr/messages/notification.rb, line 50
def parse_message(message)
  @message   = message
  @jr_method = message['method']
  @jr_args   = message['params']

  parse_headers(message)
end