sinatra-jsend 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 Alex Beregszaszi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ Sinatra::JSend
2
+ ==============
3
+
4
+ JSend output helper for [Sinatra](http://sinatrarb.com). Extends Sinatra to support the [JSend proprosal](http://labs.omniti.com/labs/jsend).
5
+
6
+ Depends on the Sinatra::JSON helper from Sinatra-Contrib.
7
+
8
+
9
+ Installation
10
+ ------------
11
+
12
+ System install
13
+
14
+ ```bash
15
+ gem install sinatra-jsend
16
+ ```
17
+
18
+ Gemfile
19
+
20
+ ```ruby
21
+ gem 'sinatra-jsend', :require => 'sinatra/jsend'
22
+ ```
23
+
24
+ Usage
25
+ -----
26
+
27
+ Classic:
28
+
29
+ ```ruby
30
+ require "sinatra"
31
+ require "sinatra/jsend"
32
+
33
+ get '/' do
34
+ jsend_fail({ :title => "A title is required" }) if params[:title] == nil
35
+ jsend_success({ :post => { :id => 1, :title => "First entry" } })
36
+ end
37
+ ```
38
+
39
+ Modular:
40
+
41
+ ```ruby
42
+ require "sinatra/base"
43
+ require "sinatra/jsend"
44
+
45
+ class MyApp < Sinatra::Base
46
+ helpers Sinatra::JSend
47
+
48
+ get '/' do
49
+ jsend_fail({ :title => "A title is required" }) if params[:title] == nil
50
+ jsend_success({ :post => { :id => 1, :title => "First entry" } })
51
+ end
52
+ end
53
+ ```
54
+
55
+ Links
56
+ -----
57
+
58
+ * [Sinatra](http://www.sinatrarb.com)
59
+ * [Sinatra-Contrib](http://github.com/sinatra/sinatra-contrib)
60
+ * [JSend](http://labs.omniti.com/labs/jsend)
61
+
62
+ License
63
+ -------
64
+
65
+ See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :test
6
+ task :test => :spec
@@ -0,0 +1,31 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/json'
3
+
4
+ module Sinatra
5
+ module JSend
6
+ include Sinatra::JSON
7
+
8
+ def jsend_success(data)
9
+ json ({ :status => "success", :data => data })
10
+ end
11
+ alias jsend_ok jsend_success
12
+
13
+ def jsend_fail(data)
14
+ jsend_stop(json ({ :status => "fail", :data => data }))
15
+ end
16
+
17
+ def jsend_error(message, code = nil, data = nil)
18
+ jsend_stop(json({ :status => "error", :message => message, :code => code, :data => data }.keep_if{|_,v| v != nil }))
19
+ end
20
+
21
+ private
22
+
23
+ # XXX: probably there is a better way in Sinatra?
24
+ def jsend_stop(ret)
25
+ response.body = ret
26
+ halt 200 # as far as HTTP is concerned we are successful
27
+ end
28
+ end
29
+
30
+ helpers JSend
31
+ end
@@ -0,0 +1,57 @@
1
+ require 'backports'
2
+ require_relative 'spec_helper'
3
+ require 'json'
4
+
5
+ describe Sinatra::JSend do
6
+ before do
7
+ mock_app do
8
+ helpers Sinatra::JSend
9
+ set :json_encoder, JSON
10
+
11
+ get '/success' do
12
+ jsend_success({ :post => { :id => 1 } })
13
+ end
14
+ get '/fail' do
15
+ jsend_fail({ :title => "A title is required" })
16
+ end
17
+ get '/error' do
18
+ jsend_error("We have to fail")
19
+ end
20
+ get '/combined' do
21
+ jsend_fail({ :title => "A title is required" }) if params[:title] == nil
22
+ jsend_error("We have to fail") if params[:title] != "correct"
23
+ jsend_success({ :post => { :id => 1 } })
24
+ end
25
+ end
26
+ end
27
+
28
+ it "should return success" do
29
+ get '/success'
30
+ body.should == '{"status":"success","data":{"post":{"id":1}}}'
31
+ end
32
+
33
+ it "should return failure" do
34
+ get '/fail'
35
+ body.should == '{"status":"fail","data":{"title":"A title is required"}}'
36
+ end
37
+
38
+ it "should return error" do
39
+ get '/error'
40
+ body.should == '{"status":"error","message":"We have to fail"}'
41
+ end
42
+
43
+ it "should return failure if title is not supplied" do
44
+ get '/combined'
45
+ body.should == '{"status":"fail","data":{"title":"A title is required"}}'
46
+ end
47
+
48
+ it "should return error if title is not supported" do
49
+ get '/combined?title=invalid'
50
+ body.should == '{"status":"error","message":"We have to fail"}'
51
+ end
52
+
53
+ it "should return success if title is supported" do
54
+ get '/combined?title=correct'
55
+ body.should == '{"status":"success","data":{"post":{"id":1}}}'
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ require 'sinatra/contrib'
2
+ require 'sinatra/jsend'
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec, :stdlib
6
+ config.include Sinatra::TestHelpers
7
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-jsend
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Alex Beregszaszi
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-05-15 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sinatra
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sinatra-contrib
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 3
44
+ - 0
45
+ version: 1.3.0
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 2
57
+ - 3
58
+ version: "2.3"
59
+ type: :development
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ type: :development
72
+ version_requirements: *id004
73
+ - !ruby/object:Gem::Dependency
74
+ name: json
75
+ prerelease: false
76
+ requirement: &id005 !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ type: :development
84
+ version_requirements: *id005
85
+ description: JSend helper for Sinatra
86
+ email: alex@rtfs.hu
87
+ executables: []
88
+
89
+ extensions: []
90
+
91
+ extra_rdoc_files: []
92
+
93
+ files:
94
+ - lib/sinatra/jsend.rb
95
+ - spec/jsend_spec.rb
96
+ - spec/spec_helper.rb
97
+ - README.md
98
+ - Rakefile
99
+ - LICENSE
100
+ has_rdoc: true
101
+ homepage: http://github.com/axic/sinatra-jsend
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project:
126
+ rubygems_version: 1.3.6
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: JSend helper for Sinatra
130
+ test_files: []
131
+