Class: RJR::RequestMessage

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

Overview

Message sent from client to server to invoke a json-rpc method

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)

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



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
      @json_message = args[:message]
      request = RJR.parse_json(@json_message)
      @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

#headersObject

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_argsObject

Arguments source is passing to destination method



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

def jr_args
  @jr_args
end

#jr_methodObject

Method source is invoking on the destination



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

def jr_method
  @jr_method
end

#json_messageObject

Message string received from the source



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

def json_message
  @json_message
end

#msg_idObject

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.is_request_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 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