Sha256: 8ae012575f3bb45091d31ba20e191f9050507dc6fc8c857127eb8d9237c1ab5c

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require 'swagger_docs_generator/parser/actions/actions'

module SwaggerDocsGenerator
  # # Parse action in controller classe to Rails application. It's adding
  # paths to swagger docs file.
  class ParserAction < Parser
    def initialize(controller, action, data)
      super(controller)
      @action = action
      @data = data
    end

    def adding_path
      json = JSON.parse(File.read(controller_file))
      File.open(controller_file, 'w') do |file|
        path_exist(json, construct_routes)
        file.puts(JSON.pretty_generate(json))
      end
    end

    private

    # :reek:UtilityFunction
    def path_exist(json, hash)
      old_route = json['paths']

      keys_new = hash.keys[0]
      keys_old = old_route.keys[0]

      if keys_new.to_s.eql?(keys_old.to_s)
        old_route[keys_old].merge!(hash[keys_new])
      else
        old_route.merge!(hash)
      end
    end

    def construct_routes
      extract = Extractor.new(controller, @action)
      verb = extract.verb
      path = extract.path
      verb.eql?('put') ? route_update(path, verb) : route(path, verb)
    end

    def super_hash
      hash = {}
      SwaggerDocsGenerator::Actions::Actions.descendants.each do |klass|
        hash.merge!(klass.new(expect_tag(klass)).hash)
      end
      hash
    end

    def expect_tag(klass)
      klass.eql?(Actions::Tags) ? controller_name : @data
    end

    def route(path, verb)
      { "#{path}": { "#{verb}": super_hash } }
    end

    def route_update(path, verb)
      { "#{path}": { "#{verb}": super_hash }.merge!(patch: super_hash) }
    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/parser/action.rb