Sha256: f411658b87d87ad73efcebc3a983828bba643122873f062ef3813b484648b3a6

Contents?: true

Size: 1.91 KB

Versions: 14

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'swagger_docs_generator/parser/model'

module SwaggerDocsGenerator
  # # Metadata generated
  #
  # Generate metadata for block definition in swagger file
  #
  # @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#definitions-object
  class MetadataDefinition < Metadata
    def initialize
      super
      @model = nil
    end

    def construct_swagger_file
      { definitions: search_definition }
    end

    private

    def search_definition
      find_models.merge!(find_in_jsons)
    end

    def find_models
      hash = {}
      all_class_documentation.each do |controller|
        data = each_controller(controller::CONTROLLER)
        hash.merge!(data) unless data.empty?
      end
      hash
    end

    def find_in_jsons
      hash = {}
      files_tmp.each do |file|
        hash.merge!(read_part_json(file, 'definitions'))
      end
      hash
    end

    # :reek:UtilityFunction
    def read_file(file)
      json = JSON.parse(File.read(file))
      json.key?('definitions') ? json['definitions'] : {}
    end

    def each_controller(model_name)
      read_model(model_name) || {}
    end

    def read_model(model_name)
      @model = Model.new(model_name)
      contruct_hash
    rescue NameError => message
      puts "-> [Model] #{message.name} -- doesn't exist"
    end

    def contruct_hash
      {
        @model.name => {
          type: 'object',
          properties: @model.attribute_properties
        }
      }
    end

    # :reek:UtilityFunction
    def temporary_file(controller)
      File.join(SwaggerDocsGenerator.temporary_folder,
                "#{controller.controller_name}.json")
    end

    # :reek:UtilityFunction
    def files_tmp
      Dir[Rails.root.join(SwaggerDocsGenerator.temporary_folder, '*.json')]
    end

    # :reek:UtilityFunction
    def read_part_json(file, key)
      JSON.parse(File.read(file))[key]
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
swagger_docs_generator-0.5.1 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.5.0.pre.42 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.4.0 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.6 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.6.pre.34 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.5 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.5.pre.31 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.4.pre.26 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.4 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.4.pre.23 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.3 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.2 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.1 lib/swagger_docs_generator/metadata/definition.rb
swagger_docs_generator-0.3.0.pre.19 lib/swagger_docs_generator/metadata/definition.rb