Class: Openapi::Rswag::Specs::SwaggerFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi/rswag/specs/swagger_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(output, config = Rswag::Specs.config) ⇒ SwaggerFormatter

Returns a new instance of SwaggerFormatter.



14
15
16
17
18
19
# File 'lib/openapi/rswag/specs/swagger_formatter.rb', line 14

def initialize(output, config = Rswag::Specs.config)
  @output = output
  @config = config
  
  @output.puts 'Generating Swagger docs ...'
end

Instance Method Details

#example_group_finished(notification) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openapi/rswag/specs/swagger_formatter.rb', line 21

def example_group_finished(notification)
  # NOTE: rspec 2.x support
  if RSPEC_VERSION > 2
     = notification.group.
  else
     = notification.
  end
  
  # !metadata[:document] won't work, since nil means we should generate
  # docs.
  return if [:document] == false
  return unless .has_key?(:response)
  
  swagger_doc = @config.get_swagger_doc([:swagger_doc])
  swagger_doc.deep_merge!(())
end

#stop(notification = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/openapi/rswag/specs/swagger_formatter.rb', line 38

def stop(notification=nil)
  @config.swagger_docs.each do |url_path, doc|
    file_path = File.join(@config.swagger_root, url_path)
    dirname = File.dirname(file_path)
    FileUtils.mkdir_p dirname unless File.exists?(dirname)
  
    File.open(file_path, 'w') do |file|
      file.write(pretty_generate(doc))
    end
  
    @output.puts "Swagger doc generated at #{file_path}"
  end
end