faraday-openapi 0.9.0 → 0.9.1

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: cecade70e78308caa40373dfb520b6fa3f970c7113e62ce116c3a8dde4404bfd
4
- data.tar.gz: ee5789e11fd934282384fe8a92d0a9370ca865c5aa08cfcaa25d53d5aa1306fd
3
+ metadata.gz: 5e437d23f0221b37e8eecc7cafa6ea93fbc34fa5e1c85a88e9132bec56f56f2a
4
+ data.tar.gz: d961589d81e7f8359fcaefd09bf0d82037afd3c2431d2b481e8a26e0472bdc4a
5
5
  SHA512:
6
- metadata.gz: 31210ba4b06e64ff1886548d6eba1d3a41c5aa19ad5027b8116331df8f9f83ee5242dd4a73c50bfc1c98dc648c8c2e3f29fad29078e1186bb31dca0c6f54d701
7
- data.tar.gz: cf51b54547212804022fec02f6efc172b2a2b9dc4dc394107d1053055de989eb8d2ad52b78f6c98e1a8739f5718de606fe33f974ae5493c185ce35025f0f5269
6
+ metadata.gz: c1d42c5151bb776b5e36a1536f76316e9330d704dc80cf4fdfda6bc5731a2490fae727dc17463679b573f34ebc5fd5f96dbd618f81beb965a1dae4eac0e1cdca
7
+ data.tar.gz: 9f11cf5ed8550bfa405007aca347f3408595a1f3fc1652a2853d834bed80cbb2d9baabaff4e84fa57e2e6a9d3e3315531cb5cbd6cfe884cf6fe48941478c528b
data/CHANGELOG.md CHANGED
@@ -2,9 +2,11 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
- ## 0.9.0
5
+ ## 0.9.1
6
6
 
7
- - Faraday::Openapi.register is a no-op if the middleware is not enabled (Faraday::Openapi.enabled returns false)
7
+ - Changed: `Faraday::Openapi.register` is lazy now. It load or downloads the API description on first use. A failed download now raises on first use of the API description instead of in `register`.
8
+
9
+ ## 0.9.0 (yanked)
8
10
 
9
11
  ## 0.8.3
10
12
 
data/README.md CHANGED
@@ -31,8 +31,8 @@ bundle install
31
31
 
32
32
  ## Usage
33
33
 
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
34
+ Register your API description (OAD) globally and reference it via a Symbol in your client code.
35
+ Registering is cheap: the OAD is loaded on first use.
36
36
 
37
37
  ```ruby
38
38
  # initializer.rb
@@ -81,7 +81,7 @@ You can define the origin of the used OAD via the :source option:
81
81
  Faraday::Openapi.register 'consumed/dice.yaml', as: :dice_api, source: 'http://localhost:8080/apis/dice/openapi.yaml',
82
82
  ```
83
83
 
84
- If 'consumed/dice.yaml' does NOT exist, it will try to download the file from the defined source.
84
+ If 'consumed/dice.yaml' does NOT exist, it will download the file from the defined source on first use.
85
85
  You can call `Faraday::Openapi.update_registered` to re-download all registered files.
86
86
 
87
87
 
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'rack'
5
+
6
+ module Faraday
7
+ module Openapi
8
+ Registered = Struct.new(:name, :allow_partial_responses, :source, :filepath) do
9
+ def oad
10
+ download if source && !File.exist?(filepath)
11
+ @oad ||= filepath.is_a?(Hash) ? OpenapiFirst.parse(filepath) : OpenapiFirst.load(filepath)
12
+ rescue OpenapiFirst::FileNotFoundError => e
13
+ raise Faraday::Openapi::FileNotFoundError, e.message
14
+ end
15
+
16
+ def update!
17
+ download
18
+ @oad = nil
19
+ end
20
+
21
+ private
22
+
23
+ def download
24
+ FileUtils.mkdir_p(File.dirname(filepath))
25
+ File.write(filepath, connection.get(source).body)
26
+ Openapi.logger.info("Loaded API description for #{name.inspect} from #{source.inspect}")
27
+ rescue Faraday::Error => e
28
+ raise Faraday::Openapi::Error,
29
+ "I was not able to download the OAD for #{name.inspect} from #{source}: #{e.message}"
30
+ end
31
+
32
+ def connection
33
+ Faraday.new(request: { open_timeout: 5, timeout: 5 }) do |f|
34
+ f.response :raise_error, include_request: true
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Openapi
5
- VERSION = '0.9.0'
5
+ VERSION = '0.9.1'
6
6
  end
