rocket_pants 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rocket_pants/base.rb +13 -11
- data/lib/rocket_pants/controller/format_verification.rb +16 -0
- data/lib/rocket_pants/controller/respondable.rb +41 -13
- metadata +138 -23
data/lib/rocket_pants/base.rb
CHANGED
@@ -2,13 +2,14 @@ require 'rocket_pants/exceptions'
|
|
2
2
|
|
3
3
|
module RocketPants
|
4
4
|
|
5
|
-
autoload :Caching,
|
6
|
-
autoload :ErrorHandling,
|
7
|
-
autoload :Instrumentation,
|
8
|
-
autoload :Rescuable,
|
9
|
-
autoload :Respondable,
|
10
|
-
autoload :Versioning,
|
11
|
-
autoload :
|
5
|
+
autoload :Caching, 'rocket_pants/controller/caching'
|
6
|
+
autoload :ErrorHandling, 'rocket_pants/controller/error_handling'
|
7
|
+
autoload :Instrumentation, 'rocket_pants/controller/instrumentation'
|
8
|
+
autoload :Rescuable, 'rocket_pants/controller/rescuable'
|
9
|
+
autoload :Respondable, 'rocket_pants/controller/respondable'
|
10
|
+
autoload :Versioning, 'rocket_pants/controller/versioning'
|
11
|
+
autoload :FormatVerification, 'rocket_pants/controller/format_verification'
|
12
|
+
autoload :UrlFor, 'rocket_pants/controller/url_for'
|
12
13
|
|
13
14
|
class Base < ActionController::Metal
|
14
15
|
|
@@ -21,18 +22,21 @@ module RocketPants
|
|
21
22
|
ActionController::ConditionalGet,
|
22
23
|
ActionController::RackDelegation,
|
23
24
|
ActionController::RecordIdentifier,
|
25
|
+
ActionController::HttpAuthentication::Basic::ControllerMethods,
|
26
|
+
ActionController::HttpAuthentication::Digest::ControllerMethods,
|
27
|
+
ActionController::HttpAuthentication::Token::ControllerMethods,
|
24
28
|
UrlFor,
|
25
29
|
Respondable,
|
26
30
|
Versioning,
|
27
31
|
Instrumentation,
|
28
32
|
Caching,
|
29
|
-
ActionController::MimeResponds,
|
30
33
|
# Include earliest as possible in the request.
|
31
34
|
AbstractController::Callbacks,
|
32
35
|
ActionController::Rescue,
|
33
36
|
ErrorHandling,
|
34
37
|
Rescuable
|
35
|
-
|
38
|
+
# FormatVerification # TODO: Implement Format Verification
|
39
|
+
].compact
|
36
40
|
|
37
41
|
# If possible, include the Rails controller methods in Airbrake to make it useful.
|
38
42
|
begin
|
@@ -46,8 +50,6 @@ module RocketPants
|
|
46
50
|
include mixin
|
47
51
|
end
|
48
52
|
|
49
|
-
respond_to :json
|
50
|
-
|
51
53
|
# Bug fix for rails - include compatibility.
|
52
54
|
config_accessor :protected_instance_variables
|
53
55
|
self.protected_instance_variables = %w(@assigns @performed_redirect @performed_render
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RocketPants
|
2
|
+
module FormatVerification
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_filter :ensure_has_valid_format
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def ensure_has_valid_format
|
12
|
+
head 422 unless request.format.json?
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -1,9 +1,45 @@
|
|
1
|
-
require 'will_paginate/collection'
|
2
|
-
|
3
1
|
module RocketPants
|
4
2
|
module Respondable
|
5
3
|
extend ActiveSupport::Concern
|
6
4
|
|
5
|
+
def self.pagination_type(object)
|
6
|
+
if defined?(WillPaginate::Collection) && object.is_a?(WillPaginate::Collection)
|
7
|
+
:will_paginate
|
8
|
+
elsif object.respond_to?(:num_pages) && object.respond_to?(:current_page)
|
9
|
+
:kaminari
|
10
|
+
else
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.paginated?(object)
|
16
|
+
!pagination_type(object).nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.extract_pagination(collection)
|
20
|
+
case pagination_type(collection)
|
21
|
+
when :will_paginate
|
22
|
+
{
|
23
|
+
:previous => collection.previous_page.try(:to_i),
|
24
|
+
:next => collection.next_page.try(:to_i),
|
25
|
+
:current => collection.current_page.try(:to_i),
|
26
|
+
:per_page => collection.per_page.try(:to_i),
|
27
|
+
:count => collection.total_entries.try(:to_i),
|
28
|
+
:pages => collection.total_pages.try(:to_i)
|
29
|
+
}
|
30
|
+
when :kaminari
|
31
|
+
current, total, per_page = collection.current_page, collection.num_pages, collection.limit_value
|
32
|
+
{
|
33
|
+
:current => current,
|
34
|
+
:previous => (current > 1 ? (current - 1) : nil),
|
35
|
+
:next => (current == total ? nil : (current + 1)),
|
36
|
+
:per_page => per_page,
|
37
|
+
:pages => total,
|
38
|
+
:count => collection.total_count
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
7
43
|
def self.normalise_object(object, options = {})
|
8
44
|
# Convert the object using a standard grape-like lookup chain.
|
9
45
|
if object.is_a?(Array) || object.is_a?(Set)
|
@@ -74,14 +110,7 @@ module RocketPants
|
|
74
110
|
render_json({
|
75
111
|
:response => normalise_object(collection, options),
|
76
112
|
:count => collection.length,
|
77
|
-
:pagination =>
|
78
|
-
:previous => collection.previous_page.try(:to_i),
|
79
|
-
:next => collection.next_page.try(:to_i),
|
80
|
-
:current => collection.current_page.try(:to_i),
|
81
|
-
:per_page => collection.per_page.try(:to_i),
|
82
|
-
:count => collection.total_entries.try(:to_i),
|
83
|
-
:pages => collection.total_pages.try(:to_i)
|
84
|
-
}
|
113
|
+
:pagination => Respondable.extract_pagination(collection)
|
85
114
|
}, options)
|
86
115
|
post_process_exposed_object collection, :paginated, false
|
87
116
|
end
|
@@ -89,10 +118,9 @@ module RocketPants
|
|
89
118
|
# Exposes an object to the response - Essentiall, it tells the
|
90
119
|
# controller that this is what we need to render.
|
91
120
|
def exposes(object, options = {})
|
92
|
-
|
93
|
-
when WillPaginate::Collection
|
121
|
+
if Respondable.paginated?(object)
|
94
122
|
paginated object, options
|
95
|
-
|
123
|
+
elsif object.is_a?(Array)
|
96
124
|
collection object, options
|
97
125
|
else
|
98
126
|
resource object, options
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rocket_pants
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: railties
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ~>
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '3.0'
|
33
38
|
type: :runtime
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: will_paginate
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ~>
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '3.0'
|
44
54
|
type: :runtime
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: hashie
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ~>
|
@@ -54,10 +69,15 @@ dependencies:
|
|
54
69
|
version: '1.0'
|
55
70
|
type: :runtime
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.0'
|
58
78
|
- !ruby/object:Gem::Dependency
|
59
79
|
name: api_smith
|
60
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
61
81
|
none: false
|
62
82
|
requirements:
|
63
83
|
- - ! '>='
|
@@ -65,10 +85,15 @@ dependencies:
|
|
65
85
|
version: '0'
|
66
86
|
type: :runtime
|
67
87
|
prerelease: false
|
68
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
69
94
|
- !ruby/object:Gem::Dependency
|
70
95
|
name: moneta
|
71
|
-
requirement:
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
72
97
|
none: false
|
73
98
|
requirements:
|
74
99
|
- - ! '>='
|
@@ -76,10 +101,15 @@ dependencies:
|
|
76
101
|
version: '0'
|
77
102
|
type: :runtime
|
78
103
|
prerelease: false
|
79
|
-
version_requirements:
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
80
110
|
- !ruby/object:Gem::Dependency
|
81
111
|
name: rspec
|
82
|
-
requirement:
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
83
113
|
none: false
|
84
114
|
requirements:
|
85
115
|
- - ~>
|
@@ -87,10 +117,15 @@ dependencies:
|
|
87
117
|
version: '2.4'
|
88
118
|
type: :development
|
89
119
|
prerelease: false
|
90
|
-
version_requirements:
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '2.4'
|
91
126
|
- !ruby/object:Gem::Dependency
|
92
127
|
name: rspec-rails
|
93
|
-
requirement:
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
94
129
|
none: false
|
95
130
|
requirements:
|
96
131
|
- - ~>
|
@@ -98,10 +133,15 @@ dependencies:
|
|
98
133
|
version: '2.4'
|
99
134
|
type: :development
|
100
135
|
prerelease: false
|
101
|
-
version_requirements:
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ~>
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '2.4'
|
102
142
|
- !ruby/object:Gem::Dependency
|
103
143
|
name: rr
|
104
|
-
requirement:
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
105
145
|
none: false
|
106
146
|
requirements:
|
107
147
|
- - ~>
|
@@ -109,10 +149,47 @@ dependencies:
|
|
109
149
|
version: '1.0'
|
110
150
|
type: :development
|
111
151
|
prerelease: false
|
112
|
-
version_requirements:
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ~>
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '1.0'
|
113
158
|
- !ruby/object:Gem::Dependency
|
114
159
|
name: webmock
|
115
|
-
requirement:
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ! '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
type: :development
|
167
|
+
prerelease: false
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ! '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
- !ruby/object:Gem::Dependency
|
175
|
+
name: activerecord
|
176
|
+
requirement: !ruby/object:Gem::Requirement
|
177
|
+
none: false
|
178
|
+
requirements:
|
179
|
+
- - ~>
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '3.0'
|
182
|
+
type: :development
|
183
|
+
prerelease: false
|
184
|
+
version_requirements: !ruby/object:Gem::Requirement
|
185
|
+
none: false
|
186
|
+
requirements:
|
187
|
+
- - ~>
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '3.0'
|
190
|
+
- !ruby/object:Gem::Dependency
|
191
|
+
name: sqlite3-ruby
|
192
|
+
requirement: !ruby/object:Gem::Requirement
|
116
193
|
none: false
|
117
194
|
requirements:
|
118
195
|
- - ! '>='
|
@@ -120,7 +197,44 @@ dependencies:
|
|
120
197
|
version: '0'
|
121
198
|
type: :development
|
122
199
|
prerelease: false
|
123
|
-
version_requirements:
|
200
|
+
version_requirements: !ruby/object:Gem::Requirement
|
201
|
+
none: false
|
202
|
+
requirements:
|
203
|
+
- - ! '>='
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: '0'
|
206
|
+
- !ruby/object:Gem::Dependency
|
207
|
+
name: reversible_data
|
208
|
+
requirement: !ruby/object:Gem::Requirement
|
209
|
+
none: false
|
210
|
+
requirements:
|
211
|
+
- - ! '>='
|
212
|
+
- !ruby/object:Gem::Version
|
213
|
+
version: '0'
|
214
|
+
type: :development
|
215
|
+
prerelease: false
|
216
|
+
version_requirements: !ruby/object:Gem::Requirement
|
217
|
+
none: false
|
218
|
+
requirements:
|
219
|
+
- - ! '>='
|
220
|
+
- !ruby/object:Gem::Version
|
221
|
+
version: '0'
|
222
|
+
- !ruby/object:Gem::Dependency
|
223
|
+
name: kaminari
|
224
|
+
requirement: !ruby/object:Gem::Requirement
|
225
|
+
none: false
|
226
|
+
requirements:
|
227
|
+
- - ! '>='
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: '0'
|
230
|
+
type: :development
|
231
|
+
prerelease: false
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
233
|
+
none: false
|
234
|
+
requirements:
|
235
|
+
- - ! '>='
|
236
|
+
- !ruby/object:Gem::Version
|
237
|
+
version: '0'
|
124
238
|
description: Rocket Pants adds JSON API love to Rails and ActionController, making
|
125
239
|
it simpler to build API-oriented controllers.
|
126
240
|
email:
|
@@ -135,6 +249,7 @@ files:
|
|
135
249
|
- lib/rocket_pants/client.rb
|
136
250
|
- lib/rocket_pants/controller/caching.rb
|
137
251
|
- lib/rocket_pants/controller/error_handling.rb
|
252
|
+
- lib/rocket_pants/controller/format_verification.rb
|
138
253
|
- lib/rocket_pants/controller/instrumentation.rb
|
139
254
|
- lib/rocket_pants/controller/rescuable.rb
|
140
255
|
- lib/rocket_pants/controller/respondable.rb
|
@@ -168,7 +283,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
283
|
version: 1.3.6
|
169
284
|
requirements: []
|
170
285
|
rubyforge_project:
|
171
|
-
rubygems_version: 1.8.
|
286
|
+
rubygems_version: 1.8.21
|
172
287
|
signing_key:
|
173
288
|
specification_version: 3
|
174
289
|
summary: JSON API love for Rails and ActionController
|