simple_endpoint 0.1.3 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55a2646d762faef9f9bd692892c9dc29837d1fb1a6683d776c4822a155ea15bc
4
- data.tar.gz: 4e05da905deeed9b872a3ad816a09c0d94a3f619506342ef64f1995f24c8cfbe
3
+ metadata.gz: 95eb82b3c6bb92b61851d7082e2a5ec6675705f8e1ce5619253b2f49354a48d5
4
+ data.tar.gz: 5d5d74d08d6e637f2ccf92dd7934fc24c8492123a285b8cebdc957b1bd9ddc3d
5
5
  SHA512:
6
- metadata.gz: 7a670e1db13967ebd1d89b6abc870cb206b63ed9688f8625b067e8300b6f4c84cbfb9bc67e6767d6e37a253abd64e1ee1d053302d3e6b4d20ff8895393bd66f7
7
- data.tar.gz: 284851163ebf6ccf7168aa9787408721cbdf5a7d2a7b8a2fb712f357cc6c1d6d9843fbe1ab33d592942b689e247a99646f597a316e93c4191141390e48de7cf6
6
+ metadata.gz: 414ab0d3317fdc77d60706b0672b6bae7b85d842c46c93f9210e8c02878d7c5e03755a1b0415241d476bababc33fdbff282cbcc799c16385190c7b879ff74de5
7
+ data.tar.gz: 58315cf3181dd558da6798fc822fd2114f65eb6e9ae3d2b43db858c7901ff62177e894c876179f4f2cc7c085204b9e62d289f1748b8f2a402c03685bedea6ea0
@@ -0,0 +1,48 @@
1
+ version: 2.1
2
+
3
+ executors:
4
+ test_executor:
5
+ docker:
6
+ - image: circleci/ruby:${RUBY_VERSION}
7
+ working_directory: ~/simple_endpoint
8
+
9
+ jobs:
10
+ build:
11
+ environment:
12
+ RUBY_VERSION: << parameters.ruby_version >>
13
+ BUNDLER_VERSION: 2.1.4
14
+ executor: test_executor
15
+ parameters:
16
+ ruby_version:
17
+ type: string
18
+ steps:
19
+ - checkout
20
+
21
+ - run:
22
+ name: 'Install bundler'
23
+ command: 'gem install bundler'
24
+
25
+ - run:
26
+ name: Install dependencies
27
+ command: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs 4 --retry 3
28
+
29
+ # - run:
30
+ # name: Run Rubocop
31
+ # command: bundle exec rubocop
32
+
33
+ - run:
34
+ name: Run Tests
35
+ command: bundle exec rspec
36
+
37
+ workflows:
38
+ build_and_test:
39
+ jobs:
40
+ - build:
41
+ name: 'ruby 2.6.8'
42
+ ruby_version: 2.6.8
43
+ - build:
44
+ name: 'ruby 2.7.0'
45
+ ruby_version: 2.7.0
46
+ - build:
47
+ name: 'ruby 3.0.3'
48
+ ruby_version: 3.0.3
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ Gemfile.lock
data/.rubocop.yml CHANGED
@@ -1,13 +1,26 @@
1
+ require: rubocop-rspec
2
+
1
3
  AllCops:
2
- TargetRubyVersion: 2.3
4
+ TargetRubyVersion: 3.1
5
+ NewCops: enable
6
+ SuggestExtensions: false
7
+ Exclude:
8
+ - 'simple_endpoint.gemspec'
9
+
10
+ Style/Documentation:
11
+ Enabled: false
3
12
 
4
- Metrics/LineLength:
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Style/FrozenStringLiteralComment:
5
17
  Enabled: true
6
- Max: 120
7
18
 
8
19
  Metrics/BlockLength:
9
- Exclude:
10
- - 'spec/**'
20
+ IgnoredMethods: ['describe','context']
11
21
 
12
- Style/Documentation:
22
+ RSpec/DescribeClass:
13
23
  Enabled: false