7
7
  end
@@ -5,6 +5,7 @@ require 'logger'
5
5
  require 'openapi_first'
6
6
  require_relative 'openapi/errors'
7
7
  require_relative 'openapi/middleware'
8
+ require_relative 'openapi/registered'
8
9
  require_relative 'openapi/version'
9
10
 
10
11
  module Faraday
@@ -33,55 +34,21 @@ module Faraday
33
34
  class << self
34
35
  attr_reader :registry
35
36
  attr_accessor :enabled, :logger
36
-
37
- private
38
-
39
- def connection
40
- @connection ||= Faraday.new(request: {
41
- open_timeout: 5,
42
- timeout: 5
43
- }) do |f|
44
- f.response :raise_error, include_request: true
45
- end
46
- end
47
-
48
- def download(name, from:, to:)
49
- filepath = to
50
- url = from
51
- FileUtils.mkdir_p(File.dirname(filepath))
52
- File.write(filepath, connection.get(url).body)
53
- logger.info(
54
- "Loaded API description for #{name.inspect} from #{url.inspect}"
55
- )
56
- rescue Faraday::Error => e
57
- raise Faraday::Openapi::Error,
58
- "I was not able to download the OAD for #{name.inspect} from #{url}: #{e.message}"
59
- end
60
37
  end
61
38
 
62
- Registered = Struct.new(:oad, :allow_partial_responses, :source, :filepath)
63
- private_constant :Registered
64
-
65
39
  def self.register(filepath, as: :default, allow_partial_responses: false, source: nil)
66
- return unless enabled
67
-
68
40
  raise AlreadyRegisteredError, "API description #{as} is already registered" if registry.key?(as)
69
41
 
70
- download(as, from: source, to: filepath) if source && !File.exist?(filepath)
42
+ if source.nil? && filepath.is_a?(String) && !File.exist?(filepath)
43
+ raise Faraday::Openapi::FileNotFoundError, "File not found: #{filepath}"
44
+ end
71
45
 
72
- oad = filepath.is_a?(Hash) ? OpenapiFirst.parse(filepath) : OpenapiFirst.load(filepath)
73
- registry[as] = Registered.new(oad:, allow_partial_responses:, source:, filepath:)
46
+ registry[as] = Registered.new(name: as, allow_partial_responses:, source:, filepath:)
74
47
  self
75
- rescue OpenapiFirst::FileNotFoundError => e
76
- raise Faraday::Openapi::FileNotFoundError, e.message
77
48
  end
78
49
 
79
50
  def self.update_registered
80
- registry.each do |name, config|
81
- next unless config.source
82
-
83
- download(name, from: config.source, to: config.filepath)
84
- end
51
+ registry.each_value { |registered| registered.update! if registered.source }
85
52
  end
86
53
 
87
54
  def self.[](key)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
@@ -84,6 +84,7 @@ files:
84
84
  - lib/faraday/openapi.rb
85
85
  - lib/faraday/openapi/errors.rb
86
86
  - lib/faraday/openapi/middleware.rb
87
+ - lib/faraday/openapi/registered.rb
87
88
  - lib/faraday/openapi/request.rb
88
89
  - lib/faraday/openapi/response.rb
89
90
  - lib/faraday/openapi/version.rb
@@ -93,7 +94,7 @@ licenses:
93
94
  metadata:
94
95
  bug_tracker_uri: https://codeberg.org/ahx/faraday-openapi/issues
95
96
  changelog_uri: https://codeberg.org/ahx/faraday-openapi/src/branch/main/CHANGELOG.md
96
- documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.9.0
97
+ documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.9.1
97
98
  homepage_uri: https://codeberg.org/ahx/faraday-openapi
98
99
  rubygems_mfa_required: 'true'
99
100
  source_code_uri: https://codeberg.org/ahx/faraday-openapi