Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Extends Rails 3 to render JSON responses in the JSend format
Branch: master
Pull request Compare This branch is 17 commits behind utahstreetlabs:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.rvmrc
Gemfile
Gemfile.lock
LICENSE
README.md
Rakefile
jsend-rails.gemspec

README.md

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.

Something went wrong with that request. Please try again.