Class: RSpec::OpenAPI::ResultRecorder

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

Instance Method Summary collapse

Constructor Details

#initialize(path_records) ⇒ ResultRecorder

Returns a new instance of ResultRecorder.



4
5
6
7
# File 'lib/rspec/openapi/result_recorder.rb', line 4

def initialize(path_records)
  @path_records = path_records
  @error_records = {}
end

Instance Method Details

#error_messageObject



42
43
44
45
46
47
48
# File 'lib/rspec/openapi/result_recorder.rb', line 42

def error_message
  <<~ERR_MSG
    RSpec::OpenAPI got errors building #{@error_records.size} requests

    #{@error_records.map { |e, record| "#{e.inspect}: #{record.inspect}" }.join("\n")}
  ERR_MSG
end

#errors?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rspec/openapi/result_recorder.rb', line 38

def errors?
  @error_records.any?
end

#record_results!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec/openapi/result_recorder.rb', line 9

def record_results!
  @path_records.each do |path, records|
    # Look for a path-specific config file and run it.
    config_file = File.join(File.dirname(path), RSpec::OpenAPI.config_filename)
    begin
      require config_file if File.exist?(config_file)
    rescue StandardError => e
      puts "WARNING: Unable to load #{config_file}: #{e}"
    end

    title = RSpec::OpenAPI.title
    RSpec::OpenAPI::SchemaFile.new(path).edit do |spec|
      schema = RSpec::OpenAPI::DefaultSchema.build(title)
      schema[:info].merge!(RSpec::OpenAPI.info)
      RSpec::OpenAPI::SchemaMerger.merge!(spec, schema)
      new_from_zero = {}
      records.each do |record|
        record_schema = RSpec::OpenAPI::SchemaBuilder.build(record)
        RSpec::OpenAPI::SchemaMerger.merge!(spec, record_schema)
        RSpec::OpenAPI::SchemaMerger.merge!(new_from_zero, record_schema)
      rescue StandardError, NotImplementedError => e # e.g. SchemaBuilder raises a NotImplementedError
        @error_records[e] = record # Avoid failing the build
      end
      cleanup_schema!(new_from_zero, spec)
      execute_post_process_hook(path, records, spec)
    end
  end
end