Sha256: b035612293fa9f5b57705f1c53d3f965bfb36eb20546da60adaa4886aa91fcd0

Contents?: true

Size: 977 Bytes

Versions: 1

Compression:

Stored size: 977 Bytes

Contents

# frozen_string_literal: true

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

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

    def path
      router do |route|
        route.path.spec.to_s.gsub('(.:format)', '.json').gsub(':id', '{id}')
      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_name
    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

1 entries across 1 versions & 1 rubygems

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