Class: JsonRpcObjects::Request

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

Overview

Request 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 request 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 keyword parameters in request. So requests without them 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



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
84
# File 'lib/json-rpc-objects/request.rb', line 51

def self.parse(string, default_v11 = :wd, serializer = JsonRpcObjects::default_serializer)
    data = serializer.deserialize(string)
    p data
    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/request"
        cls = V20::Request
    elsif data.include? "version"
        if (default_v11 == :alt) or (data.include? "kwparams")
            file = "json-rpc-objects/v11/alt/procedure-call"
            cls = V11::Alt::ProcedureCall
        else
            file = "json-rpc-objects/v11/wd/procedure-call"
            cls = V11::WD::ProcedureCall
        end
    else
        file = "json-rpc-objects/v10/request"
        cls = V10::Request
    end
    
    # Returns
    if not @@files.include?(file)
        require file
        @@files[file] = true
    end
    
    return cls::new(data)
end