Skip to content

Show path, method and status code while generating docs #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions lib/rspec/rails/swagger/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
require 'rspec/core/formatters/base_text_formatter'
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
RSpec::Support.require_rspec_core "formatters/console_codes"

module RSpec
module Rails
module Swagger
class Formatter < RSpec::Core::Formatters::BaseTextFormatter
RSpec::Core::Formatters.register self, :example_finished, :close
RSpec::Core::Formatters.register self, :example_group_started,
:example_passed, :example_pending, :example_failed, :example_finished,
:close

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

def example_group_started(notification)
output.print *group_output(notification)
end

def example_passed(notification)
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :success)
end

def example_pending(notification)
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :pending)
end

def example_failed(notification)
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :failure)
end

def example_finished(notification)
metadata = notification.example.metadata
metadata = notification.example.metadata
return unless metadata[:swagger_object] == :response

# metadata.each do |k, v|
# puts "#{k}\t#{v}" if k.to_s.starts_with?("swagger")
# end

# Then add everything to the document
document = document_for(metadata[:swagger_document])
path_item = path_item_for(document, metadata[:swagger_path_item])
operation = operation_for(path_item, metadata[:swagger_operation])
response_for(operation, metadata[:swagger_response])
response = response_for(operation, metadata[:swagger_response])
end

def close(_notification)
documents.each{|k, v| write_json(k, v)}

self
end

private

def group_output(notification)
metadata = notification.group.metadata

# This is a little odd because I didn't want to split the logic across
# a start and end method. Instead we just start a new line for each
# path and operation and just let the status codes pile up on the end.
# There's probably a better way that doesn't have the initial newline.
case metadata[:swagger_object]
when :path_item
["\n", metadata[:swagger_path_item][:path]]
when :operation
["\n ", metadata[:swagger_operation][:method].to_s, "\t"]
end
end

def example_output(notification)
" #{notification.example.metadata[:swagger_response][:status_code]}"
end

def write_json(name, document)
Expand Down Expand Up @@ -91,9 +130,9 @@ def prepare_parameters(params)
end

def prepare_examples(examples)
if examples["application/json"].present?
if examples['application/json'].kind_of? String
begin
examples["application/json"] = JSON.parse(examples["application/json"])
examples['application/json'] = JSON.parse(examples['application/json'])
rescue JSON::ParserError
end
end
Expand Down