Class: RJR::Messages::Notification

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

Overview

Message sent to a JSON-RPC server 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 = {}) ⇒ Notification

RJR Notification Message initializer

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



36
37
38
# File 'lib/rjr/messages/notification.rb', line 36

def initialize(args = {})
  parse_args(args)
end

Instance Attribute Details

#headersObject

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



25
26
27
# File 'lib/rjr/messages/notification.rb', line 25

def headers
  @headers
end

#jr_argsObject

Arguments source is passing to destination method



22
23
24
# File 'lib/rjr/messages/notification.rb', line 22

def jr_args
  @jr_args
end

#jr_methodObject

Method source is invoking on the destination



19
20
21
# File 'lib/rjr/messages/notification.rb', line 19

def jr_method
  @jr_method
end

#json_messageObject

Message string received from the source



16
17
18
# File 'lib/rjr/messages/notification.rb', line 16

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



72
73
74
75
76
77
78
79
80
# File 'lib/rjr/messages/notification.rb', line 72

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

Instance Method Details

#to_json(*a) ⇒ Object

Convert notification message to json



83
84
85
86
87
# File 'lib/rjr/messages/notification.rb', line 83

def to_json(*a)
  {'jsonrpc' => '2.0',
   'method'  => @jr_method,
   'params'  => @jr_args}.merge(@headers).to_json(*a)
end

#to_sObject

Convert request to string format



90
91
92
# File 'lib/rjr/messages/notification.rb', line 90

def to_s
  to_json.to_s
end