lib/enroute/routes.rb in enroute-0.0.1 vs lib/enroute/routes.rb in enroute-0.0.2
- old
+ new
@@ -1,11 +1,19 @@
# frozen_string_literal: true
module Enroute
- module Routes
- extend self
+ class Routes
+ attr_reader :config
+ def self.call(config = {})
+ new(config).call
+ end
+
+ def initialize(config)
+ @config = config
+ end
+
def call
grouped_routes.each_with_object([]) do |(_pattern, routes), buffer|
route = routes.find {|r| r.name.present? }
next unless route
@@ -15,16 +23,16 @@
end
def build_payload(route)
{
name: route.name.camelize(:lower),
- typeName: "#{route.name.camelize}RouteHandler",
incomingPattern: camelize_pattern(route),
outgoingPattern: route.ast.to_s,
method: reduce_methods(routes),
segments: route.segments,
- requiredSegments: route.path.required_names
+ requiredSegments: route.path.required_names,
+ typings: config.dig(:typings, route.name) || {}
}
end
def camelize_pattern(route)
route
@@ -46,11 +54,27 @@
route.ast.to_s
end
end
def filtered_routes
- routes.reject do |route|
- route.name =~ /rails|script/
+ 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
def routes
Rails.application.routes.routes