Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds optional msgpack serialization format to Rabl. #69

Merged
merged 1 commit into from
Sep 2, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ RABL is intended to require little to no configuration to get working. This is t
Rabl.configure do |config|
# Commented as these are the defaults
# config.json_engine = nil # Any multi\_json engines
# config.msgpack_engine = nil # Defaults to ::MessagePack
# config.include_json_root = true
# config.include_msgpack_root = true
# config.include_xml_root = false
# config.enable_json_callbacks = false
# config.xml_options = { :dasherize => true, :skip_types => false }
Expand All @@ -105,6 +107,32 @@ gem 'yajl-ruby', :require => "yajl"

and RABL will automatically start using that engine for encoding your JSON responses!

### Message Pack ###

Rabl also includes optional support for [Message Pack](http://www.msgpack.org/) serialization format using the [msgpack gem](https://rubygems.org/gems/msgpack).
To enable, include the msgpack gem in your project's Gemfile. Then use Rabl as normal with the `msgpack` format (akin to json and xml formats).

```ruby
# Gemfile
gem 'msgpack', '~> 0.4.5'
```

One can additionally use a custom Message Pack implementation by setting the Rabl `msgpack_engine` configuration attribute. This custom message pack engine must conform to the MessagePack#pack method signature.

```ruby
class CustomEncodeEngine
def self.pack string
# Custom Encoding by your own engine.
end
end

Rabl.configure do |config|
config.msgpack_engine = CustomEncodeEngine
end
```

*NOTE*: Attempting to render the msgpack format without either including the msgpack gem or setting a `msgpack_engine` will cause an exception to be raised.

## Usage ##

### Object Assignment ###
Expand Down Expand Up @@ -359,4 +387,4 @@ See the [examples](https://github.com/nesquena/rabl/tree/master/examples) direct

## Copyright

Copyright © 2011 Nathan Esquenazi. See [MIT-LICENSE](https://github.com/nesquena/rabl/blob/master/MIT-LICENSE) for details.
Copyright © 2011 Nathan Esquenazi. See [MIT-LICENSE](https://github.com/nesquena/rabl/blob/master/MIT-LICENSE) for details.
16 changes: 16 additions & 0 deletions lib/rabl/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
# We load the msgpack library if it is available.
begin
require 'msgpack'
rescue LoadError
end

module Rabl
# Rabl.host
class Configuration
attr_accessor :include_json_root
attr_accessor :include_msgpack_root
attr_accessor :include_xml_root
attr_accessor :enable_json_callbacks
attr_writer :json_engine
attr_writer :msgpack_engine
attr_writer :xml_options

DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }

def initialize
@include_json_root = true
@include_msgpack_root = true
@include_xml_root = false
@enable_json_callbacks = false
@json_engine = nil
@msgpack_engine = nil
@xml_options = {}
end

Expand All @@ -22,6 +32,12 @@ def json_engine
@json_engine || MultiJson.engine
end

##
# @return the MessagePack encoder/engine to use.
def msgpack_engine
@msgpack_engine || ::MessagePack
end

# Allows config options to be read like a hash
#
# @param [Symbol] option Key for a given attribute
Expand Down
9 changes: 9 additions & 0 deletions lib/rabl/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def to_json(options={})
format_json result
end

# Returns a msgpack representation of the data object
# to_msgpack(:root => true)
def to_msgpack(options={})
include_root = Rabl.configuration.include_msgpack_root
options = options.reverse_merge(:root => include_root, :child_root => include_root)
result = @_collection_name ? { @_collection_name => to_hash(options) } : to_hash(options)
Rabl.configuration.msgpack_engine.pack result
end

# Returns an xml representation of the data object
# to_xml(:root => true)
def to_xml(options={})
Expand Down
1 change: 1 addition & 0 deletions rabl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'tilt'
s.add_development_dependency 'bson_ext'
s.add_development_dependency 'yajl-ruby'
s.add_development_dependency 'msgpack', '~> 0.4.5'
end
Loading