class RJR::Messages::Response

Message sent from server to client in response to a JSON-RPC request

Attributes

headers[RW]

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

message[RW]

Message string received from the source

msg_id[RW]

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

result[RW]

Result encapsulated in the response message @see RJR::Result

Public Class Methods

is_response_message?(message) click to toggle source

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

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

# File lib/rjr/messages/response.rb, line 91
def self.is_response_message?(message)
  message.has?('result') || message.has?('error')
end
new(args = {}) click to toggle source

ResponseMessage initializer

@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] :id id to set in response message, should

be same as that in received message

@option args [RJR::Result] :result result of json-rpc method invocation

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

Public Instance Methods

error_json() click to toggle source
# File lib/rjr/messages/response.rb, line 99
def error_json
  {'error' => {'code'    => @result.error_code,
               'message' => @result.error_msg,
               'class'   => @result.error_class}}
end
success_json() click to toggle source
# File lib/rjr/messages/response.rb, line 95
def success_json
  {'result' => @result.result}
end
to_json(*a) click to toggle source

Convert request message to json

# File lib/rjr/messages/response.rb, line 106
def to_json(*a)
  result_json = @result.success ? success_json : error_json

  {'jsonrpc' => '2.0',
   'id'      => @msg_id}.merge(@headers).
                         merge(result_json).to_json(*a)
end
to_s() click to toggle source

Convert request to string format

# File lib/rjr/messages/response.rb, line 115
def to_s
  to_json.to_s
end

Private Instance Methods

parse_args(args) click to toggle source
# File lib/rjr/messages/response.rb, line 43
def parse_args(args)
  @msg_id  = args[:id]
  @result  = args[:result]
  @headers = args[:headers] || {}

  parse_message(args[:message]) if args.has_key?(:message)
end
parse_headers(message) click to toggle source
# File lib/rjr/messages/response.rb, line 78
def parse_headers(message)
  message.keys.select { |k|
    !['jsonrpc', 'id', 'method', 'result', 'error'].include?(k)
  }.each { |k| @headers[k] = message[k] }
end
parse_message(message) click to toggle source
# File lib/rjr/messages/response.rb, line 51
def parse_message(message)
  @message = message
  @msg_id  = message['id']

  parse_result(message)
  parse_headers(message)
end
parse_result(message) click to toggle source
# File lib/rjr/messages/response.rb, line 59
def parse_result(message)
  @result         = Result.new
  @result.success = message.has?('result')
  @result.failed  = !@result.success

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

  elsif message.has?('error')
    @result.error_code  = message['error']['code']
    @result.error_msg   = message['error']['message']

    # TODO can we safely constantize this ?
    @result.error_class = message['error']['class']
  end

  @result
end