24
+
25
+ RSpec/NestedGroups:
26
+ Max: 4
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ simple_endpoint
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
data/CHANGES.md ADDED
@@ -0,0 +1,15 @@
1
+ simple_endpoint changelog
2
+ =====================
3
+
4
+ [2022-02-12] Version 1.0.2
5
+ ---------------------------
6
+ - Add Ruby 3+ support
7
+
8
+ [2022-02-09] Version 1.0.1
9
+ ---------------------------
10
+ - Add Ruby 2.7+ support
11
+
12
+ [2020-01-25] Version 1.0.0
13
+ ---------------------------
14
+ **Breaking changes**
15
+ - [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/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # SimpleEndpoint
2
+ [![<differencialx>](https://circleci.com/gh/differencialx/simple_endpoint.svg?style=svg)](https://circleci.com/gh/differencialx/simple_endpoint)
3
+ [![Gem Version](https://img.shields.io/gem/v/simple_endpoint.svg)](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.2'
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'], **result['render_options'], status: 200 },
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
@@ -72,7 +74,8 @@ Now you are able to use `endpoint` method at other controllers
72
74
  | `:different_cases`| no | {} | Cases that should be redefined for exact `#endpoint` call |
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
- | `:before_render` | no | {} | Allow to process code before specific case handler |
77
+ | `:before_response` | 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
+ ```
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleEndpoint
4
- VERSION = '0.1.3'
4
+ VERSION = '1.0.2'
5
5
  end
@@ -1,16 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'simple_endpoint/version'
4
-
3
+ # rubocop:disable Metrics/ParameterLists
5
4
  module SimpleEndpoint
6
5
  module Controller
7
- def endpoint(operation:, different_cases: {}, different_handler: {}, options: {}, before_response: {})
6
+ def endpoint(operation:,
7
+ different_cases: {},
8
+ different_handler: {},
9
+ options: {},
10
+ before_response: {},
11
+ renderer_options: {})
8
12
  Endpoint.call(
9
13
  operation,
10
14
  default_handler.merge(different_handler),
11
15
  default_cases.merge(different_cases),
16
+ renderer_options,
12
17
  before_response,
13
- endpoint_options.merge(options)
18
+ **endpoint_options.merge(options)
14
19
  )
15
20
  end
16
21
 
@@ -30,15 +35,15 @@ module SimpleEndpoint
30
35
  end
31
36
 
32
37
  class Endpoint
33
- def self.call(operation, handler, cases, before_response = {}, **args)
38
+ def self.call(operation, handler, cases, renderer_options = {}, before_response = {}, **args)
34
39
  result = operation.call(**args)
35
- new.call(result, cases, handler, before_response)
40
+ new.call(result, cases, renderer_options, handler, before_response)
36
41
  end
37
42
 
38
- def call(result, cases, handler = {}, before_response = {})
43
+ def call(result, cases, renderer_options = {}, handler = {}, before_response = {})
39
44
  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)
45
+ procees_handler(matched_case, before_response, result, renderer_options) unless before_response.empty?
46
+ procees_handler(matched_case, handler, result, renderer_options, UnhadledResultError)
42
47
  end
43
48
 
44
49
  def matched_case(cases, result)
@@ -48,19 +53,16 @@ module SimpleEndpoint
48
53
  matched_case
49
54
  end
50
55
 
51
- def procees_handler(matched_case, handler, result, exception_class = nil)
56
+ def procees_handler(matched_case, handler, result, renderer_options, exception_class = nil)
52
57
  if handler.key?(matched_case)
53
- handler.dig(matched_case)&.(result)
58
+ handler[matched_case]&.(result, **renderer_options)
54
59
  elsif exception_class
55
60
  raise exception_class, "Key: #{matched_case} is not present at #{handler}"
56
61
  end
57
62
  end
58
63
 
59
64
  def obtain_matched_case(cases, result)
60
- matched_case = cases.each { |kase, condition| break kase if condition.call(result) }
61
- return if matched_case.is_a?(Hash)
62
-
63
- matched_case
65
+ cases.detect { |_kase, condition| condition.call(result) }&.first
64
66
  end
65
67
  end
66
68
 
@@ -124,3 +126,4 @@ module SimpleEndpoint
124
126
  You can move this to separate singleton class
125
127
  LARGE_ERROR
126
128
  end
129
+ # rubocop:enable Metrics/ParameterLists
@@ -11,8 +11,8 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ['differencialx@gmail.com']
12
12
 
13
13
  spec.summary = 'Simple implementation of Trailblazer endpoint'
14
- spec.description = 'Simple implementation of Trailblazer endpoint'
15
- spec.homepage = ''
14
+ spec.description = 'Dry-matcher free implementation of trailblazer endpoint, with ability to redefine matchers and handlers behavior for separate controllers or actions.'
15
+ spec.homepage = 'https://github.com/differencialx/simple_endpoint'
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
18
  # to allow pushing to a single host or delete this section to allow pushing to any host.
@@ -32,9 +32,10 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ['lib']
34
34
 
35
- spec.add_development_dependency 'bundler', '~> 1.16'
35
+ spec.add_development_dependency 'bundler', '>= 2.1.0'
36
36
  spec.add_development_dependency 'pry-byebug'
37
- spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rake', '~> 13.0'
38
38
  spec.add_development_dependency 'rspec', '~> 3.0'
39
- spec.add_development_dependency 'rubocop'
39
+ spec.add_development_dependency 'rubocop', '~> 1.25.1'
40
+ spec.add_development_dependency 'rubocop-rspec'
40
41
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_endpoint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Bal
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-15 00:00:00.000000000 Z
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: 2.1.0
20
20
  type: :development
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: '1.16'
26
+ version: 2.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pry-byebug
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -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: 1.25.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.25.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
@@ -80,19 +94,22 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
- description: Simple implementation of Trailblazer endpoint
97
+ description: Dry-matcher free implementation of trailblazer endpoint, with ability
98
+ to redefine matchers and handlers behavior for separate controllers or actions.
84
99
  email:
85
100
  - differencialx@gmail.com
86
101
  executables: []
87
102
  extensions: []
88
103
  extra_rdoc_files: []
89
104
  files:
105
+ - ".circleci/config.yml"
90
106
  - ".gitignore"
91
107
  - ".rspec"
92
108
  - ".rubocop.yml"
93
- - ".travis.yml"
109
+ - ".ruby-gemset"
110
+ - ".ruby-version"
111
+ - CHANGES.md
94
112
  - Gemfile
95
- - Gemfile.lock
96
113
  - LICENSE
97
114
  - README.md
98
115
  - Rakefile
@@ -101,7 +118,7 @@ files:
101
118
  - lib/simple_endpoint.rb
102
119
  - lib/simple_endpoint/version.rb
103
120
  - simple_endpoint.gemspec
104
- homepage: ''
121
+ homepage: https://github.com/differencialx/simple_endpoint
105
122
  licenses: []
106
123
  metadata:
107
124
  allowed_push_host: https://rubygems.org
@@ -120,8 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
137
  - !ruby/object:Gem::Version
121
138
  version: '0'
122
139
  requirements: []
123
- rubyforge_project:
124
- rubygems_version: 2.7.7
140
+ rubygems_version: 3.1.2
125
141
  signing_key:
126
142
  specification_version: 4
127
143
  summary: Simple implementation of Trailblazer endpoint
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.5.1
5
- before_install: gem install bundler -v 1.16.2
data/Gemfile.lock DELETED
@@ -1,63 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- simple_endpoint (0.1.3)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- ast (2.4.0)
10
- byebug (10.0.2)
11
- coderay (1.1.2)
12
- diff-lcs (1.3)
13
- jaro_winkler (1.5.1)
14
- method_source (0.9.2)
15
- parallel (1.12.1)
16
- parser (2.5.3.0)
17
- ast (~> 2.4.0)
18
- powerpack (0.1.2)
19
- pry (0.12.2)
20
- coderay (~> 1.1.0)
21
- method_source (~> 0.9.0)
22
- pry-byebug (3.6.0)
23
- byebug (~> 10.0)
24
- pry (~> 0.10)
25
- rainbow (3.0.0)
26
- rake (10.5.0)
27
- rspec (3.8.0)
28
- rspec-core (~> 3.8.0)
29
- rspec-expectations (~> 3.8.0)
30
- rspec-mocks (~> 3.8.0)
31
- rspec-core (3.8.0)
32
- rspec-support (~> 3.8.0)
33
- rspec-expectations (3.8.2)
34
- diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.8.0)
36
- rspec-mocks (3.8.0)
37
- diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.8.0)
39
- rspec-support (3.8.0)
40
- rubocop (0.60.0)
41
- jaro_winkler (~> 1.5.1)
42
- parallel (~> 1.10)
43
- parser (>= 2.5, != 2.5.1.1)
44
- powerpack (~> 0.1)
45
- rainbow (>= 2.2.2, < 4.0)
46
- ruby-progressbar (~> 1.7)
47
- unicode-display_width (~> 1.4.0)
48
- ruby-progressbar (1.10.0)
49
- unicode-display_width (1.4.0)
50
-
51
- PLATFORMS
52
- ruby
53
-
54
- DEPENDENCIES
55
- bundler (~> 1.16)
56
- pry-byebug
57
- rake (~> 10.0)
58
- rspec (~> 3.0)
59
- rubocop
60
- simple_endpoint!
61
-
62
- BUNDLED WITH
63
- 1.16.2