Skip to content

cstrahan/capnp-ruby

Repository files navigation

Bitdeli Badge

Cap'n Proto

Ruby Edition

This here is a Ruby wrapper for the official C++ implementation of Cap'n Proto.

Build Status

Installing

First install libcapnp, then install the gem:

gem install capn_proto --pre

The native extension for this gem requires a C++ compiler with C++11 features, so use the same C++ compiler and flags that you used to compile libcapnp (e.g. CXX and CXXFLAGS). As an OSX user, having followed the instructions for installing libcapnp on OSX, the correct incantation is as follows:

CXX=$HOME/clang-3.2/bin/clang++ gem install capn_proto --pre

Example

require 'capn_proto'

module AddressBook extend CapnProto::SchemaLoader
  load_schema("addressbook.capnp")
end

def write_address_book(file)
  addresses = AddressBook::AddressBook.new_message
  people = addresses.initPeople(2)

  alice = people[0]
  alice.id = 123
  alice.name = 'Alice'
  alice.email = '[email protected]'
  alice_phones = alice.initPhones(1)
  alice_phones[0].number = "555-1212"
  alice_phones[0].type = 'mobile'
  alice.employment.school = "MIT"

  bob = people[1]
  bob.id = 456
  bob.name = 'Bob'
  bob.email = '[email protected]'
  bob_phones = bob.initPhones(2)
  bob_phones[0].number = "555-4567"
  bob_phones[0].type = 'home'
  bob_phones[1].number = "555-7654"
  bob_phones[1].type = 'work'
  bob.employment.unemployed = nil

  addresses.write(file)
end

def print_address_book(file)
  addresses = AddressBook::AddressBook.read_from(file)

  addresses.people.each do |person|
    puts "#{person.name} : #{person.email}"

    person.phones.each do |phone|
      puts "#{phone.type} : #{phone.number}"
    end

    if person.employment.unemployed?
      puts "unemployed"
    if person.employment.employer?
      puts "employer: #{person.employment.employer}"
    if person.employment.school?
      puts "student at: #{person.employment.school}"
    if person.employment.selfEmployed?
      puts "self employed"
    end
  end
end

if __FILE__ == $0
  file = File.open("addressbook.bin", "wb")
  write_address_book(file)

  file = File.open("addressbook.bin", "rb")
  print_address_book(file)
end

Status

What's implemented:

  • Schema parsing/loading
  • Message reading
    • From byte string
    • From file descriptor
  • Message writing
    • To byte string
    • To file descriptor

What's to come:

  • More reading/writing mechanisms:
    • Packing/unpacking
  • Extensive test coverage
  • Proper support for JRuby
  • Support for RPC