Wynn Netherland changelog.com/posts

beefcake: A sane Google Protocol Buffers library for Ruby

Needing an uber efficient means of transporting and storing data, Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.

Not satisfied with the current support for Protocol Buffers in Ruby, Sinatra creator Blake Mizerany has created Beefcake. Built to feel like Ruby, Beefcake also supports non-blocking tools like EventMachine.

To create a protocol format, just create a Ruby class that inherits from Beefcake::Message:

require 'beefcake'

class Point < Beefcake::Message
  required :x, :int32, 1
  required :y, :int32, 2
  optional :tag, :string, 3
end

point = Point.new :x => 1, :y => 2
# or
point = Point.new
point.x = 1
point.y = 2

Any Ruby type that respones to << can accept encoding:

s = ""
point.encode(s)
p [:s, s]
# or (because encode returns the string/stream)
p [:s, point.encode("")]
# or
open("point.dat") do |f|
  point.encode(f)
end

# decode
encoded = point.encode("")
decoded = Point.decode(encoded)
p [:point, decoded]

Blake is looking to add .proto file parsing, Enums, and wire types 1,3,5. Want to help out? Submit a patch.

[Source on GitHub]


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00