Sha256: ba38668c05b72e414046cbe62336f52eb51eb7edde428f98007779c02736b28a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require "csv"

module Hanami
  class Router
    # Renders a CSV representation of the routes
    #
    # You can forward [CSV generation
    # options](https://ruby-doc.org/stdlib-3.1.0/libdoc/csv/rdoc/CSV.html#class-CSV-label-Options+for+Generating]
    # when calling it:
    #
    # ```
    # require "hanami/router/inspector"
    # require "hanami/router/formatter/csv"
    #
    # Hanami::Router::Inspector.new(
    #   routes: Router.routes,
    #   formatter: Hanami::Router::Formatter::CSV.new
    # ).call(write_headers: false)
    # ```
    #
    # @api private
    # @since 2.0.0
    module Formatter
      class CSV
        # @api private
        # @since 2.0.0
        DEFAULT_OPTIONS = {
          write_headers: true
        }.freeze

        # @api private
        # @since 2.0.0
        HEADERS = %w[METHOD PATH TO AS CONSTRAINTS].freeze

        # @api private
        # @since 2.0.0
        def call(routes, **csv_opts)
          ::CSV.generate(**DEFAULT_OPTIONS, **csv_opts) do |csv|
            csv << HEADERS if csv.write_headers?
            routes.reduce(csv) do |acc, route|
              route.head? ? acc : acc << row(route)
            end
          end
        end

        private

        def row(route)
          [
            route.http_method.to_s,
            route.path,
            route.inspect_to,
            route.as? ? route.inspect_as : "",
            route.constraints? ? route.inspect_constraints : ""
          ]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-router-2.2.0 lib/hanami/router/formatter/csv.rb