Class: Sinatra::JsonRpc::Response
- Inherits:
-
Object
- Object
- Sinatra::JsonRpc::Response
- Includes:
- ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/sinatra/json_rpc/response.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- ERR_CODES =
{ -32700 => 'Parse error', -32600 => 'Invalid request', -32601 => 'Method not found', -32602 => 'Invalid params', -32603 => 'Internal error' }
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#jsonrpc ⇒ Object
Returns the value of attribute jsonrpc.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
- #error ⇒ Object
- #error=(code) ⇒ Object
-
#initialize(id = nil) ⇒ Response
constructor
A new instance of Response.
- #to_json ⇒ Object
Constructor Details
#initialize(id = nil) ⇒ Response
Returns a new instance of Response.
29 30 31 |
# File 'lib/sinatra/json_rpc/response.rb', line 29 def initialize(id = nil) @jsonrpc, @id = "2.0", id end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
21 22 23 |
# File 'lib/sinatra/json_rpc/response.rb', line 21 def id @id end |
#jsonrpc ⇒ Object
Returns the value of attribute jsonrpc.
21 22 23 |
# File 'lib/sinatra/json_rpc/response.rb', line 21 def jsonrpc @jsonrpc end |
#result ⇒ Object
Returns the value of attribute result.
21 22 23 |
# File 'lib/sinatra/json_rpc/response.rb', line 21 def result @result end |
Instance Method Details
#error ⇒ Object
33 34 35 |
# File 'lib/sinatra/json_rpc/response.rb', line 33 def error @error ||= Error.new end |
#error=(code) ⇒ Object
37 38 39 40 41 |
# File 'lib/sinatra/json_rpc/response.rb', line 37 def error= (code) raise Sinatra::JsonRpc::ResponseError unless ERR_CODES.has_key? code error.code = code error. = ERR_CODES[code] end |
#to_json ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/sinatra/json_rpc/response.rb', line 43 def to_json if valid? data = { jsonrpc: @jsonrpc, result: @result, error: @error, id: @id } data.delete_if { |_,v| v.blank? } MultiJson.dump(data) end end |