simple_rest 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +141 -0
- data/Rakefile +46 -0
- data/lib/simple_rest.rb +25 -0
- data/lib/simple_rest/action_controller_methods.rb +61 -0
- data/test/simple_rest_test.rb +102 -0
- data/test/test_helper.rb +12 -0
- metadata +69 -0
data/README.rdoc
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
= Simple Rest
|
2
|
+
|
3
|
+
* http://github.com/niquola/simple_rest
|
4
|
+
|
5
|
+
== DESCRIPTION
|
6
|
+
|
7
|
+
ActionControllers helper methods for restful response handling
|
8
|
+
Just DRY respond_to do |format|; ...; end;
|
9
|
+
Support for jsonp and pdf
|
10
|
+
|
11
|
+
|
12
|
+
== INSTALL:
|
13
|
+
|
14
|
+
in config/environment.rb
|
15
|
+
|
16
|
+
config.gem 'simple_rest'
|
17
|
+
|
18
|
+
then
|
19
|
+
|
20
|
+
sudo rake gems:install
|
21
|
+
|
22
|
+
To handle uncatched exceptios restfully add this to ApplicationController
|
23
|
+
|
24
|
+
class ApplicationController < ApplicationController::Base
|
25
|
+
rescue_exceptions_restfully
|
26
|
+
end
|
27
|
+
|
28
|
+
== USAGE
|
29
|
+
|
30
|
+
Example:
|
31
|
+
|
32
|
+
def action
|
33
|
+
#fetch result
|
34
|
+
options = {:status=>:ok,:serialize_opts=>some options will be passed to to_json or to_xml}
|
35
|
+
simple_rest result,options
|
36
|
+
end
|
37
|
+
|
38
|
+
See more in tests
|
39
|
+
|
40
|
+
== TEST EXAMPLES:
|
41
|
+
|
42
|
+
|
43
|
+
ActionController::Routing::Routes.draw do |map|
|
44
|
+
map.connect ':controller/:action/:id.:format'
|
45
|
+
map.connect ':controller/:action.:format'
|
46
|
+
map.connect ':controller/:action/:id'
|
47
|
+
end
|
48
|
+
|
49
|
+
class MyController < ActionController::Base
|
50
|
+
rescue_exceptions_restfully
|
51
|
+
|
52
|
+
class MyException< Exception
|
53
|
+
end
|
54
|
+
|
55
|
+
rescue_from MyException do |exception|
|
56
|
+
simple_rest({:message=>exception.to_s},{:status=>500})
|
57
|
+
end
|
58
|
+
|
59
|
+
def index
|
60
|
+
result = {:field=>'value'}
|
61
|
+
simple_rest result
|
62
|
+
end
|
63
|
+
|
64
|
+
def action_with_error
|
65
|
+
raise MyException.new("Some error")
|
66
|
+
end
|
67
|
+
|
68
|
+
def action_with_uncatched_error
|
69
|
+
raise Exception.new("Some error")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class MyControllerTest < ActionController::TestCase
|
74
|
+
def test_methods_mixed
|
75
|
+
assert(MyController.new.respond_to?(:simple_rest))
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_js_format
|
79
|
+
get :index, :format => 'js'
|
80
|
+
resp = @response.body
|
81
|
+
assert(resp)
|
82
|
+
resp_obj = nil
|
83
|
+
assert_nothing_raised(Exception) {
|
84
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
85
|
+
}
|
86
|
+
assert_not_nil(resp_obj)
|
87
|
+
assert_equal('value', resp_obj['field'])
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_jsonp_format
|
91
|
+
get :index, :format => 'jsonp'
|
92
|
+
resp = @response.body
|
93
|
+
assert(resp)
|
94
|
+
resp_obj = nil
|
95
|
+
assert_nothing_raised(Exception) {
|
96
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
97
|
+
}
|
98
|
+
assert_not_nil(resp_obj)
|
99
|
+
assert_not_nil(resp_obj['status'])
|
100
|
+
assert_equal(200,resp_obj['status'])
|
101
|
+
assert_equal('value',resp_obj['data']['field'])
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_xml_format
|
105
|
+
get :index, :format => 'xml'
|
106
|
+
resp_obj = nil
|
107
|
+
assert_nothing_raised(Exception) {
|
108
|
+
resp_obj = html_document
|
109
|
+
}
|
110
|
+
assert_not_nil(resp_obj)
|
111
|
+
#FIXME: add more asserts
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_jsonp_exception
|
115
|
+
get :action_with_error, :format => 'jsonp'
|
116
|
+
resp = @response.body
|
117
|
+
resp_obj = nil
|
118
|
+
assert_nothing_raised(Exception) {
|
119
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
120
|
+
}
|
121
|
+
assert_not_nil(resp_obj)
|
122
|
+
assert_not_nil(resp_obj['data']['message'])
|
123
|
+
assert_equal(500,resp_obj['status'])
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_rescue_exceptions_restfully
|
127
|
+
get :action_with_uncatched_error, :format => 'jsonp'
|
128
|
+
resp = @response.body
|
129
|
+
resp_obj = nil
|
130
|
+
assert_nothing_raised(Exception) {
|
131
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
132
|
+
}
|
133
|
+
assert_not_nil(resp_obj)
|
134
|
+
assert_not_nil(resp_obj['data']['message'])
|
135
|
+
assert_equal(500,resp_obj['status'])
|
136
|
+
assert_response(:ok)
|
137
|
+
|
138
|
+
get :action_with_uncatched_error, :format => 'js'
|
139
|
+
assert_response(500)
|
140
|
+
end
|
141
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Test the dojo_on_rails plugin.'
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'test'
|
13
|
+
t.test_files = FileList['test/*_test.rb']
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate rdoc'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'Simple Rest'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*', 'test/**/*' ]
|
27
|
+
|
28
|
+
spec = Gem::Specification.new do |s|
|
29
|
+
s.name = "simple_rest"
|
30
|
+
s.version = "0.0.1"
|
31
|
+
s.author = "niquola,smecsia"
|
32
|
+
s.email = "niquola@gmail.com,smecsia@gmail"
|
33
|
+
#s.homepage = ""
|
34
|
+
s.platform = Gem::Platform::RUBY
|
35
|
+
s.summary = "ActionControllers helpers for restful rails"
|
36
|
+
s.add_dependency('rails', '>= 2.3.5')
|
37
|
+
s.files = PKG_FILES.to_a
|
38
|
+
s.require_path = "lib"
|
39
|
+
s.has_rdoc = false
|
40
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'Turn this plugin into a gem.'
|
44
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
45
|
+
pkg.gem_spec = spec
|
46
|
+
end
|
data/lib/simple_rest.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
module SimpleRest
|
4
|
+
VERSION = '0.0.1'
|
5
|
+
autoload :ActionControllerMethods, 'simple_rest/action_controller_methods'
|
6
|
+
class << self
|
7
|
+
def enable
|
8
|
+
ActionController::Base.send :include, ActionControllerMethods
|
9
|
+
enable_mime_types
|
10
|
+
end
|
11
|
+
|
12
|
+
def enable_mime_types
|
13
|
+
unless defined? Mime::PDF
|
14
|
+
Mime::Type.register("application/pdf", :pdf)
|
15
|
+
end
|
16
|
+
unless defined? Mime::JSONP
|
17
|
+
Mime::Type.register("text/javascript", :jsonp)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if defined? ActionController
|
24
|
+
SimpleRest.enable if defined? ActionController
|
25
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module SimpleRest
|
2
|
+
module ActionControllerMethods
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def rescue_exceptions_restfully
|
9
|
+
rescue_from Exception do |exception|
|
10
|
+
simple_rest({:message=>exception.to_s},{:status=>500})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def simple_rest(data, opts={})
|
16
|
+
status= opts[:status] || :ok
|
17
|
+
serial_opts= opts[:serialize_opts] || {}
|
18
|
+
respond_to do |format|
|
19
|
+
format.js { render :json => data.to_json(serial_opts),:status=>status}
|
20
|
+
format.html { render}
|
21
|
+
format.yaml { render :partial=> 'shared/inspect',:content_type =>'text/html',:locals => { :data => data } ,:status=>status }
|
22
|
+
format.xml { render :xml => data.to_xml(serial_opts),:status=>status}
|
23
|
+
format.pdf { render (opts[:pdf_opts] || {}).merge(:layout=>false) }
|
24
|
+
format.jsonp { render_jsonp(data,opts) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def render_jsonp(data, options={})
|
29
|
+
text = build_jsonp_message(data,options)
|
30
|
+
render({:content_type => :js, :text => text}.merge(options.merge(:status=>:ok)))
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_jsonp_message(data,options)
|
34
|
+
message = {:status=>options[:status] || 200, :data=>data}
|
35
|
+
json=message.to_json(options)
|
36
|
+
callback, variable = params[:callback], params[:variable]
|
37
|
+
response =
|
38
|
+
begin
|
39
|
+
if callback && variable
|
40
|
+
"var #{variable} = #{json};\n#{callback}(#{variable});"
|
41
|
+
elsif variable
|
42
|
+
"var #{variable} = #{json};"
|
43
|
+
elsif callback
|
44
|
+
"#{callback}(#{json});"
|
45
|
+
else
|
46
|
+
json
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def store_location
|
52
|
+
session[:return_to] = request.request_uri
|
53
|
+
end
|
54
|
+
|
55
|
+
def redirect_back_or_default(default)
|
56
|
+
redirect_to(session[:return_to] || default)
|
57
|
+
session[:return_to] = nil
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
ActionController::Routing::Routes.draw do |map|
|
4
|
+
map.connect ':controller/:action/:id.:format'
|
5
|
+
map.connect ':controller/:action.:format'
|
6
|
+
map.connect ':controller/:action/:id'
|
7
|
+
end
|
8
|
+
|
9
|
+
class MyController < ActionController::Base
|
10
|
+
rescue_exceptions_restfully
|
11
|
+
|
12
|
+
class MyException< Exception
|
13
|
+
end
|
14
|
+
|
15
|
+
rescue_from MyException do |exception|
|
16
|
+
simple_rest({:message=>exception.to_s},{:status=>500})
|
17
|
+
end
|
18
|
+
|
19
|
+
def index
|
20
|
+
result = {:field=>'value'}
|
21
|
+
simple_rest result
|
22
|
+
end
|
23
|
+
|
24
|
+
def action_with_error
|
25
|
+
raise MyException.new("Some error")
|
26
|
+
end
|
27
|
+
|
28
|
+
def action_with_uncatched_error
|
29
|
+
raise Exception.new("Some error")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class MyControllerTest < ActionController::TestCase
|
34
|
+
def test_methods_mixed
|
35
|
+
assert(MyController.new.respond_to?(:simple_rest))
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_js_format
|
39
|
+
get :index, :format => 'js'
|
40
|
+
resp = @response.body
|
41
|
+
assert(resp)
|
42
|
+
resp_obj = nil
|
43
|
+
assert_nothing_raised(Exception) {
|
44
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
45
|
+
}
|
46
|
+
assert_not_nil(resp_obj)
|
47
|
+
assert_equal('value', resp_obj['field'])
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_jsonp_format
|
51
|
+
get :index, :format => 'jsonp'
|
52
|
+
resp = @response.body
|
53
|
+
assert(resp)
|
54
|
+
resp_obj = nil
|
55
|
+
assert_nothing_raised(Exception) {
|
56
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
57
|
+
}
|
58
|
+
assert_not_nil(resp_obj)
|
59
|
+
assert_not_nil(resp_obj['status'])
|
60
|
+
assert_equal(200,resp_obj['status'])
|
61
|
+
assert_equal('value',resp_obj['data']['field'])
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_xml_format
|
65
|
+
get :index, :format => 'xml'
|
66
|
+
resp_obj = nil
|
67
|
+
assert_nothing_raised(Exception) {
|
68
|
+
resp_obj = html_document
|
69
|
+
}
|
70
|
+
assert_not_nil(resp_obj)
|
71
|
+
#FIXME: add more asserts
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_jsonp_exception
|
75
|
+
get :action_with_error, :format => 'jsonp'
|
76
|
+
resp = @response.body
|
77
|
+
resp_obj = nil
|
78
|
+
assert_nothing_raised(Exception) {
|
79
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
80
|
+
}
|
81
|
+
assert_not_nil(resp_obj)
|
82
|
+
assert_not_nil(resp_obj['data']['message'])
|
83
|
+
assert_equal(500,resp_obj['status'])
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_rescue_exceptions_restfully
|
87
|
+
get :action_with_uncatched_error, :format => 'jsonp'
|
88
|
+
resp = @response.body
|
89
|
+
resp_obj = nil
|
90
|
+
assert_nothing_raised(Exception) {
|
91
|
+
resp_obj = ActiveSupport::JSON.decode(resp)
|
92
|
+
}
|
93
|
+
assert_not_nil(resp_obj)
|
94
|
+
assert_not_nil(resp_obj['data']['message'])
|
95
|
+
assert_equal(500,resp_obj['status'])
|
96
|
+
assert_response(:ok)
|
97
|
+
|
98
|
+
get :action_with_uncatched_error, :format => 'js'
|
99
|
+
assert_response(500)
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
def path(path)
|
2
|
+
File.join(File.dirname(__FILE__),path)
|
3
|
+
end
|
4
|
+
|
5
|
+
$:.unshift(path('../lib'))
|
6
|
+
require 'rubygems'
|
7
|
+
require "test/unit"
|
8
|
+
require 'active_support'
|
9
|
+
require 'active_support/test_case'
|
10
|
+
require 'action_controller'
|
11
|
+
require 'simple_rest'
|
12
|
+
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_rest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- niquola,smecsia
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-04 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.5
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: niquola@gmail.com,smecsia@gmail
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
files:
|
34
|
+
- Rakefile
|
35
|
+
- README.rdoc
|
36
|
+
- lib/simple_rest/action_controller_methods.rb
|
37
|
+
- lib/simple_rest.rb
|
38
|
+
- test/simple_rest_test.rb
|
39
|
+
- test/test_helper.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage:
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: ActionControllers helpers for restful rails
|
68
|
+
test_files: []
|
69
|
+
|