Skip to content

Commit

Permalink
Merge pull request #69 from byu/feature/msgpack
Browse files Browse the repository at this point in the history
Adds optional msgpack serialization format to Rabl.
  • Loading branch information
nesquena committed Sep 2, 2011
2 parents e7a5815 + ae108f5 commit 4462ea8
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 1 deletion.
30 changes: 29 additions & 1 deletion README.md
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| Rabl.configure do |config|
# Commented as these are the defaults # Commented as these are the defaults
# config.json_engine = nil # Any multi\_json engines # config.json_engine = nil # Any multi\_json engines
# config.msgpack_engine = nil # Defaults to ::MessagePack
# config.include_json_root = true # config.include_json_root = true
# config.include_msgpack_root = true
# config.include_xml_root = false # config.include_xml_root = false
# config.enable_json_callbacks = false # config.enable_json_callbacks = false
# config.xml_options = { :dasherize => true, :skip_types => 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! 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 ## ## Usage ##


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


## Copyright ## 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
@@ -1,19 +1,29 @@
# We load the msgpack library if it is available.
begin
require 'msgpack'
rescue LoadError
end

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


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


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


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


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

# Allows config options to be read like a hash # Allows config options to be read like a hash
# #
# @param [Symbol] option Key for a given attribute # @param [Symbol] option Key for a given attribute
Expand Down
9 changes: 9 additions & 0 deletions lib/rabl/engine.rb
Expand Up @@ -46,6 +46,15 @@ def to_json(options={})
format_json result format_json result
end 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 # Returns an xml representation of the data object
# to_xml(:root => true) # to_xml(:root => true)
def to_xml(options={}) def to_xml(options={})
Expand Down
1 change: 1 addition & 0 deletions rabl.gemspec
Expand Up @@ -27,4 +27,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'tilt' s.add_development_dependency 'tilt'
s.add_development_dependency 'bson_ext' s.add_development_dependency 'bson_ext'
s.add_development_dependency 'yajl-ruby' s.add_development_dependency 'yajl-ruby'
s.add_development_dependency 'msgpack', '~> 0.4.5'
end end

0 comments on commit 4462ea8

Please sign in to comment.