Sha256: dfc9c9f8c9e147c92f39d7618400a7c35c9bfb20724ae48e4cee8ac8e40fc98d

Contents?: true

Size: 1.6 KB

Versions: 15

Compression:

Stored size: 1.6 KB

Contents

module OasRails
  class OasRoute
    attr_accessor(:controller_class, :controller_action, :controller, :controller_path, :method, :verb, :path,
                  :rails_route, :docstring, :source_string)

    def initialize; end

    def self.new_from_rails_route(rails_route: ActionDispatch::Journey::Route)
      instance = new
      instance.rails_route = rails_route
      instance.extract_rails_route_data
      instance
    end

    def extract_rails_route_data
      @controller_action = "#{@rails_route.defaults[:controller].camelize}Controller##{@rails_route.defaults[:action]}"
      @controller_class = "#{@rails_route.defaults[:controller].camelize}Controller"
      @controller = @rails_route.defaults[:controller]
      @controller_path = controller_path_extractor(@rails_route.defaults[:controller])
      @method = @rails_route.defaults[:action]
      @verb = @rails_route.verb
      @path = Extractors::RouteExtractor.clean_route(@rails_route.path.spec.to_s)
      @docstring = extract_docstring
      @source_string = extract_source_string
    end

    def extract_docstring
      ::YARD::Docstring.parser.parse(
        controller_class.constantize.instance_method(method).comment.lines.map { |line| line.sub(/^#\s*/, '') }.join
      ).to_docstring
    end

    def extract_source_string
      @controller_class.constantize.instance_method(method).source
    end

    def path_params
      @rails_route.path.spec.to_s.scan(/:(\w+)/).flatten.reject! { |e| e == 'format' }
    end

    def controller_path_extractor(controller)
      Rails.root.join("app/controllers/#{controller}_controller.rb").to_s
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
oas_rails-0.9.0 lib/oas_rails/oas_route.rb
oas_rails-0.8.4 lib/oas_rails/oas_route.rb
oas_rails-0.8.3 lib/oas_rails/oas_route.rb
oas_rails-0.8.2 lib/oas_rails/oas_route.rb
oas_rails-0.8.1 lib/oas_rails/oas_route.rb
oas_rails-0.8.0 lib/oas_rails/oas_route.rb
oas_rails-0.7.0 lib/oas_rails/oas_route.rb
oas_rails-0.6.0 lib/oas_rails/oas_route.rb
oas_rails-0.5.0 lib/oas_rails/oas_route.rb
oas_rails-0.4.5 lib/oas_rails/oas_route.rb
oas_rails-0.4.4 lib/oas_rails/oas_route.rb
oas_rails-0.4.3 lib/oas_rails/oas_route.rb
oas_rails-0.4.2 lib/oas_rails/oas_route.rb
oas_rails-0.4.1 lib/oas_rails/oas_route.rb
oas_rails-0.4.0 lib/oas_rails/oas_route.rb