Class: JsonRpcObjects::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/json-rpc-objects/response.rb

Overview

Response class for version detection and universal API.

Since:

  • 0.2.0

Constant Summary collapse

@@files =

Holds loaded files indicator.

Since:

  • 0.2.0

{ }

Class Method Summary collapse

Class Method Details

.parse(string, default_v11 = :wd, serializer = JsonRpcObjects::default_serializer) ⇒ Object

Parses JSON-RPC string for response and uses differential heuristic for detecting the right class.

Be warn, it’s impossible to distinguish JSON-RPC 1.1 Alt and WD if there aren’t error. So responsens without it are detected as WD. Set the default 1.1 version by second argument.

Parameters:

  • string (String)

    JSON string for parse

  • default_v11 (:wd, :alt) (defaults to: :wd)

    type of the eventually returned 1.1 object

  • serializer (JsonRpcObjects::Serializer) (defaults to: JsonRpcObjects::default_serializer)

    instance of serializer class

Since:

  • 0.2.0



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/json-rpc-objects/response.rb', line 50

def self.parse(string, default_v11 = :wd, serializer = JsonRpcObjects::default_serializer)
    data = serializer.deserialize(string)
    
    if not data.kind_of? Hash
        raise Exception::new("Data in JSON string aren't object.")
    end
    
    #data.keys_to_sym!
    
    # Detects
    if data.include? "jsonrpc"
        file = "json-rpc-objects/v20/response"
        cls = V20::Response
    elsif data.include? "version"
        if (default_v11 == :wd) or ((data.include? "error") and (data["error"].kind_of? Hash) and (data["error"].include? "name"))
            file = "json-rpc-objects/v11/wd/procedure-return"
            cls = V11::WD::ProcedureReturn
        else
            file = "json-rpc-objects/v11/alt/procedure-return"
            cls = V11::Alt::ProcedureReturn
        end
    else
        file = "json-rpc-objects/v10/response"
        cls = V10::Response
    end
    
    # Returns
    if not @@files.include?(file)
        require file
        @@files[file] = true
    end
    
    return cls::new(data)
end