Module: Barrister
Overview
Barrister Module
Defined Under Namespace
Classes: BatchClient, BatchTransport, Client, Contract, Function, HttpTransport, Interface, InterfaceProxy, RpcException, RpcResponse, Server
Class Method Summary collapse
-
.contract_from_file(fname) ⇒ Object
Reads the given filename and returns a Barrister::Contract object.
-
.parse_method(method) ⇒ Object
Helper function that takes a JSON-RPC method string and tokenizes it at the period.
-
.rand_str(len) ⇒ Object
Helper function to generate IDs for requests.
Instance Method Summary collapse
-
#err_resp(req, code, message, data = nil) ⇒ Object
Helper function to create a JSON-RPC 2.0 response error hash.
-
#ok_resp(req, result) ⇒ Object
Helper function to create a JSON-RPC 2.0 response hash.
Class Method Details
.contract_from_file(fname) ⇒ Object
Reads the given filename and returns a Barrister::Contract object. The filename should be a Barrister IDL JSON file created with the ‘barrister` tool.
31 32 33 34 35 36 37 |
# File 'lib/barrister.rb', line 31 def contract_from_file(fname) file = File.open(fname, "r") contents = file.read file.close idl = JSON::parse(contents) return Contract.new(idl) end |
.parse_method(method) ⇒ Object
Helper function that takes a JSON-RPC method string and tokenizes it at the period. Barrister encodes methods as “interface.function”. Returns a two element tuple: interface name, and function name.
If no period exists in the method, then we return a nil interface name, and the whole method as the function name.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/barrister.rb', line 61 def parse_method(method) pos = method.index(".") if pos == nil return nil, method else iface_name = method.slice(0, pos) func_name = method.slice(pos+1, method.length) return iface_name, func_name end end |
.rand_str(len) ⇒ Object
Helper function to generate IDs for requests. These IDs only need to be unique within a single request batch, although they may be used for other purposes in the future. This library will generate a 22 character alpha-numeric ID, which is about 130 bits of entropy.
44 45 46 47 48 49 50 51 52 |
# File 'lib/barrister.rb', line 44 def rand_str(len) rchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" s = "" len.times do || pos = rand(rchars.length) s += rchars[pos,1] end return s end |
Instance Method Details
#err_resp(req, code, message, data = nil) ⇒ Object
Helper function to create a JSON-RPC 2.0 response error hash.
-
‘req` - Request hash sent from the client
-
‘code` - Integer error code
-
‘message` - String description of the error
-
‘data` - Optional. Additional info about the error. Must be JSON serializable.
Returns a hash with the ‘error` slot set, but no `result`
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/barrister.rb', line 95 def err_resp(req, code, , data=nil) resp = { "jsonrpc"=>"2.0", "error"=> { "code"=>code, "message"=> } } if req["id"] resp["id"] = req["id"] end if data resp["error"]["data"] = data end return resp end |
#ok_resp(req, result) ⇒ Object
Helper function to create a JSON-RPC 2.0 response hash.
-
‘req` - Request hash sent from the client
-
‘result` - Result object from the handler function we called
Returns a hash with the ‘result` slot set, but no `error` slot
79 80 81 82 83 84 85 |
# File 'lib/barrister.rb', line 79 def ok_resp(req, result) resp = { "jsonrpc"=>"2.0", "result"=>result } if req["id"] resp["id"] = req["id"] end return resp end |