Class: Swagalicious::SwaggerFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/swagalicious/swagger_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(output, config = nil) ⇒ SwaggerFormatter

Returns a new instance of SwaggerFormatter.



13
14
15
16
17
18
# File 'lib/swagalicious/swagger_formatter.rb', line 13

def initialize(output, config = nil)
  @output = output
  @config = config

  @output.puts "Generating Swagger docs ..."
end

Instance Method Details

#configObject



9
10
11
# File 'lib/swagalicious/swagger_formatter.rb', line 9

def config
  @config ||= Swagalicious.config
end

#merge_metadata_to_document(doc, example) ⇒ Object



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

def (doc, example)
    = example.
   # !metadata[:document] won"t work, since nil means we should generate
   # docs.
   return {} if [:document] == false
   return {} unless .key?(:response)
   # This is called multiple times per file!
   # metadata[:operation] is also re-used between examples within file
   # therefore be careful NOT to modify its content here.
   upgrade_servers!(doc)
   upgrade_oauth!(doc)
   upgrade_response_produces!(doc, )
   upgrade_request_type!()

   doc.deep_merge!(())
end

#stop(notification = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/swagalicious/swagger_formatter.rb', line 37

def stop(notification = nil)
  config.swagger_docs.each do |url_path, doc|
    examples = notification.examples.select { |e| e.[:swagger_doc] == url_path }

    merged_doc = examples.each_with_object(doc) { |e, doc| doc = doc.deep_merge!((doc, e)) }

    file_path = File.join(config.swagger_root, url_path)
    dirname   = File.dirname(file_path)
    FileUtils.mkdir_p dirname unless File.exist?(dirname)

    File.open(file_path, "w") do |file|
      file.write(pretty_generate(merged_doc.except(:metadata)))
    end

    @output.puts "Swagger doc generated at #{file_path}"
  end
end