Sha256: 070085248756db5850b46a2c389501ff26468e3c779238a0189bdbbdcf64b52a

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

# :reek:NestedIterators
# :reek:TooManyStatements
# :reek:UtilityFunction

module SwaggerDocsGenerator
  # # Extractor routes info
  #
  # Give information about routes
  class Extractor
    def initialize(controller, action)
      @action = action
      @controller = controller
      @routes = Rails.application.routes.routes
    end

    # Extract verb to routes
    def verb
      router do |route|
        route.verb.source.to_s.delete('$' + '^')
      end
    end

    # Extract path to routes and change format to parameter path
    def path
      temporary = []
      actual_route = nil
      router do |route|
        actual_route = extract_and_format_route(route)
        temporary.push(actual_route) unless temporary.include?(actual_route)
        actual_route
      end
      temporary
    end

    private

    def rte_controller(rte)
      rte[:controller].eql?(controller_name)
    end

    def rte_action(rte)
      rte[:action].eql?(@action.to_s)
    end

    def controller_name
      @controller.controller_path
    end

    def router
      data = nil
      @routes.map do |route|
        rte = route.defaults
        data = yield(route, rte) if rte_controller(rte) && rte_action(rte)
      end
      data.downcase
    end

    def extract_and_format_route(route)
      route.path.spec.to_s.gsub('(.:format)',
                                '.json').gsub(/:[a-z1-9_A-Z]*/) do |word|
        "{#{word.delete(':')}}"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
swagger_docs_generator-0.5.1 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.5.0.pre.42 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.4.0 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.3.6 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.3.6.pre.34 lib/swagger_docs_generator/extractor.rb