Sha256: 76058335df0c8ac88992281cece9ea3a45d4cc7c6e210a46a5454b3b5a0491b4

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

namespace :praxis do
  desc 'List routes, format=json or table, default table'
  task :routes, [:format] => [:environment] do |_t, args|
    require 'terminal-table'

    table = Terminal::Table.new title: 'Routes',
                                headings: %w[
                                  Version Path Verb
                                  Endpoint Action Implementation Options
                                ]

    rows = []
    Praxis::Application.instance.endpoint_definitions.each do |endpoint_definition|
      endpoint_definition.actions.each do |name, action|
        method = begin
          endpoint_definition.controller.instance_method(name)
        rescue StandardError
          nil
        end

        method_name = method ? "#{method.owner.name}##{method.name}" : 'n/a'

        row = {
          resource: endpoint_definition.name,
          action: name,
          implementation: method_name
        }

        if action.route
          route = action.route
          rows << row.merge({
                              version: route.version,
                              verb: route.verb,
                              path: route.path,
                              options: route.options
                            })
        else
          warn "Warning: No routes defined for #{endpoint_definition.name}##{name}."
          rows << row
        end
      end
    end
    format_type = args[:format] || 'table'
    case format_type
    when 'json'
      puts JSON.pretty_generate(rows)
    when 'table'
      rows.each do |row|
        formatted_options = row[:options].map { |(k, v)| "#{k}:#{v}" }.join("\n")
        row_data = row.values_at(:version, :path, :verb, :resource,
                                 :action, :implementation)
        row_data << formatted_options
        table.add_row(row_data)
      end
      puts table
    else
      raise "unknown output format: #{args[:format]}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
praxis-2.0.pre.24 lib/praxis/tasks/routes.rb
praxis-2.0.pre.23 lib/praxis/tasks/routes.rb
praxis-2.0.pre.22 lib/praxis/tasks/routes.rb
praxis-2.0.pre.21 lib/praxis/tasks/routes.rb
praxis-2.0.pre.20 lib/praxis/tasks/routes.rb
praxis-2.0.pre.19 lib/praxis/tasks/routes.rb