Class: RSpec::Rails::Api::OpenApiRenderer
- Inherits:
-
Object
- Object
- RSpec::Rails::Api::OpenApiRenderer
- Defined in:
- lib/rspec/rails/api/open_api_renderer.rb
Overview
Class to render metadata. Example:
```rb
renderer = RSpec::Rails::Api::OpenApiRenderer.new
renderer.merge_context(example_context)
renderer.write_files
```
Instance Attribute Summary collapse
-
#api_description ⇒ Object
writeonly
rubocop:disable Metrics/ClassLength.
-
#api_servers ⇒ Object
writeonly
rubocop:disable Metrics/ClassLength.
-
#api_title ⇒ Object
writeonly
rubocop:disable Metrics/ClassLength.
-
#api_tos ⇒ Object
writeonly
rubocop:disable Metrics/ClassLength.
-
#api_version ⇒ Object
writeonly
rubocop:disable Metrics/ClassLength.
-
#redactables ⇒ Object
readonly
Returns the value of attribute redactables.
Instance Method Summary collapse
-
#add_security_scheme(reference, definition) ⇒ Object
Adds a security scheme definition to the API documentation.
-
#api_contact=(name: nil, email: nil, url: nil) ⇒ void
Sets the contact field.
-
#api_license=(name: nil, url: nil) ⇒ void
Sets the license field.
-
#initialize ⇒ OpenApiRenderer
constructor
A new instance of OpenApiRenderer.
-
#merge_context(context, dump_metadata: false) ⇒ void
Merges example context definition with the renderer data.
-
#prepare_metadata ⇒ Hash
Extracts metadata from context to generate an OpenAPI structure.
- #redact_responses(pairs) ⇒ Object
-
#write_files(path = nil, only: %i[yaml json])) ⇒ void
Write OpenAPI files.
Constructor Details
#initialize ⇒ OpenApiRenderer
Returns a new instance of OpenApiRenderer.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 20 def initialize @metadata = { resources: {}, entities: {} } @api_infos = {} @api_servers = [] @api_paths = {} @api_components = {} @api_tags = [] @api_contact = {} @api_license = {} @api_security = {} @redactables = {} end |
Instance Attribute Details
#api_description=(value) ⇒ Object (writeonly)
rubocop:disable Metrics/ClassLength
17 18 19 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 17 def api_description=(value) @api_description = value end |
#api_servers=(value) ⇒ Object
rubocop:disable Metrics/ClassLength
17 18 19 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 17 def api_servers=(value) @api_servers = value end |
#api_title=(value) ⇒ Object (writeonly)
rubocop:disable Metrics/ClassLength
17 18 19 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 17 def api_title=(value) @api_title = value end |
#api_tos=(value) ⇒ Object (writeonly)
rubocop:disable Metrics/ClassLength
17 18 19 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 17 def api_tos=(value) @api_tos = value end |
#api_version=(value) ⇒ Object (writeonly)
rubocop:disable Metrics/ClassLength
17 18 19 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 17 def api_version=(value) @api_version = value end |
#redactables ⇒ Object (readonly)
Returns the value of attribute redactables.
18 19 20 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 18 def redactables @redactables end |
Instance Method Details
#add_security_scheme(reference, definition) ⇒ Object
Adds a security scheme definition to the API documentation
42 43 44 45 46 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 42 def add_security_scheme(reference, definition) raise "Security scheme #{reference} is already defined" if @api_security.key? reference @api_security[reference] = definition end |
#api_contact=(name: nil, email: nil, url: nil) ⇒ void
This method returns an undefined value.
Sets the contact field
113 114 115 116 117 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 113 def api_contact=(name: nil, email: nil, url: nil) @api_contact[:name] = name if name @api_contact[:email] = email if email @api_contact[:url] = url if url end |
#api_license=(name: nil, url: nil) ⇒ void
This method returns an undefined value.
Sets the license field
126 127 128 129 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 126 def api_license=(name: nil, url: nil) @api_license[:name] = name if name @api_license[:url] = url if url end |
#merge_context(context, dump_metadata: false) ⇒ void
This method returns an undefined value.
Merges example context definition with the renderer data
55 56 57 58 59 60 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 55 def merge_context(context, dump_metadata: false) @metadata[:resources].deep_merge! context.respond_to?(:resources) ? context.resources : context[:resources] # Save context for debug and fixtures File.write ::Rails.root.join('tmp', 'rra_metadata.yaml'), @metadata.to_yaml if end |
#prepare_metadata ⇒ Hash
Extracts metadata from context to generate an OpenAPI structure
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 91 def # Example: https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/petstore-expanded.yaml hash = { openapi: '3.0.0', info: @api_infos, servers: @api_servers, paths: @api_paths, components: @api_components, tags: @api_tags, } JSON.parse(hash.to_json) end |
#redact_responses(pairs) ⇒ Object
33 34 35 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 33 def redact_responses(pairs) @redactables = pairs end |
#write_files(path = nil, only: %i[yaml json])) ⇒ void
This method returns an undefined value.
Write OpenAPI files
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rspec/rails/api/open_api_renderer.rb', line 69 def write_files(path = nil, only: %i[yaml json]) return unless write_file? RSpec.world.filtered_examples path ||= ::Rails.root.join('tmp', 'rspec_api_rails') = file_types = %i[yaml json] only.each do |type| next unless file_types.include? type data = .to_yaml if type == :yaml data = JSON.pretty_generate() if type == :json File.write "#{path}.#{type}", data end end |