Giter Site home page Giter Site logo

msgpack-rails's Introduction

MsgpackRails

Uses the fast and compact msgpack binary format to let you serialize objects.

Installation

In your Gemfile include the following line:

gem 'msgpack-rails', :git => 'git://github.com/nzifnab/msgpack-rails.git'

Usage

To encode objects use msgpack's to_msgpack method:

@person = Person.new(:first_name => 'bob', :last_name => 'joe')
@person.to_msgpack

In your controller's render action (to be consumed as an API like ActiveResource):

respond_to do |format|
    format.mpac { render :text => @person.to_msgpack, :content_type => 'application/x-mpac' }
end

Additionally you can set up ActiveResource to accept :mpac content types:

class Animal < ActiveResource::Base
  self.format = :msgpack
end

# Meanwhile, across the desert...

Animal.find(1)   # =>  Expects the content to be in the msgpack format

Gotchas

  1. Serialization options hash

    It is currently more limiting than to_json or to_xml in that the 'options' hash does not (yet) work. If you want the equivalent of this from json:

     @person.to_json(:include => [:group], :methods => [:monkey_see])
    

    Then you have to generate it as a hash manually:

     @person.serializable_hash(:include => [:group], :methods => [:monkey_see]).to_msgpack
    

    I plan on finding a way to get this working as the other two format methods do, but for now this work-around should be fine.

  2. Date formats

    The msgpack gem (written in C) does not natively serialize date formats. Instead, I convert the dates to a string and send them that way. This is bad, because they are seen by the ActiveResource endpoint as a string and are not converted to a date. You need to keep this in mind and may have to use to_datetime on dates in those models.

TODO

  1. Fix the two gotchas mentioned above

  2. Better render support in the controller (something like render :mpac => @person.to_msgpack without the :content_type requirement)

  3. Better support and testing for rails 2.3 (maybe, but low priority)

Contributing

If you use or are interested in the msgpack gem (even by itself) and want to or already do use it in Rails (with or without this msgpack-rails gem) I would love to hear about your experiences! What did you do to get it to work for you?

If you have suggestions, ideas, or have taken a look at this gem's code and want to help improve it (fixing bugs, adding additional features, etc) then that's great! Fork the project, make a 'feature branch' with your suggested changes (preferably one major 'feature' per branch/pull request), and then do a pull request from that branch. Feel free to discuss with me ideas etc in the issues tracker here on github.

This project is licensed under the MIT-LICENSE.

msgpack-rails's People

Contributors

nzifnab avatar gotchahn avatar

Stargazers

Klaus Heissler avatar misoobu avatar Paul Blaze avatar stephen murdoch avatar  avatar Nathan Broadbent avatar uetchy avatar Chee Yeo avatar KWJL avatar Shinji Nakamatsu avatar  avatar Shumpei Kobayashi avatar Richard Lee avatar Przemysław Wróblewski avatar kawasaki tatsuya avatar Nikita avatar  avatar Leo Lou avatar Maxime Liron avatar Alex Koppel avatar Kain avatar Chad Jolly avatar

Watchers

Chad Jolly avatar Shinji Nakamatsu avatar Patrick White avatar  avatar Igor Fedoronchuk avatar Jeremy Church avatar  avatar James Cloos avatar Jesse Bethke avatar Ellie P. avatar Isaiah Baca avatar Jovanni Lo avatar Jose Trujillo avatar  avatar  avatar

msgpack-rails's Issues

self.format not available ?

Hi,

I'm on a Ruby on Rails API that use message pack for serialization.

As I want to isolate API, I've create a namespace for API, here is my config/routes.rb*

namespace :api do
    resources :tournaments, only: :create, via: :post, defaults: { format: "mpac" }
  end

The controller is very basic

class Api::TournamentsController < ApplicationController

  skip_before_filter :verify_authenticity_token

  def create
    @tournament = Tournament.new(title: params[:title])

    respond_to do |format|
      if @tournament.save
        format.json { render json: @tournament, status: :created }
        format.mpac { render text: @tournament.to_msgpack, status: :created }
      else
        format.mpac { render json: @tournament.errors, status: :unprocessable_entity }
        format.mpac { render text: @tournament.errors.full_messages.to_msgpack, status: :unprocessable_entity }
      end
    end
  end
end

Ant the model try to convert enverything into msg pack. As the documentation says, I have puts self.format = :msgpack in my model.

But in the logs, I have

NoMethodError (undefined method `format=' for #<Class:0x007f04e449d500>)

When I post data on http://localhost:3000/api/tournaments

Who to enable message pack serialization format ?

  • Ruby : 2.1.1p76
  • Rails : 4.0.2

PS : The request works fine with json

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.