Sha256: 3ce3a1b74a08cfd39aed91d88d18c2850c050f74dcd8912a8104fa50625dfff8

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require 'json'

module SwaggerDocsGenerator
  # # Generate JSON file
  #
  # Create a json file for swagger-ui service
  #
  # @!attribute [r] swagger_file
  #   @return [String] the swagger file name with path
  class Generator
    attr_reader :swagger_file

    def initialize
      @file = 'swagger.json'
      @path = File.join(Dir.pwd, '/public')
      @swagger_file = File.join(@path, @file)
    end

    # Open or create a swagger.json file
    def generate_swagger_file
      delete_file_before
      create_version_folder
      File.open(@swagger_file, 'a+') { |file| file.puts agregate_metadata }
    end

    # Delete files temporary
    def delete_emporary_files
      version = File.join(@path, SwaggerDocsGenerator.configure_info.version)
      Dir.delete(File.join(@path, version))
    end

    private

    attr_reader :meta

    def delete_file_before
      File.delete(@swagger_file) if File.exist?(@swagger_file)
    end

    def create_version_folder
      version = File.join(@path, SwaggerDocsGenerator.configure_info.version)
      FileUtils.mkdir_p(version) unless File.directory?(version)
    end

    # :reek:UtilityFunction
    def write_in_swagger_file
      hash = {}
      hash.merge!(MetadataConfiguration.new.construct_swagger_file)
      hash.merge!(MetadataInfo.new.construct_swagger_file)
      hash.merge!(MetadataPath.new.construct_swagger_file)
      hash.merge!(MetadataTag.new.construct_swagger_file)
    end

    def agregate_metadata
      case defined?(Rails) && Rails.env
      when 'production' || 'test'
        write_in_swagger_file.to_json
      else
        JSON.pretty_generate write_in_swagger_file
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
swagger_docs_generator-0.1.0 lib/swagger_docs_generator/generator.rb