module Barrister

Barrister Module

Public Class Methods

contract_from_file(fname) click to toggle source

Reads the given filename and returns a Barrister::Contract object. The filename should be a Barrister IDL JSON file created with the ‘barrister` tool.

# 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) click to toggle source

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.

# 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) click to toggle source

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.

# 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

Public Instance Methods

err_resp(req, code, message, data=nil) click to toggle source

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`

# File lib/barrister.rb, line 95
def err_resp(req, code, message, data=nil)
  resp = { "jsonrpc"=>"2.0", "error"=> { "code"=>code, "message"=>message } }
  if req["id"]
    resp["id"] = req["id"]
  end
  if data
    resp["error"]["data"] = data
  end

  return resp
end
ok_resp(req, result) click to toggle source

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

# 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

Private Instance Methods

contract_from_file(fname) click to toggle source

Reads the given filename and returns a Barrister::Contract object. The filename should be a Barrister IDL JSON file created with the ‘barrister` tool.

# 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) click to toggle source

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.

# 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) click to toggle source

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.

# 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