class Enroute::Routes

Attributes

config[R]

Public Class Methods

call(config = {}) click to toggle source
# File lib/enroute/routes.rb, line 7
def self.call(config = {})
  new(config).call
end
new(config) click to toggle source
# File lib/enroute/routes.rb, line 11
def initialize(config)
  @config = config
end

Public Instance Methods

build_payload(route) click to toggle source
# File lib/enroute/routes.rb, line 25
def build_payload(route)
  {
    name: route.name.camelize(:lower),
    incomingPattern: camelize_pattern(route),
    outgoingPattern: route.ast.to_s,
    method: reduce_methods(routes),
    segments: route.segments,
    requiredSegments: route.path.required_names,
    typings: config.dig(:typings, route.name) || {}
  }
end
call() click to toggle source
# File lib/enroute/routes.rb, line 15
def call
  grouped_routes.each_with_object([]) do |(_pattern, routes), buffer|
    route = routes.find {|r| r.name.present? }

    next unless route

    buffer << build_payload(route)
  end
end
camelize_map(list) click to toggle source
# File lib/enroute/routes.rb, line 48
def camelize_map(list)
  list.map {|item| item.camelize(:lower) }
end
camelize_pattern(route) click to toggle source
# File lib/enroute/routes.rb, line 37
def camelize_pattern(route)
  route
    .ast
    .to_s
    .gsub(/_(.)/) { Regexp.last_match(1).upcase }
end
filtered_routes() click to toggle source
# File lib/enroute/routes.rb, line 58
def filtered_routes
  only_conditions = config.fetch(:only, [])

  # If `:only` has at least one item, then select matching routes.
  # Otherwise, use all routes.
  selected_routes = if only_conditions.empty?
                      routes
                    else
                      routes.select do |route|
                        only_conditions.include?(route.name)
                      end
                    end

  # Filter out unnamed routes, Rails' internal routes, and anything present
  # on `:ignore`.
  selected_routes.reject do |route|
    route.name.nil? ||
      route.name.match?(/rails|script/) ||
      config.fetch(:ignore, []).include?(route.name)
  end
end
grouped_routes() click to toggle source
# File lib/enroute/routes.rb, line 52
def grouped_routes
  filtered_routes.group_by do |route|
    route.ast.to_s
  end
end
reduce_methods(routes) click to toggle source
# File lib/enroute/routes.rb, line 44
def reduce_methods(routes)
  routes.map(&:verb).flatten.map(&:downcase).uniq
end
routes() click to toggle source
# File lib/enroute/routes.rb, line 80
def routes
  Rails.application.routes.routes
end