jsend-rails 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # jsend-rails
2
+
3
+ extensions to Rails for rendering JSON responses in the [JSend](http://labs.omniti.com/labs/jsend) format
4
+
5
+ ## Usage
6
+
7
+ In a controller:
8
+
9
+ def create
10
+ thing = Thing.new_from_json(request.body)
11
+ if thing.save
12
+ render_jsend(:success => thing)
13
+ else
14
+ render_jsend(:fail => thing.errors)
15
+ end
16
+ rescue Exception => e
17
+ render_jsend(:error => e.message, :code => 12345, :data => ['other', 'stuff])
18
+ end
19
+
20
+ Valid statuses are `:success`, `:fail` and `:error`. You can pass either the status itself (which implies an empty data
21
+ 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
22
+ data object.
23
+
24
+ ## Tests
25
+
26
+ Some handy matchers are provided for RSpec users, and `ActionController::TestResponse` is extended with a few methods:
27
+
28
+ require 'jsend-rails/test/matchers'
29
+ require 'jsend-rails/test/response'
30
+
31
+ describe Thing do
32
+ let(:thing) { Thing.create(...) }
33
+
34
+ before(:each) do
35
+ xhr :get, :show, :id => thing.id
36
+ end
37
+
38
+ it "succeeds" do
39
+ response.should be_jsend_success
40
+ end
41
+
42
+ it "returns thing id" do
43
+ response.jsend_data['id'].should == thing.id
44
+ end
45
+ end
46
+
47
+ ## Copyright
48
+
49
+ Copyright (c) 2011 Utah Street Labs. See LICENSE for details.
@@ -0,0 +1,3 @@
1
+ require 'jsend-rails/envelope'
2
+ require 'jsend-rails/controller'
3
+ require 'jsend-rails/railtie'
@@ -0,0 +1,9 @@
1
+ module JSend
2
+ module Rails
3
+ module Controller
4
+ def render_jsend(options_or_status)
5
+ render(:json => Envelope.compute(options_or_status))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ module JSend
2
+ module Rails
3
+ class Envelope
4
+ STATUS = [:error, :fail, :success]
5
+
6
+ def self.compute(options_or_status)
7
+ if options_or_status.is_a?(Hash)
8
+ if options_or_status.include?(:error)
9
+ p = {:status => :error, :message => options_or_status[:error]}
10
+ p[:code] = options_or_status[:code] if options_or_status.include?(:code)
11
+ p[:data] = options_or_status[:data] if options_or_status.include?(:data)
12
+ p
13
+ elsif options_or_status.include?(:fail)
14
+ {:status => :fail, :data => options_or_status[:fail]}
15
+ elsif options_or_status.include?(:success)
16
+ {:status => :success, :data => options_or_status[:success]}
17
+ else
18
+ raise ArgumentError.new("one of #{STATUS.join(', ')} required")
19
+ end
20
+ else
21
+ raise ArgumentError.new("one of #{STATUS.join(', ')} required") unless STATUS.include?(options_or_status)
22
+ {:status => options_or_status, :data => {}}
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module JSend
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ initializer "jsend.configure_rails_initialization" do
7
+ ActionController::Base.send :include, JSend::Rails::Controller
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ require 'rspec/core'
2
+ require 'rspec/matchers'
3
+
4
+ module JSend
5
+ module Rails
6
+ module Test
7
+ module Matchers
8
+ RSpec::Matchers.define :be_json do
9
+ match do |response|
10
+ response.content_type =~ /^application\/json/
11
+ end
12
+ failure_message_for_should do |response|
13
+ "Response should have application/json content type"
14
+ end
15
+ failure_message_for_should_not do |response|
16
+ "Response should not have application/json content type"
17
+ end
18
+ end
19
+
20
+ RSpec::Matchers.define :be_jsend_success do
21
+ match do |response|
22
+ response.json['status'].should == 'success'
23
+ end
24
+ failure_message_for_should do |response|
25
+ "Response should be JSend success"
26
+ end
27
+ failure_message_for_should_not do |response|
28
+ "Response should not be JSend success"
29
+ end
30
+ end
31
+
32
+ RSpec::Matchers.define :be_jsend_failure do
33
+ match do |response|
34
+ response.json['status'].should == 'fail'
35
+ end
36
+ failure_message_for_should do |response|
37
+ "Response should be JSend failure"
38
+ end
39
+ failure_message_for_should_not do |response|
40
+ "Response should not be JSend failure"
41
+ end
42
+ end
43
+
44
+ RSpec::Matchers.define :be_jsend_error do
45
+ match do |response|
46
+ response.json['status'].should == 'error'
47
+ end
48
+ failure_message_for_should do |response|
49
+ "Response should be JSend error"
50
+ end
51
+ failure_message_for_should_not do |response|
52
+ "Response should not be JSend error"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/json'
2
+ require 'action_controller/test_case'
3
+
4
+ module JSend
5
+ module Rails
6
+ module Test
7
+ module Response
8
+ def json
9
+ ActiveSupport::JSON.decode(body)
10
+ end
11
+
12
+ def jsend_status
13
+ json['status']
14
+ end
15
+
16
+ def jsend_data
17
+ json['data']
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ ActionController::TestResponse.send :include, JSend::Rails::Test::Response
@@ -0,0 +1,5 @@
1
+ module JSend
2
+ module Rails
3
+ VERSION = "0.9.2"
4
+ end
5
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jsend-rails
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.1
5
+ version: 0.9.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brian Moseley
@@ -44,8 +44,15 @@ extensions: []
44
44
 
45
45
  extra_rdoc_files: []
46
46
 
47
- files: []
48
-
47
+ files:
48
+ - lib/jsend-rails/controller.rb
49
+ - lib/jsend-rails/envelope.rb
50
+ - lib/jsend-rails/railtie.rb
51
+ - lib/jsend-rails/test/matchers.rb
52
+ - lib/jsend-rails/test/response.rb
53
+ - lib/jsend-rails/version.rb
54
+ - lib/jsend-rails.rb
55
+ - README.md
49
56
  has_rdoc: true
50
57
  homepage: http://github.com/utahstreetlabs/jsend-rails
51
58
  licenses: []