introspective_grape 0.2.9 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +21 -5
- data/lib/introspective_grape.rb +1 -0
- data/lib/introspective_grape/api.rb +2 -12
- data/lib/introspective_grape/snake_params.rb +16 -0
- data/lib/introspective_grape/version.rb +1 -1
- data/spec/dummy/app/api/api_helpers.rb +6 -6
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb071dc16f5b81676e9c1f8472729d13d6ac51b5
|
4
|
+
data.tar.gz: ff807faad54a81270717f30e7c46320f56147bbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcb4c96419246d601ebcc8c96ea5b1bad2ca4cdb44ec5e3fc2c89ce72a59b53778b21258160cde463c23bf7cb468fbec833cb2337c24e8b754a6530a7049ca21
|
7
|
+
data.tar.gz: fa6897a8637f5e58b2ace975324f4c186257046bb340774d11851268b0bd78cd1bc432b8b92eff4fcf6065cb74f1bec7e310085d4a33dec7731079fbc4f2327b
|
data/README.md
CHANGED
@@ -70,6 +70,18 @@ IntrospectiveGrape::API.authentication_method = "whatever!"
|
|
70
70
|
|
71
71
|
Pundit authorization is invoked against index?, show?, update?, create?, and destroy? methods with the model instance in question (or a new instance in the case of index).
|
72
72
|
|
73
|
+
The joke goes that you may find you need to allow an unauthenticated user to attempt a log in, which can be handled with something like:
|
74
|
+
|
75
|
+
```
|
76
|
+
def authorize!
|
77
|
+
unauthorized! unless current_user || login_request?
|
78
|
+
end
|
79
|
+
|
80
|
+
def login_request?
|
81
|
+
# is it the session login endpoint?
|
82
|
+
self.method_name.start_with?('POST') && self.namespace == '/login'
|
83
|
+
end
|
84
|
+
```
|
73
85
|
|
74
86
|
## Generate End Points for Models
|
75
87
|
|
@@ -152,11 +164,14 @@ class MyModelAPI < IntrospectiveGrape::API
|
|
152
164
|
end
|
153
165
|
```
|
154
166
|
|
155
|
-
Multiple values can be specified at once for Integer attributes that end in "id" (i.e.
|
167
|
+
Multiple values can be specified at once for Integer attributes that end in "id" (i.e.
|
168
|
+
conventional primary and foreign keys) by passing a comma separated list of IDs.
|
156
169
|
|
157
|
-
For timestamp attributes it will generate `<name_of_timestamp>_start` and
|
170
|
+
For timestamp attributes it will generate `<name_of_timestamp>_start` and
|
171
|
+
`<name_of_timestamp>_end` range constraints.
|
158
172
|
|
159
|
-
There is also a special "filter" filter
|
173
|
+
There is also a special "filter" filter that accepts a JSON hash of attributes and values:
|
174
|
+
this allows more complex filtering if one is familiar with ActiveRecord's query conventions.
|
160
175
|
|
161
176
|
### Overriding Filter Queries
|
162
177
|
|
@@ -199,7 +214,8 @@ end
|
|
199
214
|
|
200
215
|
## Documenting Endpoints
|
201
216
|
|
202
|
-
If you wish to provide additional documentation for end points you can define
|
217
|
+
If you wish to provide additional documentation for end points you can define
|
218
|
+
`<action>_documentation` methods in the API class.
|
203
219
|
|
204
220
|
## Pagination
|
205
221
|
|
@@ -210,7 +226,7 @@ The index action by default will not be paginated, simply declared `paginate` be
|
|
210
226
|
By default any association included in the strong params argument will have all
|
211
227
|
RESTful (`:index,:show,:create,:update, :destroy`) endpoints defined. These can
|
212
228
|
be excluded (or conversely included) with the `exclude_actions` or `include_actions`
|
213
|
-
declarations
|
229
|
+
declarations in the API class. You can also include or exclude :all or :none as shorthand.
|
214
230
|
|
215
231
|
## Grape Hooks
|
216
232
|
|
data/lib/introspective_grape.rb
CHANGED
@@ -5,6 +5,7 @@ module IntrospectiveGrape
|
|
5
5
|
autoload :Doc, 'introspective_grape/doc'
|
6
6
|
autoload :Filters, 'introspective_grape/filters'
|
7
7
|
autoload :Helpers, 'introspective_grape/helpers'
|
8
|
+
autoload :SnakeParams, 'introspective_grape/snake_params'
|
8
9
|
autoload :Traversal, 'introspective_grape/traversal'
|
9
10
|
|
10
11
|
module Formatter
|
@@ -12,6 +12,7 @@ module IntrospectiveGrape
|
|
12
12
|
extend IntrospectiveGrape::Filters
|
13
13
|
extend IntrospectiveGrape::Traversal
|
14
14
|
extend IntrospectiveGrape::Doc
|
15
|
+
extend IntrospectiveGrape::SnakeParams
|
15
16
|
|
16
17
|
# Allow files to be uploaded through ActionController:
|
17
18
|
ActionController::Parameters::PERMITTED_SCALAR_TYPES.push Rack::Multipart::UploadedFile, ActionController::Parameters
|
@@ -57,18 +58,7 @@ module IntrospectiveGrape
|
|
57
58
|
self.send(IntrospectiveGrape::API.authentication_method(self))
|
58
59
|
end
|
59
60
|
|
60
|
-
if IntrospectiveGrape.config.camelize_parameters
|
61
|
-
child.before_validation do
|
62
|
-
# We have to snake case the Rack params then re-assign @params to the
|
63
|
-
# request.params, because of the I-think-very-goofy-and-inexplicable
|
64
|
-
# way Grape interacts with both independently of each other
|
65
|
-
(params.try(:with_snake_keys)||{}).each do |k,v|
|
66
|
-
request.delete_param(k.camelize(:lower))
|
67
|
-
request.update_param(k, v)
|
68
|
-
end
|
69
|
-
@params = request.params
|
70
|
-
end
|
71
|
-
end
|
61
|
+
child.snake_params_before_validation if IntrospectiveGrape.config.camelize_parameters
|
72
62
|
end
|
73
63
|
|
74
64
|
# We will probably need before and after hooks eventually, but haven't yet...
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module IntrospectiveGrape
|
2
|
+
module SnakeParams
|
3
|
+
def snake_params_before_validation
|
4
|
+
before_validation do
|
5
|
+
# We have to snake case the Rack params then re-assign @params to the
|
6
|
+
# request.params, because of the I-think-very-goofy-and-inexplicable
|
7
|
+
# way Grape interacts with both independently of each other
|
8
|
+
(params.try(:with_snake_keys)||{}).each do |k,v|
|
9
|
+
request.delete_param(k.camelize(:lower))
|
10
|
+
request.update_param(k, v)
|
11
|
+
end
|
12
|
+
@params = request.params
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module ApiHelpers
|
2
|
-
def warden
|
3
|
-
env['warden']
|
4
|
-
end
|
5
|
-
|
6
2
|
def current_user
|
7
|
-
|
3
|
+
params[:api_key].present? && @user = User.find_by_authentication_token(params[:api_key])
|
8
4
|
end
|
9
5
|
|
10
6
|
def authenticate!
|
11
|
-
unauthenticated! unless current_user
|
7
|
+
unauthenticated! unless login_request? || current_user
|
8
|
+
end
|
9
|
+
|
10
|
+
def login_request?
|
11
|
+
self.method_name.start_with?('POST') && self.namespace == '/sessions'
|
12
12
|
end
|
13
13
|
|
14
14
|
# returns an 'unauthenticated' response
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: introspective_grape
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Buermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -264,6 +264,7 @@ files:
|
|
264
264
|
- lib/introspective_grape/filters.rb
|
265
265
|
- lib/introspective_grape/formatter/camel_json.rb
|
266
266
|
- lib/introspective_grape/helpers.rb
|
267
|
+
- lib/introspective_grape/snake_params.rb
|
267
268
|
- lib/introspective_grape/traversal.rb
|
268
269
|
- lib/introspective_grape/validators.rb
|
269
270
|
- lib/introspective_grape/version.rb
|