Sha256: 71d7d6a3366e687dd7d5b77ff9cb54a7cf189564d5f16c92a088f41aa83dcb33

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

require 'active_support/core_ext/hash/deep_merge'
require 'swagger_helper'

module Rswag3
  module Specs
    class SwaggerFormatter

      # NOTE: rspec 2.x support
      if RSPEC_VERSION > 2
        ::RSpec::Core::Formatters.register self, :example_group_finished, :stop
      end

      def initialize(output, config = Rswag3::Specs.config)
        @output = output
        @config = config

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

      def example_group_finished(notification)
        # NOTE: rspec 2.x support
        if RSPEC_VERSION > 2
          metadata = notification.group.metadata
        else
          metadata = notification.metadata
        end

        return unless metadata.has_key?(:response)
        swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
        swagger_doc.deep_merge!(metadata_to_swagger(metadata))
      end

      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(JSON.pretty_generate(doc))
          end

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

      private

      def metadata_to_swagger(metadata)
        response_code = metadata[:response][:code]
        response = metadata[:response].reject { |k,v| k == :code }

        verb = metadata[:operation][:verb]
        operation = metadata[:operation]
          .reject { |k,v| k == :verb }
          .merge(responses: { response_code => response })

        path_template = metadata[:path_item][:template]
        path_item = metadata[:path_item]
          .reject { |k,v| k == :template }
          .merge(verb => operation)

        { paths: { path_template => path_item } }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rswag3-specs-0.0.2 lib/rswag3/specs/swagger_formatter.rb
rswag3-specs-0.0.1 lib/rswag3/specs/swagger_formatter.rb