faraday-openapi 0.1.1 → 0.2.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: 3b0869a090382dc9f8780e3039255868de82f073a707f57a7d80293c03a1137e
4
- data.tar.gz: b21feb28749ab4d2adbd5ce16dbc64dc944c375836af229e17e5fabeff9e63cd
3
+ metadata.gz: 9f4f143b7d1bfe03b425ccb2b7f9551c0f1432495fd49dd9c98f1308a3099901
4
+ data.tar.gz: 9c0d8e7542a3144f933fdf292ae1fdabfe1a5210a9cea55ac8e035fe769046fb
5
5
  SHA512:
6
- metadata.gz: '08cb67099b1fb9a6d3ee9f223f7ecc2d0d1fdabad49eb462c6c4262df3433f8b6267fcc31af83679ccfcf7904d7daa6d16d70d6b471caabc482c05a616b349fd'
7
- data.tar.gz: f39f446d15a1c20228a2c1157825eb2b139345e5db794b5e5a5e53ed3b62af7077bcae3ce9d7bcf4233496ae262c9b7ca1e334b298413b0ea30dc70522b06270
6
+ metadata.gz: 989d36d2f1704d3bbfa45630eac87f0cf992c7d9f2d3e50fe9a5c5aa840ed0065b6e0b6cf5eaba87b30c0c3d6c9c2cbe2a88bf5d5cc77692dae7f7935b40381b
7
+ data.tar.gz: a4d6196f44f0df42271dc88c266797a7238c14f9d10bf6322955353d2dc8f78397dec5041f8124c90f94df6bf703ab076f85b9f46ce870877bfffd618ae1e056
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.2.0
6
+
7
+ - Add Faraday::Openapi.register to easily load, cache and reference OADs
8
+
5
9
  ## 0.1.1
6
10
 
7
11
  Fix URL to homepage, changelog
data/README.md CHANGED
@@ -7,6 +7,14 @@ You can use this to test your client code or to make sure your mocks do match th
7
7
 
8
8
  Note that the middleware currently deliberately ignores **unknown** responses with status codes 401 or higher because those usually don't come with a useful response body.
9
9
 
10
+ ## TL;DR
11
+
12
+ ```ruby
13
+ conn = Faraday.new do |f|
14
+ f.use :openapi
15
+ end
16
+ ```
17
+
10
18
  ## Installation
11
19
 
12
20
  Add this line to your application's Gemfile:
@@ -21,33 +29,41 @@ And then execute:
21
29
  bundle install
22
30
  ```
23
31
 
24
- Or install it yourself as:
32
+ ## Usage
25
33
 
26
- ```shell
27
- gem install faraday-openapi
28
- ```
34
+ In order to avoid loading YAML files at inappropriate times you should
35
+ register your API description (OAD) globally and reference it via a Symbol in your client code
29
36
 
30
- ## Usage
37
+ ```ruby
38
+ # initializer.rb
39
+
40
+ require 'faraday/openapi'
41
+ Faraday::Openapi.register 'dice-openapi.yaml', as: :dice_api
42
+
43
+ # Only activate in test env
44
+ Faraday::Openapi::Middleware.enabled = ENV['RACK_ENV'] == 'test'
45
+ ```
31
46
 
32
47
  ```ruby
48
+ # some_client.rb
33
49
  require 'faraday/openapi'
34
50
 
35
51
  conn = Faraday.new do |f|
36
- f.use :openapi, 'openapi/openapi.yaml'
52
+ f.use :openapi, :dice_api
37
53
  end
38
54
 
39
55
  # Or validate only requests
40
56
  conn = Faraday.new do |f|
41
- f.request :openapi, 'openapi/openapi.yaml'
57
+ f.request :openapi, :dice_api
42
58
  end
43
59
 
44
60
  # Or validate only responses
45
61
  conn = Faraday.new do |f|
46
- f.response :openapi, 'openapi/openapi.yaml'
62
+ f.response :openapi, :dice_api
47
63
  end
48
64
  ```
49
65
 
50
- You can disable the whole middleware globally:
66
+ You can disable the whole middleware globally via Faraday's conventional default_options as well:
51
67
 
52
68
  ```ruby
53
69
  Faraday::Openapi::Middleware.default_options[:enabled] = false
