faraday-openapi 0.2.0 → 0.3.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: 9f4f143b7d1bfe03b425ccb2b7f9551c0f1432495fd49dd9c98f1308a3099901
4
- data.tar.gz: 9c0d8e7542a3144f933fdf292ae1fdabfe1a5210a9cea55ac8e035fe769046fb
3
+ metadata.gz: c5288681710835ea083d20cd469415b3cba5a880cb535692419a0090c0e1432f
4
+ data.tar.gz: 8e4cbe4290b598c8f9fdb5f15eb1505070c83e1dc8bbc91761a01fb9401d4913
5
5
  SHA512:
6
- metadata.gz: 989d36d2f1704d3bbfa45630eac87f0cf992c7d9f2d3e50fe9a5c5aa840ed0065b6e0b6cf5eaba87b30c0c3d6c9c2cbe2a88bf5d5cc77692dae7f7935b40381b
7
- data.tar.gz: a4d6196f44f0df42271dc88c266797a7238c14f9d10bf6322955353d2dc8f78397dec5041f8124c90f94df6bf703ab076f85b9f46ce870877bfffd618ae1e056
6
+ metadata.gz: a7e2eb94fdc7d4e6b96d03485cc6364f7f49d6ab4eb1b13928e03015734e40dcf84cfa21e323935794d23d036bc02945926ffb00a007694e1bf7af00d2919707
7
+ data.tar.gz: 23cfeddcd5ff1dd3cdf7512ae08780799b15bc4ddab990f7edc1246bde72b4cd167bc7b3f8fba76637f702047ec61945b64c5d6eb34ae3fd1808ab80ec1a6c1f
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.3.0
6
+
7
+ - Breaking change: Use `Faraday::Openapi.enabled=` instead of `Faraday::Openapi::Middleware.enabled=`
8
+ - Fix: Make `f.request :openapi` handle only request, not response validation
9
+ - Support passing a Hash to .register
10
+
5
11
  ## 0.2.0
6
12
 
7
13
  - Add Faraday::Openapi.register to easily load, cache and reference OADs
data/README.md CHANGED
@@ -41,7 +41,7 @@ require 'faraday/openapi'
41
41
  Faraday::Openapi.register 'dice-openapi.yaml', as: :dice_api
42
42
 
43
43
  # Only activate in test env
44
- Faraday::Openapi::Middleware.enabled = ENV['RACK_ENV'] == 'test'
44
+ Faraday::Openapi.enabled = ENV['RACK_ENV'] == 'test'
45
45
  ```
46
46
 
47
47
  ```ruby
@@ -63,10 +63,10 @@ conn = Faraday.new do |f|
63
63
  end
64
64
  ```
65
65
 
66
- You can disable the whole middleware globally via Faraday's conventional default_options as well:
66
+ You can disable all middlewares in this gem globally, which you probably want to do on production.
67
67
 
68
68
  ```ruby
