simple_rest 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -4
- data/lib/simple_rest/action_controller_methods.rb +4 -34
- data/lib/simple_rest/action_controller_responder_methods.rb +45 -0
- data/lib/simple_rest.rb +3 -1
- data/test/simple_rest_test.rb +23 -14
- data/test/test_helper.rb +4 -3
- metadata +9 -13
data/Rakefile
CHANGED
@@ -24,16 +24,16 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
24
24
|
end
|
25
25
|
|
26
26
|
PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*', 'test/**/*' ]
|
27
|
-
require 'lib/simple_rest.rb'
|
27
|
+
require './lib/simple_rest.rb'
|
28
28
|
spec = Gem::Specification.new do |s|
|
29
29
|
s.name = "simple_rest"
|
30
30
|
s.version = SimpleRest::VERSION
|
31
|
-
s.author = "niquola,smecsia"
|
32
|
-
s.email = "niquola@gmail.com,smecsia@gmail.com"
|
31
|
+
s.author = "niquola,smecsia,mirasrael"
|
32
|
+
s.email = "niquola@gmail.com,smecsia@gmail.com,alexander.i.bondarev@gmail.com"
|
33
33
|
#s.homepage = ""
|
34
34
|
s.platform = Gem::Platform::RUBY
|
35
35
|
s.summary = "ActionControllers helpers for restful rails"
|
36
|
-
s.add_dependency('rails', '>=
|
36
|
+
s.add_dependency('rails', '>= 3.0')
|
37
37
|
s.files = PKG_FILES.to_a
|
38
38
|
s.require_path = "lib"
|
39
39
|
s.has_rdoc = false
|
@@ -3,6 +3,7 @@ module SimpleRest
|
|
3
3
|
def self.included(base)
|
4
4
|
base.extend(ClassMethods)
|
5
5
|
base.before_filter :json_request_handling_filter
|
6
|
+
base.respond_to :js, :xml, :jsonp, :pdf, :yaml, :html
|
6
7
|
end
|
7
8
|
|
8
9
|
module ClassMethods
|
@@ -22,40 +23,9 @@ module SimpleRest
|
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
25
|
-
def simple_rest(data, opts={})
|
26
|
-
|
27
|
-
|
28
|
-
respond_to do |format|
|
29
|
-
format.js { render :json => data.to_json(serial_opts),:status=>status}
|
30
|
-
format.html { render opts}
|
31
|
-
format.yaml { render :partial=> 'shared/inspect',:content_type =>'text/html',:locals => { :data => data } ,:status=>status }
|
32
|
-
format.xml { render :xml => data.to_xml(serial_opts),:status=>status}
|
33
|
-
format.pdf { render (opts[:pdf_opts] || {}).merge(:layout=>false) }
|
34
|
-
format.jsonp { render_jsonp(data,opts) }
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def render_jsonp(data, options={})
|
39
|
-
text = build_jsonp_message(data,options)
|
40
|
-
render({:content_type => :js, :text => text}.merge(options.merge(:status=>:ok)))
|
41
|
-
end
|
42
|
-
|
43
|
-
def build_jsonp_message(data,options)
|
44
|
-
message = {:status=>options[:status] || 200, :data=>data}
|
45
|
-
json=message.to_json(options)
|
46
|
-
callback, variable = params[:callback], params[:variable]
|
47
|
-
response =
|
48
|
-
begin
|
49
|
-
if callback && variable
|
50
|
-
"var #{variable} = #{json};\n#{callback}(#{variable});"
|
51
|
-
elsif variable
|
52
|
-
"var #{variable} = #{json};"
|
53
|
-
elsif callback
|
54
|
-
"#{callback}(#{json});"
|
55
|
-
else
|
56
|
-
json
|
57
|
-
end
|
58
|
-
end
|
26
|
+
def simple_rest(data, opts = {})
|
27
|
+
::ActiveSupport::Deprecation.warn("simple_rest is deprecated. Please use respond_with instead")
|
28
|
+
respond_with data, opts
|
59
29
|
end
|
60
30
|
|
61
31
|
def store_location
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module SimpleRest
|
2
|
+
module ActionControllerResponderMethods
|
3
|
+
def to_js
|
4
|
+
status = options[:status] || :ok
|
5
|
+
render :json => resource.to_json(options[:serialize_opts] || {}), :status => status
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_yaml
|
9
|
+
status = options[:status] || :ok
|
10
|
+
render :partial=> 'shared/inspect', :content_type =>'text/html', :locals => {:data => resource}, :status=> status
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_pdf
|
14
|
+
render (options[:pdf_opts] || {}).merge(:layout=>false)
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_xml
|
18
|
+
status = options[:status] || :ok
|
19
|
+
render :xml => resource.to_xml(options[:serialize_opts] || {}), :status => status
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_jsonp
|
23
|
+
text = build_jsonp_message(resource, options)
|
24
|
+
render ({:content_type => :js, :text => text}.merge(options.merge(:status=>:ok)))
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_jsonp_message(data, options)
|
28
|
+
message = {:status=>options[:status] || 200, :data=>data}
|
29
|
+
json = message.to_json(options)
|
30
|
+
callback, variable = controller.params[:callback], controller.params[:variable]
|
31
|
+
response =
|
32
|
+
begin
|
33
|
+
if callback && variable
|
34
|
+
"var #{variable} = #{json};\n#{callback}(#{variable});"
|
35
|
+
elsif variable
|
36
|
+
"var #{variable} = #{json};"
|
37
|
+
elsif callback
|
38
|
+
"#{callback}(#{json});"
|
39
|
+
else
|
40
|
+
json
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/simple_rest.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)) unless
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
module SimpleRest
|
4
|
-
VERSION = '0.0.
|
4
|
+
VERSION = '0.0.4'
|
5
5
|
autoload :ActionControllerMethods, 'simple_rest/action_controller_methods'
|
6
|
+
autoload :ActionControllerResponderMethods, 'simple_rest/action_controller_responder_methods'
|
6
7
|
class << self
|
7
8
|
def enable
|
8
9
|
ActionController::Base.send :include, ActionControllerMethods
|
10
|
+
ActionController::Responder.send :include, ActionControllerResponderMethods
|
9
11
|
enable_mime_types
|
10
12
|
end
|
11
13
|
|
data/test/simple_rest_test.rb
CHANGED
@@ -1,10 +1,4 @@
|
|
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
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
|
8
2
|
|
9
3
|
class MyController < ActionController::Base
|
10
4
|
rescue_exceptions_restfully
|
@@ -13,7 +7,7 @@ class MyController < ActionController::Base
|
|
13
7
|
end
|
14
8
|
|
15
9
|
rescue_from MyException do |exception|
|
16
|
-
simple_rest({:message=>exception.to_s},{:status=>500})
|
10
|
+
simple_rest({:message=>exception.to_s}, {:status=>500})
|
17
11
|
end
|
18
12
|
|
19
13
|
def echo
|
@@ -32,10 +26,25 @@ class MyController < ActionController::Base
|
|
32
26
|
def action_with_uncatched_error
|
33
27
|
raise Exception.new("Some error")
|
34
28
|
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class TestApp < Rails::Application
|
35
32
|
|
36
33
|
end
|
37
34
|
|
35
|
+
TestApp.initialize!
|
36
|
+
|
38
37
|
class MyControllerTest < ActionController::TestCase
|
38
|
+
def setup
|
39
|
+
@routes = ActionDispatch::Routing::RouteSet.new
|
40
|
+
@routes.draw do
|
41
|
+
match ':controller/:action/:id.:format', :to => MyController
|
42
|
+
match ':controller/:action.:format', :to => MyController
|
43
|
+
match ':controller/:action/:id', :to => MyController
|
44
|
+
end
|
45
|
+
MyController.send :include, @routes.url_helpers
|
46
|
+
end
|
47
|
+
|
39
48
|
def test_methods_mixed
|
40
49
|
assert(MyController.new.respond_to?(:simple_rest))
|
41
50
|
end
|
@@ -67,8 +76,8 @@ class MyControllerTest < ActionController::TestCase
|
|
67
76
|
get :index, :format => 'jsonp'
|
68
77
|
resp_obj = parse_json_responce
|
69
78
|
assert_not_nil(resp_obj['status'])
|
70
|
-
assert_equal(200,resp_obj['status'])
|
71
|
-
assert_equal('value',resp_obj['data']['field'])
|
79
|
+
assert_equal(200, resp_obj['status'])
|
80
|
+
assert_equal('value', resp_obj['data']['field'])
|
72
81
|
end
|
73
82
|
|
74
83
|
def test_xml_format
|
@@ -85,15 +94,15 @@ class MyControllerTest < ActionController::TestCase
|
|
85
94
|
get :action_with_error, :format => 'jsonp'
|
86
95
|
resp_obj = parse_json_responce
|
87
96
|
assert_not_nil(resp_obj['data']['message'])
|
88
|
-
assert_equal(500,resp_obj['status'])
|
97
|
+
assert_equal(500, resp_obj['status'])
|
89
98
|
end
|
90
99
|
|
91
100
|
def test_rescue_exceptions_restfully
|
92
101
|
get :action_with_uncatched_error, :format => 'jsonp'
|
93
|
-
resp
|
102
|
+
resp = @response.body
|
94
103
|
resp_obj = parse_json_responce
|
95
104
|
assert_not_nil(resp_obj['data']['message'])
|
96
|
-
assert_equal(500,resp_obj['status'])
|
105
|
+
assert_equal(500, resp_obj['status'])
|
97
106
|
assert_response(:ok)
|
98
107
|
|
99
108
|
get :action_with_uncatched_error, :format => 'js'
|
@@ -102,7 +111,7 @@ class MyControllerTest < ActionController::TestCase
|
|
102
111
|
end
|
103
112
|
|
104
113
|
def test_json_request_support
|
105
|
-
get :echo, :format => 'jsonp'
|
114
|
+
get :echo, :format => 'jsonp', :_json=>'{suboject:{field:[1,2,3]}}'
|
106
115
|
resp_obj = nil
|
107
116
|
resp_obj = parse_json_responce
|
108
117
|
assert_not_nil(resp_obj['data'])
|
data/test/test_helper.rb
CHANGED
@@ -5,8 +5,9 @@ end
|
|
5
5
|
$:.unshift(path('../lib'))
|
6
6
|
require 'rubygems'
|
7
7
|
require "test/unit"
|
8
|
-
require '
|
9
|
-
require
|
10
|
-
require
|
8
|
+
require 'rails'
|
9
|
+
require "action_controller"
|
10
|
+
require "action_dispatch/routing"
|
11
|
+
#require 'rails/application/configuration'
|
11
12
|
require 'simple_rest'
|
12
13
|
|
metadata
CHANGED
@@ -1,21 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 25
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
|
-
- niquola,smecsia
|
12
|
+
- niquola,smecsia,mirasrael
|
14
13
|
autorequire:
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date:
|
17
|
+
date: 2011-01-26 00:00:00 +03:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,16 +25,14 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 9
|
30
28
|
segments:
|
31
|
-
- 2
|
32
29
|
- 3
|
33
|
-
-
|
34
|
-
version:
|
30
|
+
- 0
|
31
|
+
version: "3.0"
|
35
32
|
type: :runtime
|
36
33
|
version_requirements: *id001
|
37
34
|
description:
|
38
|
-
email: niquola@gmail.com,smecsia@gmail.com
|
35
|
+
email: niquola@gmail.com,smecsia@gmail.com,alexander.i.bondarev@gmail.com
|
39
36
|
executables: []
|
40
37
|
|
41
38
|
extensions: []
|
@@ -43,9 +40,10 @@ extensions: []
|
|
43
40
|
extra_rdoc_files:
|
44
41
|
- README.rdoc
|
45
42
|
files:
|
46
|
-
- Rakefile
|
47
43
|
- README.rdoc
|
44
|
+
- Rakefile
|
48
45
|
- lib/simple_rest/action_controller_methods.rb
|
46
|
+
- lib/simple_rest/action_controller_responder_methods.rb
|
49
47
|
- lib/simple_rest.rb
|
50
48
|
- test/simple_rest_test.rb
|
51
49
|
- test/test_helper.rb
|
@@ -63,7 +61,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
61
|
requirements:
|
64
62
|
- - ">="
|
65
63
|
- !ruby/object:Gem::Version
|
66
|
-
hash: 3
|
67
64
|
segments:
|
68
65
|
- 0
|
69
66
|
version: "0"
|
@@ -72,7 +69,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
69
|
requirements:
|
73
70
|
- - ">="
|
74
71
|
- !ruby/object:Gem::Version
|
75
|
-
hash: 3
|
76
72
|
segments:
|
77
73
|
- 0
|
78
74
|
version: "0"
|