nucleus-core 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/lib/nucleus_core/responder.rb +14 -9
- data/lib/nucleus_core/version.rb +1 -1
- data/lib/nucleus_core.rb +11 -34
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cad40c45a637495932d264dfef04ca978e3f47dba177e8acbfbd59a37e0c2217
|
4
|
+
data.tar.gz: 3389a5a8e8bb3d63c4afb7fb47ae900367295d86c7bb4ccf651773b1805ca9b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 461315b12ca674353043961e25e49498878392b89d4aef410aa1e0d16e43938087e4eb377840dd4b815d4a176e3f8f09c296be096198dcd39022b647cee4b89a
|
7
|
+
data.tar.gz: 6cbd15c2ce8e2d264ecdd849e1f723b60519c132e2b422f9fc9857f0fc9828649b712bed93bf90680d1a422e87d25e6277fb801dd457436e39667665f390b3c4
|
data/README.md
CHANGED
@@ -22,8 +22,10 @@ Below is an example using NucleusCore Core with Rails:
|
|
22
22
|
```ruby
|
23
23
|
# controllers/payments_controller.rb
|
24
24
|
class PaymentsController < ApplicationController
|
25
|
+
include NucleusCore::Responder
|
26
|
+
|
25
27
|
def create
|
26
|
-
|
28
|
+
handle_response do
|
27
29
|
policy.enforce!(:can_write?)
|
28
30
|
|
29
31
|
context, _process = HandleCheckoutWorkflow.call(invoice_params)
|
@@ -104,6 +106,7 @@ class ShoppingCartRepository < NucleusCore::Repository
|
|
104
106
|
end
|
105
107
|
end
|
106
108
|
|
109
|
+
# app/models/shopping_cart.rb
|
107
110
|
class ShoppingCart < ActiveRecord::Base
|
108
111
|
# ...
|
109
112
|
end
|
@@ -2,13 +2,22 @@ require "set"
|
|
2
2
|
|
3
3
|
module NucleusCore
|
4
4
|
module Responder
|
5
|
+
attr_reader :request_format, :response_adapter
|
6
|
+
|
7
|
+
def init_responder(request_format: nil, response_adapter: nil)
|
8
|
+
set_request_format(request_format)
|
9
|
+
set_response_adapter(response_adapter)
|
10
|
+
end
|
11
|
+
|
5
12
|
def set_request_format(request=nil)
|
6
|
-
@request_format = request&.
|
13
|
+
@request_format = request&.to_sym || :json
|
7
14
|
end
|
8
15
|
|
9
|
-
|
10
|
-
|
16
|
+
# rubocop:disable Naming/AccessorMethodName
|
17
|
+
def set_response_adapter(response_adapter)
|
18
|
+
@response_adapter = response_adapter
|
11
19
|
end
|
20
|
+
# rubocop:enable Naming/AccessorMethodName
|
12
21
|
|
13
22
|
# rubocop:disable Lint/RescueException:
|
14
23
|
def handle_response(&block)
|
@@ -70,7 +79,7 @@ module NucleusCore
|
|
70
79
|
def render_response(entity)
|
71
80
|
render_headers(entity.headers)
|
72
81
|
|
73
|
-
|
82
|
+
method_name = {
|
74
83
|
NucleusCore::JsonResponse => :render_json,
|
75
84
|
NucleusCore::XmlResponse => :render_xml,
|
76
85
|
NucleusCore::PdfResponse => :render_pdf,
|
@@ -79,7 +88,7 @@ module NucleusCore
|
|
79
88
|
NucleusCore::NoResponse => :render_nothing
|
80
89
|
}.fetch(entity.class, nil)
|
81
90
|
|
82
|
-
response_adapter&.send(
|
91
|
+
response_adapter&.send(method_name, entity)
|
83
92
|
end
|
84
93
|
|
85
94
|
def render_headers(headers={})
|
@@ -122,9 +131,5 @@ module NucleusCore
|
|
122
131
|
def exception_map
|
123
132
|
NucleusCore.configuration.exceptions_map
|
124
133
|
end
|
125
|
-
|
126
|
-
def response_adapter
|
127
|
-
NucleusCore.configuration.response_adapter
|
128
|
-
end
|
129
134
|
end
|
130
135
|
end
|
data/lib/nucleus_core/version.rb
CHANGED
data/lib/nucleus_core.rb
CHANGED
@@ -24,12 +24,15 @@ module NucleusCore
|
|
24
24
|
class BadRequest < BaseException; end
|
25
25
|
|
26
26
|
class Configuration
|
27
|
-
attr_reader :exceptions_map
|
27
|
+
attr_reader :exceptions_map
|
28
28
|
attr_accessor :logger
|
29
29
|
|
30
|
+
RESPONSE_ADAPTER_METHODS = %i[
|
31
|
+
render_json render_xml render_text render_pdf render_csv render_nothing set_header
|
32
|
+
].freeze
|
33
|
+
|
30
34
|
def initialize
|
31
35
|
@logger = nil
|
32
|
-
@response_adapter = nil
|
33
36
|
@exceptions_map = nil
|
34
37
|
end
|
35
38
|
|
@@ -37,10 +40,6 @@ module NucleusCore
|
|
37
40
|
@exceptions_map = format_exceptions(args)
|
38
41
|
end
|
39
42
|
|
40
|
-
def response_adapter=(adapter)
|
41
|
-
@response_adapter = format_adapter(adapter)
|
42
|
-
end
|
43
|
-
|
44
43
|
private
|
45
44
|
|
46
45
|
def objectify(hash)
|
@@ -51,35 +50,13 @@ module NucleusCore
|
|
51
50
|
|
52
51
|
def format_exceptions(exceptions={})
|
53
52
|
exception_defaults = ERROR_STATUSES.reduce({}) { |acc, ex| acc.merge(ex => nil) }
|
53
|
+
exceptions_map = (exceptions || exception_defaults)
|
54
|
+
.slice(*exception_defaults.keys)
|
55
|
+
.reduce({}) do |acc, (key, value)|
|
56
|
+
acc.merge(key => Array.wrap(value))
|
57
|
+
end
|
54
58
|
|
55
|
-
objectify(
|
56
|
-
(exceptions || exception_defaults)
|
57
|
-
.slice(*exception_defaults.keys)
|
58
|
-
.reduce({}) do |acc, (key, value)|
|
59
|
-
acc.merge(key => Array.wrap(value))
|
60
|
-
end
|
61
|
-
)
|
62
|
-
end
|
63
|
-
|
64
|
-
def format_adapter(adapter={})
|
65
|
-
adapter.tap do |a|
|
66
|
-
verify_adapter!(a)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
ADAPTER_METHODS = %i[
|
71
|
-
render_json render_xml render_text render_pdf render_csv render_nothing set_header
|
72
|
-
].freeze
|
73
|
-
|
74
|
-
def verify_adapter!(adapter)
|
75
|
-
current_adapter_methods = Set[*(adapter.methods - Object.methods)]
|
76
|
-
required_adapter_methods = ADAPTER_METHODS.to_set
|
77
|
-
|
78
|
-
return if current_adapter_methods == required_adapter_methods
|
79
|
-
|
80
|
-
missing = current_adapter_methods.subtract(required_adapter_methods)
|
81
|
-
|
82
|
-
raise ArgumentError, "responder.adapter must implement: #{missing}"
|
59
|
+
objectify(exceptions_map)
|
83
60
|
end
|
84
61
|
end
|
85
62
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nucleus-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dodgerogers
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -72,28 +72,28 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - '='
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 1.
|
75
|
+
version: 1.44.1
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - '='
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 1.
|
82
|
+
version: 1.44.1
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rubocop-minitest
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - '='
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0.
|
89
|
+
version: 0.27.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - '='
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0.
|
96
|
+
version: 0.27.0
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rubocop-packaging
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|