Class: RJR::ResponseMessage

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

Overview

Message sent from server to client in response to json-rpc request message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ResponseMessage

ResponseMessage initializer

This should be invoked with one of two argument sets. If creating a new message to send to the client, specify :id, :result, 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)

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

  • :id (String)

    id to set in response message, should be same as that in received message

  • :result (RJR::Result)

    result of json-rpc method invocation



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rjr/message.rb', line 129

def initialize(args = {})
  if args.has_key?(:message)
    @json_message  = args[:message]
    response = JSON.parse(@json_message)
    @msg_id  = response['id']
    @result   = Result.new
    @result.success   = response.has_key?('result')
    @result.failed    = !response.has_key?('result')
    @headers   = args.has_key?(:headers) ? {}.merge!(args[:headers]) : {}

    if @result.success
      @result.result = response['result']

    elsif response.has_key?('error')
      @result.error_code = response['error']['code']
      @result.error_msg  = response['error']['message']
      @result.error_class = response['error']['class']  # TODO safely constantize this ?

    end

    response.keys.select { |k|
      !['jsonrpc', 'id', 'result', 'error'].include?(k)
    }.each { |k| @headers[k] = response[k] }

  elsif args.has_key?(:result)
    @msg_id  = args[:id]
    @result  = args[:result]
    @headers = args[:headers]

  #else
  #  raise ArgumentError, "must specify :message or :result"

  end

end

Instance Attribute Details

#headersObject

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



114
115
116
# File 'lib/rjr/message.rb', line 114

def headers
  @headers
end

#json_messageObject

Message string received from the source



104
105
106
# File 'lib/rjr/message.rb', line 104

def json_message
  @json_message
end

#msg_idObject

ID of the message in accordance w/ json-rpc specification



107
108
109
# File 'lib/rjr/message.rb', line 107

def msg_id
  @msg_id
end

#resultObject

Result encapsulated in the response message

See Also:



111
112
113
# File 'lib/rjr/message.rb', line 111

def result
  @result
end

Class Method Details

.is_response_message?(message) ⇒ true, false

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

Parameters:

  • message (String)

    string message to check

Returns:

  • (true, false)

    indicating if message is response message



169
170
171
172
173
174
175
176
177
178
# File 'lib/rjr/message.rb', line 169

def self.is_response_message?(message)
  begin
    json = JSON.parse(message)
    json.has_key?('result') || json.has_key?('error')
  rescue Exception => e
    # FIXME log error
    puts e.to_s
    false
  end
end

Instance Method Details

#to_sObject

Convert request message to string json format



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rjr/message.rb', line 181

def to_s
  s = ''
  if result.success
    s =    {'jsonrpc' => '2.0',
            'id'      => @msg_id,
            'result'  => @result.result}

  else
    s =    {'jsonrpc' => '2.0',
            'id'      => @msg_id,
            'error'   => { 'code'    => @result.error_code,
                           'message' => @result.error_msg,
                           'class'   => @result.error_class}}
  end

  s.merge! @headers unless headers.nil?
  return s.to_json.to_s
end