@@ -67,7 +83,7 @@ See how it works [here](https://bundler.io/guides/creating_gem.html#releasing-th
67
83
 
68
84
  ## Contributing
69
85
 
70
- Bug reports and pull requests are welcome on [Codeberg](https://codeberg.org/ahx/faraday-openapi).
86
+ Bug reports and pull requests are welcome on [Codeberg](https://codeberg.org/ahx/faraday-openapi) or [Github](https://github.com/ahx/faraday-openapi).
71
87
 
72
88
  ## License
73
89
 
@@ -10,5 +10,8 @@ module Faraday
10
10
 
11
11
  # Raised if response does not match API description or is unknown
12
12
  class ResponseInvalidError < Error; end
13
+
14
+ class AlreadyRegisteredError < Error; end
15
+ class NotRegisteredError < Error; end
13
16
  end
14
17
  end
@@ -22,11 +22,16 @@ module Faraday
22
22
  class Middleware < Faraday::Middleware
23
23
  DEFAULT_OPTIONS = { enabled: true }.freeze
24
24
 
25
- def initialize(app, filepath)
25
+ def self.enabled=(bool)
26
+ Faraday::Openapi::Middleware.default_options[:enabled] = bool
27
+ end
28
+
29
+ def initialize(app, path = :default)
26
30
  super(app)
27
- @filepath = filepath
28
31
  @enabled = options.fetch(:enabled, true)
29
- @oad = OpenapiFirst.load(filepath) if @enabled
32
+ return unless @enabled
33
+
34
+ @oad = path.is_a?(Symbol) ? Faraday::Openapi[path] : OpenapiFirst.load(path)
30
35
  end
31
36
 
32
37
  def call(env)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Openapi
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'openapi_first'
3
4
  require_relative 'openapi/errors'
4
5
  require_relative 'openapi/middleware'
5
6
  require_relative 'openapi/version'
@@ -14,18 +15,33 @@ module Faraday
14
15
  # After calling this line, the following are both valid ways to set the middleware in a connection:
15
16
  # * conn.use Faraday::Openapi::Middleware
16
17
  # * conn.use :openapi
17
- # Without this line, only the former method is valid.
18
18
  Faraday::Middleware.register_middleware(openapi: Faraday::Openapi::Middleware)
19
19
  Faraday::Request.register_middleware(openapi: Faraday::Openapi::Middleware)
20
20
  Faraday::Response.register_middleware(openapi: Faraday::Openapi::Middleware)
21
21
 
22
- # Alternatively, you can register your middleware under Faraday::Request or Faraday::Response.
23
- # This will allow to load your middleware using the `request` or `response` methods respectively.
24
- #
25
- # Load middleware with conn.request :openapi
26
- # Faraday::Request.register_middleware(openapi: Faraday::Openapi::Middleware)
27
- #
28
- # Load middleware with conn.response :openapi
29
- # Faraday::Response.register_middleware(openapi: Faraday::Openapi::Middleware)
22
+ @registry = {}
23
+
24
+ class << self
25
+ attr_reader :registry
26
+ end
27
+
28
+ def self.register(filepath, as: :default)
29
+ raise AlreadyRegisteredError, "API description #{as} is already registered" if registry.key?(as)
30
+
31
+ registry[as] = OpenapiFirst.load(filepath)
32
+ end
33
+
34
+ def self.[](key)
35
+ registry.fetch(key) do
36
+ message = if registry.empty?
37
+ 'No API descriptions have been registered. Please register your API description via ' \
38
+ "Faraday::Openapi.register('myopenapi.yaml')"
39
+ else
40
+ "API description #{key.inspect} was not found. Please register your API description via " \
41
+ "Faraday::Openapi.register('myopenapi.yaml'#{key == :default ? '' : ", as: #{key.inspect}"})"
42
+ end
43
+ raise NotRegisteredError, message
44
+ end
45
+ end
30
46
  end
31
47
  end
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.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-18 00:00:00.000000000 Z
10
+ date: 2025-02-26 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.1.1
96
+ documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.2.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
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  - !ruby/object:Gem::Version
115
115
  version: '0'
116
116
  requirements: []
117
- rubygems_version: 3.6.2
117
+ rubygems_version: 3.6.5
118
118
  specification_version: 4
119
119
  summary: Validate requests/responses against OpenAPI API descriptions
120
120
  test_files: []