0.0
No commit activity in last 3 years
No release in over 3 years
Extensions to Rails for rendering JSend responses
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

>= 3.0.0
 Project Readme

jsend-rails

extensions to Rails for rendering JSON responses in the JSend format

Usage

In a controller:

def create
  thing = Thing.new_from_json(request.body)
  if thing.save
    render_jsend(:success => thing)
  else
    render_jsend(:fail => thing.errors)
  end
rescue Exception => e
  render_jsend(:error => e.message, :code => 12345, :data => ['other', 'stuff'])
end

Valid statuses are :success, :fail and :error. You can pass either the status itself (which implies an empty data object) or a hash of options; in the case of options, one of the keys must be the status, and its value is used for the data object.

Tests

Some handy matchers are provided for RSpec users, and ActionController::TestResponse is extended with a few methods:

require 'jsend-rails/test/matchers'
require 'jsend-rails/test/response'

describe Thing do
  let(:thing) { Thing.create(...) }

  before(:each) do
    xhr :get, :show, :id => thing.id
  end

  it "succeeds" do
    response.should be_jsend_success
  end

  it "returns thing id" do
    response.jsend_data['id'].should == thing.id
  end
end

Copyright

Copyright (c) 2011 Utah Street Labs. See LICENSE for details.