69
- Faraday::Openapi::Middleware.default_options[:enabled] = false
69
+ Faraday::Openapi.enabled = false
70
70
  ```
71
71
 
72
72
 
@@ -7,39 +7,24 @@ require_relative 'response'
7
7
 
8
8
  module Faraday
9
9
  module Openapi
10
- # This class provides the main implementation for your middleware.
11
- # Your middleware can implement any of the following methods:
12
- # * on_request - called when the request is being prepared
13
- # * on_complete - called when the response is being processed
14
- #
15
- # Optionally, you can also override the following methods from Faraday::Middleware
16
- # * initialize(app, options = {}) - the initializer method
17
- # * call(env) - the main middleware invocation method.
18
- # This already calls on_request and on_complete, so you normally don't need to override it.
19
- # You may need to in case you need to "wrap" the request or need more control
20
- # (see "retry" middleware: https://github.com/lostisland/faraday-retry/blob/41b7ea27e30d99ebfed958abfa11d12b01f6b6d1/lib/faraday/retry/middleware.rb#L147).
21
- # IMPORTANT: Remember to call `@app.call(env)` or `super` to not interrupt the middleware chain!
22
- class Middleware < Faraday::Middleware
23
- DEFAULT_OPTIONS = { enabled: true }.freeze
24
-
25
- def self.enabled=(bool)
26
- Faraday::Openapi::Middleware.default_options[:enabled] = bool
27
- end
28
-
10
+ # Methods for all middlewares
11
+ module Base
29
12
  def initialize(app, path = :default)
30
13
  super(app)
31
- @enabled = options.fetch(:enabled, true)
32
- return unless @enabled
14
+ return unless Openapi.enabled
33
15
 
34
16
  @oad = path.is_a?(Symbol) ? Faraday::Openapi[path] : OpenapiFirst.load(path)
35
17
  end
36
18
 
37
19
  def call(env)
38
- return app.call(env) unless @enabled
20
+ return app.call(env) unless Openapi.enabled
39
21
 
40
22
  super
41
23
  end
24
+ end
42
25
 
26
+ # on_request method to handle request validation
27
+ module RequestValidation
43
28
  # This method will be called when the request is being prepared.
44
29
  # You can alter it as you like, accessing things like request_body, request_headers, and more.
45
30
  # Refer to Faraday::Env for a list of accessible fields:
@@ -52,7 +37,10 @@ module Faraday
52
37
  rescue OpenapiFirst::RequestInvalidError => e
53
38
  raise RequestInvalidError, e.message
54
39
  end
40
+ end
55
41
 
42
+ # on_complete method to handle response validation
43
+ module ResponseValidation
56
44
  # This method will be called when the response is being processed.
57
45
  # You can alter it as you like, accessing things like response_body, response_headers, and more.
58
46
  # Refer to Faraday::Env for a list of accessible fields:
@@ -69,5 +57,21 @@ module Faraday
69
57
  raise ResponseInvalidError, e.message
70
58
  end
71
59
  end
60
+
61
+ class Middleware < Faraday::Middleware
62
+ include Base
63
+ include RequestValidation
64
+ include ResponseValidation
65
+ end
66
+
67
+ class RequestMiddleware < Faraday::Middleware
68
+ include Base
69
+ include RequestValidation
70
+ end
71
+
72
+ class ResponseMiddleware < Faraday::Middleware
73
+ include Base
74
+ include ResponseValidation
75
+ end
72
76
  end
73
77
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Openapi
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -16,19 +16,22 @@ module Faraday
16
16
  # * conn.use Faraday::Openapi::Middleware
17
17
  # * conn.use :openapi
18
18
  Faraday::Middleware.register_middleware(openapi: Faraday::Openapi::Middleware)
19
- Faraday::Request.register_middleware(openapi: Faraday::Openapi::Middleware)
20
- Faraday::Response.register_middleware(openapi: Faraday::Openapi::Middleware)
19
+ Faraday::Request.register_middleware(openapi: Faraday::Openapi::RequestMiddleware)
20
+ Faraday::Response.register_middleware(openapi: Faraday::Openapi::ResponseMiddleware)
21
21
 
22
22
  @registry = {}
23
+ @enabled = true
23
24
 
24
25
  class << self
25
26
  attr_reader :registry
27
+ attr_accessor :enabled
26
28
  end
27
29
 
28
30
  def self.register(filepath, as: :default)
29
31
  raise AlreadyRegisteredError, "API description #{as} is already registered" if registry.key?(as)
30
32
 
31
- registry[as] = OpenapiFirst.load(filepath)
33
+ oad = filepath.is_a?(Hash) ? OpenapiFirst.parse(filepath) : OpenapiFirst.load(filepath)
34
+ registry[as] = oad
32
35
  end
33
36
 
34
37
  def self.[](key)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-26 00:00:00.000000000 Z
10
+ date: 2025-03-14 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: faraday
@@ -93,7 +93,7 @@ licenses:
93
93
  metadata:
94
94
  bug_tracker_uri: https://codeberg.org/ahx/faraday-openapi/issues
95
95
  changelog_uri: https://codeberg.org/ahx/faraday-openapi/src/branch/main/CHANGELOG.md
96
- documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.2.0
96
+ documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.3.0
97
97
  homepage_uri: https://codeberg.org/ahx/faraday-openapi
98
98
  rubygems_mfa_required: 'true'
99
99
  source_code_uri: https://codeberg.org/ahx/faraday-openapi