Class: RSpec::OpenAPI::SchemaFile

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

Overview

TODO: Support JSON

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SchemaFile

Returns a new instance of SchemaFile.

Parameters:

  • path (String)


13
14
15
# File 'lib/rspec/openapi/schema_file.rb', line 13

def initialize(path)
  @path = path
end

Instance Method Details

#edit(&block) ⇒ Hash

Reads the existing spec, lets the block mutate it, writes the result back and returns the (symbolized) spec so it can be mirrored to other files.

Returns:

  • (Hash)


20
21
22
23
24
25
26
# File 'lib/rspec/openapi/schema_file.rb', line 20

def edit(&block)
  spec = read
  block.call(spec)
  spec
ensure
  write(spec)
end

#write(spec) ⇒ Object

Writes an already-built spec to this file, choosing the format from the file extension.

Parameters:

  • spec (Hash)


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

def write(spec)
  stringified = RSpec::OpenAPI::KeyTransformer.stringify(spec)
  FileUtils.mkdir_p(File.dirname(@path))
  output =
    if json?
      JSON.pretty_generate(stringified)
    else
      prepend_comment(YAML.dump(stringified))
    end
  File.write(@path, output)
end