Class: JsonRpcObjects::V10::Response

Inherits:
Generic::Response show all
Defined in:
lib/json-rpc-objects/v10/response.rb

Overview

Response object class.

Constant Summary collapse

VERSION =

Holds link to its version module.

JsonRpcObjects::V10
ERROR_CLASS =

Identifies the error object class.

JsonRpcObjects::V10::Error

Instance Attribute Summary collapse

Attributes inherited from Generic::Object

#serializer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic::Object

#initialize, parse, #serialize, #to_json, version

Constructor Details

This class inherits a constructor from JsonRpcObjects::Generic::Object

Instance Attribute Details

#errorJsonRpcObjects::V10::Error

Holds error data.



51
52
53
# File 'lib/json-rpc-objects/v10/response.rb', line 51

def error
  @error
end

#idString

Call ID.

Returns:

  • (String)


59
60
61
# File 'lib/json-rpc-objects/v10/response.rb', line 59

def id
  @id
end

#resultObject

Holds result data.

Returns:

  • (Object)


43
44
45
# File 'lib/json-rpc-objects/v10/response.rb', line 43

def result
  @result
end

Class Method Details

.create(result = nil, error = nil, opts = { }) ⇒ V10::Response

Creates new one.

Parameters:

  • result (Object) (defaults to: nil)

    of the call for response

  • error (Object) (defaults to: nil)

    of the call for response

  • opts (Hash) (defaults to: { })

    additional options

Returns:



71
72
73
74
75
76
77
78
79
# File 'lib/json-rpc-objects/v10/response.rb', line 71

def self.create(result = nil, error = nil, opts = { })
    data = {
        :result => result,
        :error => error
    }
    
    data.merge! opts
    return self::new(data)
end

Instance Method Details

#<<(result) ⇒ Object

Receives result by array fill operator.

Parameters:

  • result (Object)

    for the response



121
122
123
# File 'lib/json-rpc-objects/v10/response.rb', line 121

def <<(result)
    @result = result
end

#check!Object

Checks correctness of the request data.



85
86
87
88
89
90
91
# File 'lib/json-rpc-objects/v10/response.rb', line 85

def check!
    self.normalize!
    
    __check_coherency
    __check_id
    __check_error
end

#error?Boolean

Indicates, response is error.

Returns:

  • (Boolean)

    true if it’s, false otherwise

Since:

  • 0.3.0



132
133
134
# File 'lib/json-rpc-objects/v10/response.rb', line 132

def error?
    not @error.nil?
end

#outputHash

Renders data to output hash.

Returns:

  • (Hash)

    with data of response



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/json-rpc-objects/v10/response.rb', line 98

def output
    self.check!
    
    if @error.nil?
        error = nil
    else
        error = @error.output
    end
    
    data = {
        "result" => @result,
        "error" => error,
        "id" => @id
    }
    
    return data
end