Sha256: 9e0f92bdd31b14a7e99f3348984c526aa8a2176df1dfb4abd43ed2ad452d30b2

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

module Swaggard
  module Parsers
    class Routes

      def run(routes)
        return {} unless routes

        routes.inject({}) do |parsed_routes, route|
          path = route_path(route)

          unless Swaggard.configuration.excluded_paths.any? { |excluded_path| Regexp.new(excluded_path) =~ path }
            verb = route_verb(route)

            parsed_routes[path] ||= {}
            parsed_routes[path][verb] = {
              controller:   route_controller(route),
              action:       route_action(route),
              path_params:  route_path_params(route)
            }
          end

          parsed_routes
        end.sort.to_h
      end

      private

      def route_controller(route)
        route.requirements[:controller]
      end

      def route_action(route)
        route.requirements[:action]
      end

      def route_verb(route)
        verb = route.verb
        verb = route.verb.source unless verb.is_a?(String)

        verb.gsub(/[$^]/, '')
      end

      def route_path(route)
        path = route.path.spec.to_s

        path.gsub!('(.:format)', '')
        route.required_parts.each { |part| path.gsub!(":#{part}", "{#{part}}") }

        path
      end

      def route_path_params(route)
        route.required_parts
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
swaggard-1.5.1 lib/swaggard/parsers/routes.rb
swaggard-1.5.0 lib/swaggard/parsers/routes.rb
swaggard-1.4.0 lib/swaggard/parsers/routes.rb
swaggard-1.3.0 lib/swaggard/parsers/routes.rb
swaggard-1.2.0 lib/swaggard/parsers/routes.rb
swaggard-1.1.1 lib/swaggard/parsers/routes.rb
swaggard-1.1.0 lib/swaggard/parsers/routes.rb
swaggard-1.0.2 lib/swaggard/parsers/routes.rb
swaggard-1.0.1 lib/swaggard/parsers/routes.rb
swaggard-1.0.0 lib/swaggard/parsers/routes.rb