Class: SharedExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/openapi/extractors/shared_extractor.rb

Overview

Shared extractor for extracting OpenAPI metadata from RSpec examples

Constant Summary collapse

VALID_EXAMPLE_MODES =
i[none single multiple].freeze

Class Method Summary collapse

Class Method Details

.attributes(example) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 7

def self.attributes(example)
   = (example.)
  summary = [:summary] || RSpec::OpenAPI.summary_builder.call(example)
  tags = [:tags] || RSpec::OpenAPI.tags_builder.call(example)
  formats = [:formats] || RSpec::OpenAPI.formats_builder.curry.call(example)
  operation_id = [:operation_id]
  required_request_params = [:required_request_params] || []
  security = [:security]
  description = [:description] || RSpec::OpenAPI.description_builder.call(example)
  deprecated = [:deprecated]
  example_mode = normalize_example_mode([:example_mode], example)
  example_name = [:example_name] || RSpec::OpenAPI.example_name_builder.call(example)
  raw_example_key = [:example_key] || example_name
  example_key = RSpec::OpenAPI::ExampleKey.normalize(raw_example_key)
  example_key = 'default' if example_key.nil? || example_key.empty?

  [summary, tags, formats, operation_id, required_request_params, security, description, deprecated, example_mode,
   example_key, example_name,]
end

.collect_openapi_metadata(metadata) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 31

def self.()
  [].tap do |result|
    current = 

    while current
      [current[:example_group], current].each do |meta|
        result.unshift(meta[:openapi]) if meta&.dig(:openapi)
      end

      current = current[:parent_example_group]
    end
  end
end

.example_mode_error(value, example) ⇒ Object



56
57
58
59
60
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 56

def self.example_mode_error(value, example)
  context = example&.full_description
  context = " (example: #{context})" if context
  "example_mode must be one of #{VALID_EXAMPLE_MODES.inspect}, got #{value.inspect}#{context}"
end

.merge_openapi_metadata(metadata) ⇒ Object



27
28
29
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 27

def self.()
  ().reduce({}, &:merge)
end

.normalize_example_mode(value, example = nil) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 45

def self.normalize_example_mode(value, example = nil)
  return :single if value.nil?

  raise ArgumentError, example_mode_error(value, example) unless value.is_a?(String) || value.is_a?(Symbol)

  mode = value.to_s.strip.downcase.to_sym
  return mode if VALID_EXAMPLE_MODES.include?(mode)

  raise ArgumentError, example_mode_error(value, example)
end