Giter Club home page Giter Club logo

barrister-ruby's Introduction

Barrister for Ruby

This project contains Ruby bindings for the Barrister RPC system.

Installation

Install the package:

gem install barrister

If you are writing a server, you will also need the main barrister command line tool to convert your IDL files to JSON. It's written in Python, and can be installed via:

pip install barrister

See the docs for more information on the barrister tool and IDL format.

Basic Usage

Client

require 'barrister'

# specify URL to the server endpoint
trans = Barrister::HttpTransport.new("http://localhost:7667/calc")

# automatically connects to endpoint and loads IDL JSON contract
# also creates proxy classes on client - one per interface in the IDL
client = Barrister::Client.new(trans)

# make a RPC call
#
# in this example the server exposes a "Calculator" interface 
# that contains a "add" method.
#
puts client.Calculator.add(1, 5.1)

Server

Note, there's no requirement to use Sinatra. Any web framework that provides access to the raw POST data is probably fair game.

Given this IDL:

// file: calc.idl
interface Calculator {
    add(a float, b float) float
    subtract(a float, b float) float
}

That you translate via:

barrister -t "Calculator Service" -j calc.json calc.idl

Then you could write this server:

require 'sinatra'
require 'barrister'

# Define a class that implements the functions in the interface
class Calculator

  def add(a, b)
    return a+b
  end

  def subtract(a, b)
    return a-b
  end

end

# Load the IDL JSON file and create a Server instance
contract = Barrister::contract_from_file("calc.json")
server   = Barrister::Server.new(contract)

# Bind your class to the Calculator interface
# If your IDL has multiple interfaces, you would bind
# them all to the same server instance.
server.add_handler("Calculator", Calculator.new)

# Serve it up
post '/calc' do
  request.body.rewind
  resp = server.handle_json(request.body.read)
  
  status 200
  headers "Content-Type" => "application/json"
  resp
end

Compatibility

Developed on MacOS with Ruby 1.9.3. CI tests run on Linux against Ruby 1.8.7.

Depends on the json gem.

More information

License

Distributed under the MIT license. See LICENSE file for details.

Release / Tag notes

Note to self on how to tag release

# Edit `barrister.gemspec`, bump version and date, then run:

make all
git add -u
git commit -m "bump gem v0.1.0"
git tag -a v0.1.0 -m "version 0.1.0"
git push --tags
make publish

barrister-ruby's People

Contributors

coopernurse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.