api-blocks 0.5.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api_blocks/blueprinter/association_extractor.rb +64 -57
- data/lib/api_blocks/blueprinter/join_keys.rb +22 -0
- data/lib/api_blocks/controller.rb +57 -54
- data/lib/api_blocks/doorkeeper.rb +6 -4
- data/lib/api_blocks/doorkeeper/invitations.rb +8 -4
- data/lib/api_blocks/doorkeeper/invitations/application.rb +10 -4
- data/lib/api_blocks/doorkeeper/invitations/controller.rb +99 -94
- data/lib/api_blocks/doorkeeper/invitations/migration_generator.rb +23 -17
- data/lib/api_blocks/doorkeeper/passwords.rb +9 -5
- data/lib/api_blocks/doorkeeper/passwords/application.rb +10 -4
- data/lib/api_blocks/doorkeeper/passwords/controller.rb +106 -100
- data/lib/api_blocks/doorkeeper/passwords/migration_generator.rb +23 -17
- data/lib/api_blocks/doorkeeper/passwords/user.rb +33 -29
- data/lib/api_blocks/interactor.rb +73 -71
- data/lib/api_blocks/railtie.rb +12 -10
- data/lib/api_blocks/responder.rb +78 -78
- data/lib/api_blocks/version.rb +1 -1
- metadata +24 -23
data/lib/api_blocks/railtie.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'problem_details-rails'
|
4
4
|
|
5
5
|
# ApiBlocks::Railtie implements the Rails integration for ApiBlocks.
|
6
6
|
#
|
7
7
|
# @private
|
8
8
|
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
module ApiBlocks
|
10
|
+
class Railtie < Rails::Railtie
|
11
|
+
generators do
|
12
|
+
require_relative 'doorkeeper/passwords/migration_generator'
|
13
|
+
require_relative 'doorkeeper/invitations/migration_generator'
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
initializer 'blueprinter.batch_loader_integration' do |app|
|
17
|
+
app.config.after_initialize do
|
18
|
+
next unless ApiBlocks.config.blueprinter.use_batch_loader
|
18
19
|
|
19
|
-
|
20
|
+
require_relative 'blueprinter/association_extractor'
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
data/lib/api_blocks/responder.rb
CHANGED
@@ -1,102 +1,102 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
3
|
+
require 'action_controller/responder'
|
4
|
+
require 'responders'
|
5
|
+
require 'dry/monads/result'
|
6
|
+
require 'dry/validation/result'
|
7
7
|
|
8
8
|
# ApiBlocks::Responder provides a responder with better error handling and
|
9
9
|
# `ApiBlocks::Interactor` through `Dry::Monads::Result` support.
|
10
10
|
#
|
11
|
-
|
12
|
-
|
11
|
+
module ApiBlocks
|
12
|
+
class Responder < ActionController::Responder
|
13
|
+
include Responders::HttpCacheResponder
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
15
|
+
# Override resource_errors to handle more error kinds and return a status
|
16
|
+
# code.
|
17
|
+
#
|
18
|
+
def resource_errors
|
19
|
+
case resource
|
20
|
+
when Dry::Validation::Result
|
21
|
+
[{ errors: resource.errors.to_h }, :unprocessable_entity]
|
22
|
+
when ActiveRecord::RecordInvalid
|
23
|
+
[{ errors: resource.record.errors }, :unprocessable_entity]
|
24
|
+
when ActiveModel::ValidationError
|
25
|
+
[{ errors: resource.model.errors }, :unprocessable_entity]
|
26
|
+
when String
|
27
|
+
[{ detail: resource }, :internal_server_error]
|
28
|
+
when ProblemDetails::Document
|
29
|
+
[resource.to_h, resource.status]
|
30
|
+
when StandardError
|
31
|
+
# propagate the error so it can be handled through the standard rails
|
32
|
+
# error handlers.
|
33
|
+
raise resource
|
34
|
+
else
|
35
|
+
super
|
36
|
+
end
|
35
37
|
end
|
36
|
-
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
# Display is just a shortcut to render a resource's errors with the current
|
40
|
+
# format using `problem_details` when format is set to JSON.
|
41
|
+
#
|
42
|
+
def display_errors
|
43
|
+
return super unless format == :json
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
controller.render problem: errors, status: status
|
47
|
-
end
|
45
|
+
errors, status = resource_errors
|
48
46
|
|
49
|
-
|
50
|
-
# template, if the template is not available, we verify if the resource
|
51
|
-
# responds to :to_format and display it.
|
52
|
-
#
|
53
|
-
# In addition, if the resource is a Dry::Monads::Result we unwrap it and
|
54
|
-
# assign the failure instead.
|
55
|
-
#
|
56
|
-
def to_format
|
57
|
-
if resource.is_a?(Dry::Monads::Result)
|
58
|
-
unwrap_dry_result
|
47
|
+
controller.render problem: errors, status: status
|
59
48
|
end
|
60
49
|
|
61
|
-
|
62
|
-
|
50
|
+
# All other formats follow the procedure below. First we try to render a
|
51
|
+
# template, if the template is not available, we verify if the resource
|
52
|
+
# responds to :to_format and display it.
|
53
|
+
#
|
54
|
+
# In addition, if the resource is a Dry::Monads::Result we unwrap it and
|
55
|
+
# assign the failure instead.
|
56
|
+
#
|
57
|
+
def to_format
|
58
|
+
unwrap_dry_result if resource.is_a?(Dry::Monads::Result)
|
59
|
+
|
60
|
+
super
|
61
|
+
end
|
63
62
|
|
64
|
-
|
65
|
-
|
63
|
+
def has_errors? # rubocop:disable Naming/PredicateName
|
64
|
+
return true if @failure
|
66
65
|
|
67
|
-
|
68
|
-
|
66
|
+
super
|
67
|
+
end
|
69
68
|
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
def json_resource_errors
|
70
|
+
[{ errors: resource.errors }, :unprocessable_entity]
|
71
|
+
end
|
73
72
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
73
|
+
# Override ActionController::Responder#api_behavior in order to
|
74
|
+
# provide one that matches our API documentation.
|
75
|
+
#
|
76
|
+
# The only difference so far is that on POST we do not render `status:
|
77
|
+
# :created` along with a `Location` header.
|
78
|
+
#
|
79
|
+
# Moreover, we display the resource on PUT.
|
80
|
+
#
|
81
|
+
def api_behavior
|
82
|
+
raise(MissingRenderer, format) unless has_renderer?
|
84
83
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
84
|
+
if get? || post? || put?
|
85
|
+
display resource
|
86
|
+
else
|
87
|
+
head :no_content
|
88
|
+
end
|
89
89
|
end
|
90
|
-
end
|
91
90
|
|
92
|
-
|
91
|
+
private
|
93
92
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
93
|
+
def unwrap_dry_result
|
94
|
+
# unwrap the result monad so it can be processed by
|
95
|
+
# ActionController::Responder
|
96
|
+
resource.fmap { |result| @resource = result }.or do |failure|
|
97
|
+
@resource = failure
|
98
|
+
@failure = true
|
99
|
+
end
|
100
100
|
end
|
101
101
|
end
|
102
102
|
end
|
data/lib/api_blocks/version.rb
CHANGED
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-blocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul d'Hubert
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2019-10-29 00:00:00.000000000 Z
|
@@ -16,14 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.1.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-configurable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.8'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: dry-monads
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,20 +136,6 @@ dependencies:
|
|
122
136
|
- - "~>"
|
123
137
|
- !ruby/object:Gem::Version
|
124
138
|
version: 3.0.0
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: dry-configurable
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - "~>"
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '0.8'
|
132
|
-
type: :runtime
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - "~>"
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '0.8'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: bundler
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -226,14 +226,14 @@ dependencies:
|
|
226
226
|
requirements:
|
227
227
|
- - '='
|
228
228
|
- !ruby/object:Gem::Version
|
229
|
-
version:
|
229
|
+
version: 1.6.1
|
230
230
|
type: :development
|
231
231
|
prerelease: false
|
232
232
|
version_requirements: !ruby/object:Gem::Requirement
|
233
233
|
requirements:
|
234
234
|
- - '='
|
235
235
|
- !ruby/object:Gem::Version
|
236
|
-
version:
|
236
|
+
version: 1.6.1
|
237
237
|
- !ruby/object:Gem::Dependency
|
238
238
|
name: yard
|
239
239
|
requirement: !ruby/object:Gem::Requirement
|
@@ -271,6 +271,7 @@ files:
|
|
271
271
|
- lib/api-blocks.rb
|
272
272
|
- lib/api_blocks.rb
|
273
273
|
- lib/api_blocks/blueprinter/association_extractor.rb
|
274
|
+
- lib/api_blocks/blueprinter/join_keys.rb
|
274
275
|
- lib/api_blocks/controller.rb
|
275
276
|
- lib/api_blocks/doorkeeper.rb
|
276
277
|
- lib/api_blocks/doorkeeper/invitations.rb
|
@@ -290,7 +291,7 @@ homepage: https://github.com/tymate/api-blocks
|
|
290
291
|
licenses:
|
291
292
|
- MIT
|
292
293
|
metadata: {}
|
293
|
-
post_install_message:
|
294
|
+
post_install_message:
|
294
295
|
rdoc_options: []
|
295
296
|
require_paths:
|
296
297
|
- lib
|
@@ -305,8 +306,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
305
306
|
- !ruby/object:Gem::Version
|
306
307
|
version: '0'
|
307
308
|
requirements: []
|
308
|
-
rubygems_version: 3.
|
309
|
-
signing_key:
|
309
|
+
rubygems_version: 3.1.3
|
310
|
+
signing_key:
|
310
311
|
specification_version: 4
|
311
312
|
summary: Simple and consistent rails api extensions
|
312
313
|
test_files: []
|