Sha256: bb2c4a468c53d79d0b70a84076a109c1847a5a364b49b122e47f072140177130

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

Contents

module Swaggard
  module Parsers
    class Routes

      def run(routes)
        return {} unless routes

        parsed_routes = {}

        routes.each do |route|
          controller = route_controller(route)
          action = route_action(route)

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

        parsed_routes
      end

      private

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

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

      def route_verb(route)
        route.verb.source.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

7 entries across 7 versions & 1 rubygems

Version Path
swaggard-0.4.0 lib/swaggard/parsers/routes.rb
swaggard-0.3.0 lib/swaggard/parsers/routes.rb
swaggard-0.2.1 lib/swaggard/parsers/routes.rb
swaggard-0.2.0 lib/swaggard/parsers/routes.rb
swaggard-0.1.1 lib/swaggard/parsers/routes.rb
swaggard-0.1.0 lib/swaggard/parsers/routes.rb
swaggard-0.0.4 lib/swaggard/parsers/routes.rb