Class: QRPC::Protocol::JsonRpc::Response

Inherits:
Abstract::Response show all
Defined in:
lib/qrpc/protocol/json-rpc/response.rb

Overview

JSON-RPC response implementation.

Since:

  • 0.9.0

Instance Attribute Summary collapse

Attributes inherited from Abstract::Object

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract::Response

#initialize

Methods inherited from Abstract::Object

#assign_options, #initialize

Constructor Details

This class inherits a constructor from QRPC::Protocol::Abstract::Response

Instance Attribute Details

#nativeJsonRpcObjects::Response

Returns the native object.

Returns:

  • (JsonRpcObjects::Response)

    native response object

Since:

  • 0.9.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/qrpc/protocol/json-rpc/response.rb', line 63

def native
    if @native.nil?
        result = @options.result
        error = @options.error
        request = @options.request
        
        error_native = error.nil? ? nil : error.native
        @native = request.native.class::version.response::create(result, error_native, :id => request.id)
        @native.serializer = @options.serializer
        @native.qrpc = QRPC::Protocol::JsonRpc::Native::QrpcObject::create.output
    end
    
    @native
end

Class Method Details

.parse(raw) ⇒ Response

Parses the data for new object.

Parameters:

  • raw (String)

    raw data

Returns:

  • (Response)

    new request according to data

Since:

  • 0.9.0



52
53
54
55
56
# File 'lib/qrpc/protocol/json-rpc/response.rb', line 52

def self.parse(raw)
    object = self::new
    object.native = JsonRpcObjects::Response::parse(raw, :wd, self::options.serializer)
    return object
end

Instance Method Details

#errorException

Returns response error.

Returns:

  • (Exception)

    error object

Since:

  • 0.9.0



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/qrpc/protocol/json-rpc/response.rb', line 110

def error
            
    # Converts protocol exception to exception data object
    proto = QRPC::Protocol::JsonRpc::Native::ExceptionData::new(native.error.data)

    # Tries to unmarshall
    if proto.dump.format == :ruby
        begin
            exception = Marshal.load(Base64.decode64(proto.dump.raw))
        rescue
            # pass
        end
    end
    
    # If unsuccessfull, creates from data
    if exception.nil?
        backtrace = data.backtrace.map { |i| Base64.decode64(i) }
        backtrace.reject! { |i| i.empty? }
        exception = self::new(data.name, data.message, backtrace)
    end
    
    return exception
    
end

#error?Boolean

Indicates, error state of the response.

Returns:

  • (Boolean)

    error indication

Since:

  • 0.9.0



101
102
103
# File 'lib/qrpc/protocol/json-rpc/response.rb', line 101

def error?
    self.native.error?
end

#idObject

Returns ID of the response.

Returns:

Since:

  • 0.9.0



92
93
94
# File 'lib/qrpc/protocol/json-rpc/response.rb', line 92

def id
    self.native.id
end

#resultObject

Returns response result.

Returns:

  • (Object)

    response result

Since:

  • 0.9.0



140
141
142
# File 'lib/qrpc/protocol/json-rpc/response.rb', line 140

def result
    self.native.result
end

#serializeString

Serializes object to the resultant form.

Returns:

  • (String)

    serialized form

Since:

  • 0.9.0



83
84
85
# File 'lib/qrpc/protocol/json-rpc/response.rb', line 83

def serialize
    self.native.serialize
end