Code Monkey home page Code Monkey logo

bert's Introduction

BERT

A BERT (Binary ERlang Term) serialization library for Ruby. It can encode Ruby objects into BERT format and decode BERT binaries into Ruby objects.

See the BERT specification at bert-rpc.org.

Instances of the following Ruby classes will be automatically converted to the proper simple BERT type:

  • Fixnum
  • Float
  • Symbol
  • Array
  • String

Instances of the following Ruby classes will be automatically converted to the proper complex BERT type:

  • NilClass
  • TrueClass
  • FalseClass
  • Hash
  • Time
  • Regexp

To designate tuples, simply prefix an Array literal with a t or use the BERT::Tuple class:

t[:foo, [1, 2, 3]]
BERT::Tuple[:foo, [1, 2, 3]]

Both of these will be converted to (in Erlang syntax):

{foo, [1, 2, 3]}

Installation

gem install bert -s http://gemcutter.org

Usage

require 'bert'

bert = BERT.encode(t[:user, {:name => 'TPW', :nick => 'mojombo'}])
# => "\203h\002d\000\004userh\003d\000\004bertd\000\004dictl\000\000\
      000\002h\002d\000\004namem\000\000\000\003TPWh\002d\000\004nickm\
      000\000\000\amojomboj"

BERT.decode(bert)
# => t[:user, {:name=>"TPW", :nick=>"mojombo"}]

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2009 Tom Preston-Werner. See LICENSE for details.

bert's People

Contributors

dgrijalva avatar fd avatar juls avatar mojombo avatar pjhyett avatar schmurfy avatar tmm1 avatar vmg avatar yarsanukayev 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar

bert's Issues

Error on installing on Windows

When I ran the following command

gem install bert

it gave the following error:

Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing bert:
        ERROR: Failed to build gem native extension.

    C:/Ruby193/bin/ruby.exe extconf.rb
creating Makefile

make  clean

make
generating decode-i386-mingw32.def
compiling decode.c
decode.c:3:24: fatal error: netinet/in.h: No such file or directory
compilation terminated.
make: *** [decode.o] Error 1

make failed, exit code 2

Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/bert-1.1.6 for inspection.
Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/extensions/x86-mingw32/1.9.1/bert-1.1.6/gem_make.out

Supporting serialization of arbitrary objects

I've implemented a super-simple (just one line of code) yet significant improvement in my fork:

http://github.com/bendiken/bert/commit/bc48200fda886f560d8611559f0233a280bb8faa

This makes BERT.encode rather more useful by allowing it to serialize any Ruby object as long as that object responds to the #to_bert method, which should return the BERT representation of the object.

This is a feature we very much need for RDF::BERT. With this patch, it now becomes possible to do things like the following:

require 'rdf'
require 'bert'

class RDF::Literal
  def to_bert
    BERT::Tuple[:literal, value.to_s, {:language => language.to_sym}]
  end
end

p BERT.encode(RDF::Literal("Hello!", :language => :en))

Much appreciated if this could be merged, it's a pretty trivial and non-invasive change but opens the door to new use cases such as the above.

ArgumentError with BERT::Decode.new

Running into a bit of a perplexing problem with version 1.1.2:

>> BERT::Decode.new(StringIO.new("\203h\002d\000\005replyl\000\000\000\001a\001j"))
ArgumentError: wrong number of arguments (1 for 0)
    from (irb):2:in `initialize'
    from (irb):2:in `new'

I don't immediately see how that error's even possible given the code for BERT::Decode#initialize. Maybe there's a redefinition of the new class method going on elsewhere - didn't look.

The following works as expected:

>> BERT::Decode.decode("\203h\002d\000\005replyl\000\000\000\001a\001j")
=> t[:reply, [1]]

However, I'd prefer to be able to use BERT::Decode.new directly with an IO object, to avoid the StringIO creation overhead inside BERT::Decode.decode; important for decoding larger messages that can be read block by block, on demand, instead of copied around in full in memory a couple of times over.

Libraries shouldn't add methods in the global namespace (namely 't')

I encounter this a lot within the ruby community, but this time it actually affects me. defining Object.t conflicts with rails' translate. t should at least be in a module, with a supported way to require bert without defining t.

I'll make a branch, but it'll just be a find/replace of t[ with BERT::Tuple[, so not worth a pull request.

C Decoder doesn't behave the same as Ruby version

Using the pure ruby decoder you can:

BERT::Decode.new(socket)

however using the C decoder I get:

BERT::Decode.new(socket)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3:in initialize' from (irb):3:innew'
from (irb):3

TypeError: Invalid magic value for BERT string

I'm trying to decode an erlang term but I'm getting this:

[3] pry(main)> BERT.decode('[{"x-max-hops",1},{"x-internal-purpose","federation"}]')
TypeError: Invalid magic value for BERT string
from /usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/bert-1.1.6/lib/bert/decoder.rb:8:in `decode'

Any ideas? - perhaps I'm not using the library right? - An example in the readme would be great, heh

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.