carioca 2.1.6 → 2.1.8

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: 59956550392bbf8603dd8426a0595afb7cd207bb6929adac7a183eca58acc400
4
- data.tar.gz: 95addcdb4fde913f5ec4dc4a15184634a24c27522e4f93a26b5912c55561d0db
3
+ metadata.gz: 5d835f54ee7bbeee2d8536aa2baaee5ea96800e6c044b52ba15a5f9c711dad72
4
+ data.tar.gz: 3bed4c3fbe420f22f3e4949b5b9c9eae874475f941f9c6ede40c8b72a801f9bc
5
5
  SHA512:
6
- metadata.gz: a26bacd589861a1543184a73791c3ea37ac0f05742efe82a414a5dc243f448824d10d9973f5b1c73fc8208fc167c004d8e67d82390c9bf6583695d673e645bf0
7
- data.tar.gz: 48001979dde2a20fe79c140f72739b2d9d7dd93523082edd11c85f720225e14428fd6873e4e81c68d82c78fd51b43858e8156819ee76f78a4ae1044c69fede9e
6
+ metadata.gz: da28ddf9bc986db91af0ed59825e42753dbe8c1dd86dbf6a0412faa5af64b917624c7a0b12648fadf12f11e8b42b7c81aa74a4e70e4c4c8e50b0a6adbf919304
7
+ data.tar.gz: b487f45e3a31842aa06994721e5856ba14c0406f2267c08a5184915b737444508bf97031c94d4f0864ef1abf6697e14f887ad594c746b6fa704759c8d3554623
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.6
1
+ 2.1.8
@@ -12,6 +12,17 @@ module Carioca
12
12
  end
13
13
  end
14
14
 
15
+
16
+ class AlternativeCatcher < StandardError
17
+ attr_reader :return_case, :data
18
+
19
+ def initialize(*arg, return_case: :status_ko, data: nil)
20
+ super(*arg)
21
+ @return_case = return_case
22
+ @data = data
23
+ end
24
+ end
25
+
15
26
  # Exiter namespace
16
27
  class Finisher
17
28
  DEFAULT_FINISHERS_SPECS = {
@@ -95,12 +106,17 @@ module Carioca
95
106
  data = yield if block_given?
96
107
  result = structured ? do_return(return_case:).merge({ data: }) : data
97
108
  rescue StandardError => e
98
- key = e.respond_to?(:error_case) ? e.error_case : :status_ko
99
- more = e.respond_to?(:error_case) ? e.message : "#{e.class} : #{e.message}"
100
- result = do_return return_case: key, more:
109
+ key = :status_ko
110
+ more = "#{e.class} : #{e.message}"
111
+ if e.respond_to?(:error_case)
112
+ result = do_return(return_case: e.error_case, more: e.message)
113
+ elsif e.respond_to?(:return_case)
114
+ result = structured ? do_return(return_case: e.return_case, more: e.message).merge({ data: e.data}) : e.data
115
+ else
116
+ result = do_return(return_case: :status_ko, more: "#{e.class.to_s} : #{e.message}")
117
+ end
101
118
  end
102
119
  if status && structured && json
103
- p result
104
120
  { status: result[:code], data: JSON.pretty_generate(JSON.parse(result.to_json)) }
105
121
  elsif json
106
122
  JSON.pretty_generate(JSON.parse(result.to_json)) if json
@@ -109,6 +125,11 @@ module Carioca
109
125
  end
110
126
  end
111
127
 
128
+ def secure_alternative(message: , return_case: :accepted, data: )
129
+ raise AlternativeCatcher.new(message, return_case: return_case, data: data)
130
+ end
131
+
132
+
112
133
  def secure_execute!(exit_case: :success_exit)
113
134
  begin
114
135
  more = yield
data/samples/test.rb CHANGED
@@ -175,28 +175,55 @@ puts test
175
175
  output.item 'api return, no-json, no-structured but with secure_raise'
176
176
  test = finisher.secure_api_return(return_case: :status_ok, structured: false, json: false) do
177
177
  finisher.secure_raise message: 'error !', error_case: :status_ko
178
- 'test'
178
+ { test: 'test'}
179
179
  end
180
180
  puts test
181
181
 
182
182
  output.item 'api return, json, structured but with secure_raise'
183
183
  test = finisher.secure_api_return(return_case: :status_ok, structured: true, json: true) do
184
184
  finisher.secure_raise message: 'error !', error_case: :status_ko
185
- 'test'
185
+ test = { test: 'test'}
186
+ test
186
187
  end
187
188
  puts test[:status]
188
189
  puts test[:data]
189
190
 
190
191
  output.item 'api return, json, structured'
191
192
  test = finisher.secure_api_return(return_case: :status_ok, structured: true, json: true) do
192
- 'test'
193
+ test = { test: 'test' }
194
+ test
195
+ end
196
+ puts test[:status]
197
+ puts test[:data]
198
+
199
+
200
+ output.item 'api return, json, structured with alternative'
201
+ test = finisher.secure_api_return(return_case: :status_ok, structured: true, json: true) do
202
+ test = { test: 'test' }
203
+ finisher.secure_alternative message: "test alernative", return_case: :accepted, data: test
204
+ test
193
205
  end
194
206
  puts test[:status]
195
207
  puts test[:data]
196
208
 
209
+
210
+ output.item 'api return, json, structured with alternative'
211
+ test = finisher.secure_api_return(return_case: :status_ok, structured: false, json: false) do
212
+ test = { test: 'test' }
213
+ finisher.secure_alternative message: "test alernative", return_case: :accepted, data: test
214
+ test
215
+ end
216
+ puts test
217
+
197
218
  output.item 'api return, json, structured with status=false'
198
219
  test = finisher.secure_api_return(return_case: :status_ok, structured: true, json: true, status: false) do
199
- 'test'
220
+ { test: 'test'}
221
+ end
222
+ puts test
223
+
224
+ output.item 'api return, json, structured but raising unknown case'
225
+ test = finisher.secure_api_return(return_case: :status_ok, structured: true, json: true, status: false) do
226
+ raise "Boom"
200
227
  end
201
228
  puts test
202
229
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carioca
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.6
4
+ version: 2.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain GEORGES
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-17 00:00:00.000000000 Z
11
+ date: 2024-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge