Class: RJR::ResponseMessage
Overview
Message sent from server to client in response to json-rpc request message
Instance Attribute Summary collapse
-
#headers ⇒ Object
Optional headers to add to json outside of standard json-rpc request.
-
#json_message ⇒ Object
Message string received from the source.
-
#msg_id ⇒ Object
ID of the message in accordance w/ json-rpc specification.
-
#result ⇒ Object
Result encapsulated in the response message.
Class Method Summary collapse
-
.is_response_message?(message) ⇒ true, false
Class helper to determine if the specified string is a valid json-rpc method response.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ ResponseMessage
constructor
ResponseMessage initializer.
-
#to_s ⇒ Object
Convert request message to string json format.
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)
125 126 127 128 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 |
# File 'lib/rjr/message.rb', line 125 def initialize(args = {}) if args.has_key?(:message) = args[:message] response = RJR.parse_json() @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
#headers ⇒ Object
Optional headers to add to json outside of standard json-rpc request
110 111 112 |
# File 'lib/rjr/message.rb', line 110 def headers @headers end |
#json_message ⇒ Object
Message string received from the source
100 101 102 |
# File 'lib/rjr/message.rb', line 100 def end |
#msg_id ⇒ Object
ID of the message in accordance w/ json-rpc specification
103 104 105 |
# File 'lib/rjr/message.rb', line 103 def msg_id @msg_id end |
#result ⇒ Object
Result encapsulated in the response message
107 108 109 |
# File 'lib/rjr/message.rb', line 107 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
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rjr/message.rb', line 165 def self.() begin json = RJR.parse_json() 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_s ⇒ Object
Convert request message to string json format
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rjr/message.rb', line 177 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 |