boto-rb 0.14.0 → 0.15.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2254e647f1624929b9640368d3bff823bfa3146962a4f806bf13fd7606af69f
4
- data.tar.gz: 2604b8b2ceaa68380231db0eec229caae590dce9b037c0d5f360210e6962eee6
3
+ metadata.gz: 6e6c6e7d53190fb7dc2d634f4eb45d64050bc661488c85cb7c5ff09ce88b7654
4
+ data.tar.gz: a36e99b2cb22d3bf41d5139dd99a53e8434c3cfbc4f499ca632429567144567d
5
5
  SHA512:
6
- metadata.gz: 9d966d746c934606691dfa6fc877627ce16de6299916312f54a61145f1e109ff63c71520881a3c03784be582d5b0f5d90951dca215f04e56b0416b7e9415bc5a
7
- data.tar.gz: 1545dec994608bc66009bc4c0a8f6cc8b4a7f29a148b640c2cc61a36e4df6ca6ddca3762f1015335d6e66941edef2c74fb512146f86e988eecd2423b1b7c60da
6
+ metadata.gz: eb6dfa8e6dc3998e517015ea266fce22bfa3de0321b7c218737f671c1b8da462d5e976efb00352ca71232bce0ef9fafe9f52079c9d51cc974f816bc0d6ec7a34
7
+ data.tar.gz: 0af32eb7b11e4628fc25827402ba30e3670d392e6170282774e1f92d5b2904df285317810929e8f9007b4437a9edee6888a73e3a79eb621cd5a9d4d3760d0be6
data/lib/boto/http/app.rb CHANGED
@@ -16,6 +16,15 @@ module Boto
16
16
  JSON_HEADERS_TYPE = "application/json"
17
17
  NO_CONTENT = 204
18
18
  INTERNAL_ERROR = 500
19
+ UNPROCESSABLE = 422
20
+ FAILURE_STATUSES = {
21
+ invalid: 422,
22
+ unprocessable: 422,
23
+ unauthorized: 401,
24
+ forbidden: 403,
25
+ not_found: 404,
26
+ conflict: 409
27
+ }.freeze
19
28
  EMPTY = {}.freeze
20
29
  NO_INPUT = [EMPTY, EMPTY].freeze
21
30
  HEAD = "HEAD"
@@ -312,12 +321,28 @@ module Boto
312
321
  end
313
322
 
314
323
  def success_response(entry, result)
324
+ return operation_response(entry, result) if result.is_a?(Boto::Operation::Result)
315
325
  return [NO_CONTENT, {}, []] if result.nil?
316
326
 
317
327
  payload = entry.return_schema ? serialize_result(entry, result) : result
318
328
  [entry.status, { "content-type" => JSON_HEADERS_TYPE }, [JSON.generate(payload)]]
319
329
  end
320
330
 
331
+ def operation_response(entry, result)
332
+ return failure_response(result.failure) unless result.success?
333
+
334
+ success_response(entry, result.value)
335
+ end
336
+
337
+ # Details stay out of the response on purpose: they may hold internal data.
338
+ def failure_response(failure)
339
+ Boto::Problem.response(
340
+ FAILURE_STATUSES.fetch(failure.code, UNPROCESSABLE),
341
+ detail: failure.message,
342
+ code: failure.code
343
+ )
344
+ end
345
+
321
346
  def serialize_result(entry, result)
322
347
  return entry.return_schema.serialize(result) unless entry.returns_list
323
348
 
@@ -8,17 +8,18 @@ module Boto
8
8
  module Problem
9
9
  CONTENT_TYPE = "application/problem+json"
10
10
 
11
- def self.response(status, detail: nil, errors: nil, headers: {})
12
- body = JSON.generate(compact_body(status, detail, errors))
11
+ def self.response(status, detail: nil, errors: nil, headers: {}, code: nil)
12
+ body = JSON.generate(compact_body(status, detail, code, errors))
13
13
  [status, { "content-type" => CONTENT_TYPE }.merge(headers), [body]]
14
14
  end
15
15
 
16
- def self.compact_body(status, detail, errors)
16
+ def self.compact_body(status, detail, code, errors)
17
17
  {
18
18
  type: "about:blank",
19
19
  title: Rack::Utils::HTTP_STATUS_CODES.fetch(status),
20
20
  status: status,
21
21
  detail: detail,
22
+ code: code&.to_s,
22
23
  errors: errors
23
24
  }.compact
24
25
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ class Operation
5
+ # Raised by Result#value! on a failure. Keeps the Result so the caller can read the code and details.
6
+ class FailedError < StandardError
7
+ attr_reader :result
8
+
9
+ def initialize(result)
10
+ @result = result
11
+ super(describe(result.failure))
12
+ end
13
+
14
+ private
15
+
16
+ def describe(failure)
17
+ failure.message ? "#{failure.code}: #{failure.message}" : failure.code.to_s
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ class Operation
5
+ # Why an operation failed: a Symbol code for the caller to branch on, plus a message and extra details.
6
+ class Failure < Data.define(:code, :message, :details)
7
+ def initialize(code:, message: nil, details: {})
8
+ raise ArgumentError, "failure code must be a Symbol, got #{code.inspect}" unless code.is_a?(Symbol)
9
+
10
+ # dup: freezing the caller's own hash would be a surprising side effect.
11
+ super(code:, message:, details: details.dup.freeze)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ class Operation
5
+ # What an operation returns: a value on success, or a Failure. Immutable.
6
+ class Result < Data.define(:value, :failure)
7
+ def self.success(value = nil) = new(value: value, failure: nil)
8
+
9
+ def self.failure(code, message = nil, **details)
10
+ new(value: nil, failure: Failure.new(code:, message:, details:))
11
+ end
12
+
13
+ def success? = failure.nil?
14
+
15
+ def failure? = !success?
16
+
17
+ def value!
18
+ return value if success?
19
+
20
+ raise FailedError.new(self)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ # Base class of operations (business rules). Call it with .call; it always returns a Result.
5
+ class Operation
6
+ def self.call(...)
7
+ returned = new(...).call
8
+ returned.is_a?(Result) ? returned : Result.success(returned)
9
+ end
10
+ private_class_method :new
11
+
12
+ def initialize(input = nil)
13
+ @input = input
14
+ end
15
+
16
+ def call
17
+ raise NotImplementedError, "#{self.class}#call"
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :input
23
+
24
+ def success(value = nil) = Result.success(value)
25
+ def failure(code, message = nil, **details) = Result.failure(code, message, **details)
26
+ end
27
+ end
data/lib/boto/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Boto
4
- VERSION = "0.14.0"
4
+ VERSION = "0.15.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boto-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evo Tech Builder
@@ -55,6 +55,10 @@ files:
55
55
  - lib/boto/openapi.rb
56
56
  - lib/boto/openapi/operation.rb
57
57
  - lib/boto/openapi/schema_catalog.rb
58
+ - lib/boto/operation.rb
59
+ - lib/boto/operation/failed_error.rb
60
+ - lib/boto/operation/failure.rb
61
+ - lib/boto/operation/result.rb
58
62
  - lib/boto/plugin.rb
59
63
  - lib/boto/plugin/stack.rb
60
64
  - lib/boto/plugins/health.rb