simple_rest 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,8 +5,10 @@
5
5
  == DESCRIPTION
6
6
 
7
7
  ActionControllers helper methods for restful response handling
8
- Just DRY respond_to do |format|; ...; end;
9
- Support for jsonp and pdf
8
+
9
+ * Just DRY respond_to do |format|; ...; end;
10
+ * Support for jsonp and pdf
11
+ * Support <b>json request</b> work around though magic parameter _json, if generic json request is imposibble (for example when using jsonp)
10
12
 
11
13
 
12
14
  == INSTALL:
@@ -19,7 +21,7 @@ then
19
21
 
20
22
  sudo rake gems:install
21
23
 
22
- To handle uncatched exceptios restfully add this to ApplicationController
24
+ To handle uncatched exceptions restfully add this to ApplicationController
23
25
 
24
26
  class ApplicationController < ApplicationController::Base
25
27
  rescue_exceptions_restfully
@@ -35,107 +37,11 @@ Example:
35
37
  simple_rest result,options
36
38
  end
37
39
 
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
40
 
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
41
+ == CHANGE LOG
103
42
 
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
43
+ * 0.0.2 add json request magic parameter support
113
44
 
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
45
+ == MORE
125
46
 
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
47
+ For more info see tests and source code :)
data/Rakefile CHANGED
@@ -27,7 +27,7 @@ PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*', 'test/**/*' ]
27
27
 
28
28
  spec = Gem::Specification.new do |s|
29
29
  s.name = "simple_rest"
30
- s.version = "0.0.1"
30
+ s.version = "0.0.2"
31
31
  s.author = "niquola,smecsia"
32
32
  s.email = "niquola@gmail.com,smecsia@gmail"
33
33
  #s.homepage = ""
@@ -2,6 +2,7 @@ module SimpleRest
2
2
  module ActionControllerMethods
3
3
  def self.included(base)
4
4
  base.extend(ClassMethods)
5
+ base.before_filter :json_request_handling_filter
5
6
  end
6
7
 
7
8
  module ClassMethods
@@ -12,6 +13,15 @@ module SimpleRest
12
13
  end
13
14
  end
14
15
 
16
+ #FIXME: add decode exception handling
17
+ def json_request_handling_filter
18
+ if params[:_json]
19
+ json_params = ActiveSupport::JSON.decode(params[:_json])
20
+ params.delete(:_json)
21
+ params.merge!(json_params)
22
+ end
23
+ end
24
+
15
25
  def simple_rest(data, opts={})
16
26
  status= opts[:status] || :ok
17
27
  serial_opts= opts[:serialize_opts] || {}
@@ -16,6 +16,10 @@ class MyController < ActionController::Base
16
16
  simple_rest({:message=>exception.to_s},{:status=>500})
17
17
  end
18
18
 
19
+ def echo
20
+ simple_rest params
21
+ end
22
+
19
23
  def index
20
24
  result = {:field=>'value'}
21
25
  simple_rest result
@@ -28,6 +32,7 @@ class MyController < ActionController::Base
28
32
  def action_with_uncatched_error
29
33
  raise Exception.new("Some error")
30
34
  end
35
+
31
36
  end
32
37
 
33
38
  class MyControllerTest < ActionController::TestCase
@@ -47,8 +52,7 @@ class MyControllerTest < ActionController::TestCase
47
52
  assert_equal('value', resp_obj['field'])
48
53
  end
49
54
 
50
- def test_jsonp_format
51
- get :index, :format => 'jsonp'
55
+ def parse_json_responce
52
56
  resp = @response.body
53
57
  assert(resp)
54
58
  resp_obj = nil
@@ -56,6 +60,12 @@ class MyControllerTest < ActionController::TestCase
56
60
  resp_obj = ActiveSupport::JSON.decode(resp)
57
61
  }
58
62
  assert_not_nil(resp_obj)
63
+ resp_obj
64
+ end
65
+
66
+ def test_jsonp_format
67
+ get :index, :format => 'jsonp'
68
+ resp_obj = parse_json_responce
59
69
  assert_not_nil(resp_obj['status'])
60
70
  assert_equal(200,resp_obj['status'])
61
71
  assert_equal('value',resp_obj['data']['field'])
@@ -73,12 +83,7 @@ class MyControllerTest < ActionController::TestCase
73
83
 
74
84
  def test_jsonp_exception
75
85
  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)
86
+ resp_obj = parse_json_responce
82
87
  assert_not_nil(resp_obj['data']['message'])
83
88
  assert_equal(500,resp_obj['status'])
84
89
  end
@@ -86,11 +91,7 @@ class MyControllerTest < ActionController::TestCase
86
91
  def test_rescue_exceptions_restfully
87
92
  get :action_with_uncatched_error, :format => 'jsonp'
88
93
  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
+ resp_obj = parse_json_responce
94
95
  assert_not_nil(resp_obj['data']['message'])
95
96
  assert_equal(500,resp_obj['status'])
96
97
  assert_response(:ok)
@@ -99,4 +100,12 @@ class MyControllerTest < ActionController::TestCase
99
100
  assert_response(500)
100
101
 
101
102
  end
103
+
104
+ def test_json_request_support
105
+ get :echo, :format => 'jsonp',:_json=>'{suboject:{field:[1,2,3]}}'
106
+ resp_obj = nil
107
+ resp_obj = parse_json_responce
108
+ assert_not_nil(resp_obj['data'])
109
+ assert_equal(2, resp_obj['data']['suboject']['field'][1])
110
+ end
102
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - niquola,smecsia