simple_endpoint 0.1.6 → 1.0.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/.circleci/config.yml +25 -0
- data/.rubocop.yml +15 -5
- data/CHNAGES.md +7 -0
- data/Gemfile.lock +11 -8
- data/README.md +34 -8
- data/lib/simple_endpoint.rb +17 -13
- data/lib/simple_endpoint/version.rb +1 -1
- data/simple_endpoint.gemspec +2 -1
- metadata +18 -3
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88057b74fb64a0b700c891f2f9d4fe47d1e7270558562421eb20ec6cc509609b
|
4
|
+
data.tar.gz: eb33a881d475b47de26b0e00fafe671981708447a1c51e400fed13a5fef6a789
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a5409549ff3eb0c0237838064ff9cdb9a95b9177b745573a8039e34370beead5b20a50b7baa5bb58c3bb397d581d706bf33e4ad9d28fc2c7fa6e53b052b3746
|
7
|
+
data.tar.gz: 949d6f100a2026f194b63cb1cd40284652a6a622ede5e18eb55872f5db8f570258c966c4ddfc8ebc16ff73a29f81fe2e87a62e1f879104da8ebf7270246b6c57
|
@@ -0,0 +1,25 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
build:
|
4
|
+
working_directory: ~/decouplio
|
5
|
+
docker:
|
6
|
+
- image: ruby:2.6.0
|
7
|
+
steps:
|
8
|
+
- checkout
|
9
|
+
|
10
|
+
- restore_cache:
|
11
|
+
keys:
|
12
|
+
- decouplio-{{ checksum "Gemfile.lock" }}
|
13
|
+
- decouplio-
|
14
|
+
- run:
|
15
|
+
name: Install dependencies
|
16
|
+
command: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs 4 --retry 3
|
17
|
+
|
18
|
+
- save_cache:
|
19
|
+
key: decouplio-{{ checksum "Gemfile.lock" }}
|
20
|
+
paths:
|
21
|
+
- vendor/bundle
|
22
|
+
|
23
|
+
- run:
|
24
|
+
name: Run Tests
|
25
|
+
command: bundle exec rspec
|
data/.rubocop.yml
CHANGED
@@ -1,13 +1,23 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
1
3
|
AllCops:
|
2
|
-
|
4
|
+
Exclude:
|
5
|
+
- 'simple_endpoint.gemspec'
|
6
|
+
|
7
|
+
Style/Documentation:
|
8
|
+
Enabled: false
|
3
9
|
|
4
10
|
Metrics/LineLength:
|
11
|
+
Max: 120
|
12
|
+
|
13
|
+
Style/FrozenStringLiteralComment:
|
5
14
|
Enabled: true
|
6
|
-
Max: 120
|
7
15
|
|
8
16
|
Metrics/BlockLength:
|
9
|
-
|
10
|
-
- 'spec/**'
|
17
|
+
ExcludedMethods: ['describe','context']
|
11
18
|
|
12
|
-
|
19
|
+
RSpec/DescribeClass:
|
13
20
|
Enabled: false
|
21
|
+
|
22
|
+
RSpec/NestedGroups:
|
23
|
+
Max: 4
|
data/CHNAGES.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
simple_endpoint changelog
|
2
|
+
=====================
|
3
|
+
|
4
|
+
[2020-01-25] Version 1.0.0
|
5
|
+
---------------------------
|
6
|
+
**Breaking changes**
|
7
|
+
- [PR #6](https://github.com/differencialx/simple_endpoint/pull/6): Added ability to pass additional options from controller tot handlers, such as options for serialization, to avoid setting them inside trailblazer operation.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
simple_endpoint (0.
|
4
|
+
simple_endpoint (1.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -10,10 +10,10 @@ GEM
|
|
10
10
|
byebug (10.0.2)
|
11
11
|
coderay (1.1.2)
|
12
12
|
diff-lcs (1.3)
|
13
|
-
jaro_winkler (1.5.
|
13
|
+
jaro_winkler (1.5.4)
|
14
14
|
method_source (0.9.2)
|
15
|
-
parallel (1.
|
16
|
-
parser (2.
|
15
|
+
parallel (1.19.1)
|
16
|
+
parser (2.7.0.2)
|
17
17
|
ast (~> 2.4.0)
|
18
18
|
powerpack (0.1.2)
|
19
19
|
pry (0.12.2)
|
@@ -37,7 +37,7 @@ GEM
|
|
37
37
|
diff-lcs (>= 1.2.0, < 2.0)
|
38
38
|
rspec-support (~> 3.8.0)
|
39
39
|
rspec-support (3.8.0)
|
40
|
-
rubocop (0.
|
40
|
+
rubocop (0.61.1)
|
41
41
|
jaro_winkler (~> 1.5.1)
|
42
42
|
parallel (~> 1.10)
|
43
43
|
parser (>= 2.5, != 2.5.1.1)
|
@@ -45,8 +45,10 @@ GEM
|
|
45
45
|
rainbow (>= 2.2.2, < 4.0)
|
46
46
|
ruby-progressbar (~> 1.7)
|
47
47
|
unicode-display_width (~> 1.4.0)
|
48
|
-
|
49
|
-
|
48
|
+
rubocop-rspec (1.35.0)
|
49
|
+
rubocop (>= 0.60.0)
|
50
|
+
ruby-progressbar (1.10.1)
|
51
|
+
unicode-display_width (1.4.1)
|
50
52
|
|
51
53
|
PLATFORMS
|
52
54
|
ruby
|
@@ -56,7 +58,8 @@ DEPENDENCIES
|
|
56
58
|
pry-byebug
|
57
59
|
rake (~> 10.0)
|
58
60
|
rspec (~> 3.0)
|
59
|
-
rubocop
|
61
|
+
rubocop (~> 0.61.0)
|
62
|
+
rubocop-rspec
|
60
63
|
simple_endpoint!
|
61
64
|
|
62
65
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# SimpleEndpoint
|
2
|
+
[](https://circleci.com/gh/differencialx/simple_endpoint)
|
3
|
+
[](https://rubygems.org/gems/simple_endpoint)
|
2
4
|
|
3
5
|
Dry-matcher free implementation of trailblazer endpoint.
|
4
6
|
|
@@ -6,7 +8,7 @@ Dry-matcher free implementation of trailblazer endpoint.
|
|
6
8
|
Add this to your Gemfile:
|
7
9
|
|
8
10
|
```ruby
|
9
|
-
gem 'simple_endpoint'
|
11
|
+
gem 'simple_endpoint', '~> 1.0.0'
|
10
12
|
```
|
11
13
|
|
12
14
|
## Getting Started
|
@@ -46,8 +48,8 @@ class ApplicationController < ActionController::Base
|
|
46
48
|
|
47
49
|
def default_handler
|
48
50
|
{
|
49
|
-
success: -> (result) { render json: result['model'], **
|
50
|
-
invalid: -> (result) { render json: result['contract.default'].errors, serializer: ErrorSerializer, status: :unprocessable_entity }
|
51
|
+
success: -> (result, **opts) { render json: result['model'], **opts, status: 200 },
|
52
|
+
invalid: -> (result, **) { render json: result['contract.default'].errors, serializer: ErrorSerializer, status: :unprocessable_entity }
|
51
53
|
}
|
52
54
|
end
|
53
55
|
end
|
@@ -73,6 +75,7 @@ Now you are able to use `endpoint` method at other controllers
|
|
73
75
|
| `:different_handler` | no | {} | Case of handler that should be handled in different way |
|
74
76
|
| `:options` | no | {} | Additional hash which will be merged to `#ednpoint_options` method result before operation execution |
|
75
77
|
| `:before_render` | no | {} | Allow to process code before specific case handler |
|
78
|
+
| `:renderer_options` | no | {} | Allow to pass serializer options from controller and Will available inside handler as second parameter.
|
76
79
|
|
77
80
|
|
78
81
|
#### Simple endpoint call
|
@@ -100,7 +103,7 @@ class PostsController < ApplicationController
|
|
100
103
|
{
|
101
104
|
success: -> (result) { result.success? && is_it_raining? },
|
102
105
|
invalid: -> (result) { result.failure? && is_vasya_in_the_house? }
|
103
|
-
... # other cases
|
106
|
+
... # other cases
|
104
107
|
}
|
105
108
|
end
|
106
109
|
|
@@ -155,7 +158,7 @@ class PostsController < ApplicationController
|
|
155
158
|
|
156
159
|
def default_handler
|
157
160
|
{
|
158
|
-
success: -> (result) { head :ok }
|
161
|
+
success: -> (result, **) { head :ok }
|
159
162
|
}
|
160
163
|
end
|
161
164
|
end
|
@@ -174,7 +177,7 @@ class PostsController < ApplicationController
|
|
174
177
|
|
175
178
|
def different_handler
|
176
179
|
{
|
177
|
-
success: -> (result) { render json: { message: 'Nice!' }, status: :created }
|
180
|
+
success: -> (result, **) { render json: { message: 'Nice!' }, status: :created }
|
178
181
|
}
|
179
182
|
end
|
180
183
|
end
|
@@ -230,17 +233,40 @@ class PostsController < ApplicationController
|
|
230
233
|
def create
|
231
234
|
endpoint operation: Post::Create,
|
232
235
|
before_response: before_render_actions
|
233
|
-
end
|
236
|
+
end
|
234
237
|
end
|
235
238
|
|
236
239
|
private
|
237
240
|
|
238
241
|
def before_response_actions
|
239
242
|
{
|
240
|
-
success: -> (result) { response.headers['Some-header'] = result[:some_data] }
|
243
|
+
success: -> (result, **) { response.headers['Some-header'] = result[:some_data] }
|
241
244
|
}
|
242
245
|
end
|
243
246
|
end
|
244
247
|
```
|
245
248
|
|
246
249
|
Code above will put data from operation result into response haeders before render
|
250
|
+
|
251
|
+
|
252
|
+
#### Pass additional options from controller
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
class PostsController < ApplicationController
|
256
|
+
def create
|
257
|
+
endpoint operation: Post::Create,
|
258
|
+
renderer_options: { serializer: SerializerClass }
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
private
|
263
|
+
|
264
|
+
def default_handler
|
265
|
+
{
|
266
|
+
# renderer_options will be available as **opts
|
267
|
+
success: -> (result, **opts) { render json: result['model'], **opts, status: 200 },
|
268
|
+
invalid: -> (result, **) { render json: result['contract.default'].errors, serializer: ErrorSerializer, status: :unprocessable_entity }
|
269
|
+
}
|
270
|
+
end
|
271
|
+
end
|
272
|
+
```
|
data/lib/simple_endpoint.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'simple_endpoint/version'
|
4
|
-
|
4
|
+
# rubocop:disable Metrics/ParameterLists
|
5
5
|
module SimpleEndpoint
|
6
6
|
module Controller
|
7
|
-
def endpoint(operation:,
|
7
|
+
def endpoint(operation:,
|
8
|
+
different_cases: {},
|
9
|
+
different_handler: {},
|
10
|
+
options: {},
|
11
|
+
before_response: {},
|
12
|
+
renderer_options: {})
|
8
13
|
Endpoint.call(
|
9
14
|
operation,
|
10
15
|
default_handler.merge(different_handler),
|
11
16
|
default_cases.merge(different_cases),
|
17
|
+
renderer_options,
|
12
18
|
before_response,
|
13
19
|
endpoint_options.merge(options)
|
14
20
|
)
|
@@ -30,15 +36,15 @@ module SimpleEndpoint
|
|
30
36
|
end
|
31
37
|
|
32
38
|
class Endpoint
|
33
|
-
def self.call(operation, handler, cases, before_response = {}, **args)
|
39
|
+
def self.call(operation, handler, cases, renderer_options = {}, before_response = {}, **args)
|
34
40
|
result = operation.call(**args)
|
35
|
-
new.call(result, cases, handler, before_response)
|
41
|
+
new.call(result, cases, renderer_options, handler, before_response)
|
36
42
|
end
|
37
43
|
|
38
|
-
def call(result, cases, handler = {}, before_response = {})
|
44
|
+
def call(result, cases, renderer_options = {}, handler = {}, before_response = {})
|
39
45
|
matched_case = matched_case(cases, result)
|
40
|
-
procees_handler(matched_case, before_response, result) unless before_response.empty?
|
41
|
-
procees_handler(matched_case, handler, result, UnhadledResultError)
|
46
|
+
procees_handler(matched_case, before_response, result, renderer_options) unless before_response.empty?
|
47
|
+
procees_handler(matched_case, handler, result, renderer_options, UnhadledResultError)
|
42
48
|
end
|
43
49
|
|
44
50
|
def matched_case(cases, result)
|
@@ -48,19 +54,16 @@ module SimpleEndpoint
|
|
48
54
|
matched_case
|
49
55
|
end
|
50
56
|
|
51
|
-
def procees_handler(matched_case, handler, result, exception_class = nil)
|
57
|
+
def procees_handler(matched_case, handler, result, renderer_options, exception_class = nil)
|
52
58
|
if handler.key?(matched_case)
|
53
|
-
handler.dig(matched_case)&.(result)
|
59
|
+
handler.dig(matched_case)&.(result, **renderer_options)
|
54
60
|
elsif exception_class
|
55
61
|
raise exception_class, "Key: #{matched_case} is not present at #{handler}"
|
56
62
|
end
|
57
63
|
end
|
58
64
|
|
59
65
|
def obtain_matched_case(cases, result)
|
60
|
-
|
61
|
-
return if matched_case.is_a?(Hash)
|
62
|
-
|
63
|
-
matched_case
|
66
|
+
cases.detect { |_kase, condition| condition.call(result) }&.first
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
@@ -124,3 +127,4 @@ module SimpleEndpoint
|
|
124
127
|
You can move this to separate singleton class
|
125
128
|
LARGE_ERROR
|
126
129
|
end
|
130
|
+
# rubocop:enable Metrics/ParameterLists
|
data/simple_endpoint.gemspec
CHANGED
@@ -36,5 +36,6 @@ Gem::Specification.new do |spec|
|
|
36
36
|
spec.add_development_dependency 'pry-byebug'
|
37
37
|
spec.add_development_dependency 'rake', '~> 10.0'
|
38
38
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
39
|
-
spec.add_development_dependency 'rubocop'
|
39
|
+
spec.add_development_dependency 'rubocop', '~> 0.61.0'
|
40
|
+
spec.add_development_dependency 'rubocop-rspec'
|
40
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_endpoint
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Bal
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,20 @@ dependencies:
|
|
68
68
|
version: '3.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.61.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.61.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - ">="
|
@@ -88,12 +102,13 @@ executables: []
|
|
88
102
|
extensions: []
|
89
103
|
extra_rdoc_files: []
|
90
104
|
files:
|
105
|
+
- ".circleci/config.yml"
|
91
106
|
- ".gitignore"
|
92
107
|
- ".rspec"
|
93
108
|
- ".rubocop.yml"
|
94
109
|
- ".ruby-gemset"
|
95
110
|
- ".ruby-version"
|
96
|
-
-
|
111
|
+
- CHNAGES.md
|
97
112
|
- Gemfile
|
98
113
|
- Gemfile.lock
|
99
114
|
- LICENSE
|