Class: Sinatra::JsonRpc::Response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#idObject

Returns the value of attribute id.



21
22
23
# File 'lib/sinatra/json_rpc/response.rb', line 21

def id
  @id
end

#jsonrpcObject

Returns the value of attribute jsonrpc.



21
22
23
# File 'lib/sinatra/json_rpc/response.rb', line 21

def jsonrpc
  @jsonrpc
end

#resultObject

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

#errorObject



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.message = ERR_CODES[code]
end

#to_jsonObject



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