Sha256: c2d362a6b764e51c4c4cedb35360c71285925d10bb17cd8024895c3a32c5ec22

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

# :reek:NestedIterators

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
      router do |route|
        route.path.spec.to_s.gsub('(.:format)',
                                  '.json').gsub(/:[a-z1-9_A-Z]*/) do |word|
          "{#{word.delete(':')}}"
        end
      end
    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
swagger_docs_generator-0.3.5 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.3.5.pre.31 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.3.4.pre.26 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.3.4 lib/swagger_docs_generator/extractor.rb
swagger_docs_generator-0.3.4.pre.23 lib/swagger_docs_generator/extractor.rb