Skip to content

Commit 9bff442

Browse files
authored
Merge pull request #11 from drewish/formatter-output
Show path, method and status code while generating docs
2 parents b8d7d6a + d6556e3 commit 9bff442

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

lib/rspec/rails/swagger/formatter.rb

+49-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
require 'rspec/core/formatters/base_text_formatter'
1+
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
2+
RSpec::Support.require_rspec_core "formatters/console_codes"
23

34
module RSpec
45
module Rails
56
module Swagger
67
class Formatter < RSpec::Core::Formatters::BaseTextFormatter
7-
RSpec::Core::Formatters.register self, :example_finished, :close
8+
RSpec::Core::Formatters.register self, :example_group_started,
9+
:example_passed, :example_pending, :example_failed, :example_finished,
10+
:close
811

912
def documents
1013
# We don't try to load the docs in `initalize` because when running
@@ -13,22 +16,58 @@ def documents
1316
@documents ||= ::RSpec.configuration.swagger_docs
1417
end
1518

19+
def example_group_started(notification)
20+
output.print *group_output(notification)
21+
end
22+
23+
def example_passed(notification)
24+
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :success)
25+
end
26+
27+
def example_pending(notification)
28+
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :pending)
29+
end
30+
31+
def example_failed(notification)
32+
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :failure)
33+
end
34+
1635
def example_finished(notification)
17-
metadata = notification.example.metadata
36+
metadata = notification.example.metadata
1837
return unless metadata[:swagger_object] == :response
1938

20-
# metadata.each do |k, v|
21-
# puts "#{k}\t#{v}" if k.to_s.starts_with?("swagger")
22-
# end
23-
39+
# Then add everything to the document
2440
document = document_for(metadata[:swagger_document])
2541
path_item = path_item_for(document, metadata[:swagger_path_item])
2642
operation = operation_for(path_item, metadata[:swagger_operation])
27-
response_for(operation, metadata[:swagger_response])
43+
response = response_for(operation, metadata[:swagger_response])
2844
end
2945

3046
def close(_notification)
3147
documents.each{|k, v| write_json(k, v)}
48+
49+
self
50+
end
51+
52+
private
53+
54+
def group_output(notification)
55+
metadata = notification.group.metadata
56+
57+
# This is a little odd because I didn't want to split the logic across
58+
# a start and end method. Instead we just start a new line for each
59+
# path and operation and just let the status codes pile up on the end.
60+
# There's probably a better way that doesn't have the initial newline.
61+
case metadata[:swagger_object]
62+
when :path_item
63+
["\n", metadata[:swagger_path_item][:path]]
64+
when :operation
65+
["\n ", metadata[:swagger_operation][:method].to_s, "\t"]
66+
end
67+
end
68+
69+
def example_output(notification)
70+
" #{notification.example.metadata[:swagger_response][:status_code]}"
3271
end
3372

3473
def write_json(name, document)
@@ -91,9 +130,9 @@ def prepare_parameters(params)
91130
end
92131

93132
def prepare_examples(examples)
94-
if examples["application/json"].present?
133+
if examples['application/json'].kind_of? String
95134
begin
96-
examples["application/json"] = JSON.parse(examples["application/json"])
135+
examples['application/json'] = JSON.parse(examples['application/json'])
97136
rescue JSON::ParserError
98137
end
99138
end

0 commit comments

Comments
 (0)