Class: Enroute::Routes
- Inherits:
-
Object
- Object
- Enroute::Routes
- Defined in:
- lib/enroute/routes.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #build_payload(route) ⇒ Object
- #call ⇒ Object
- #camelize_map(list) ⇒ Object
- #camelize_pattern(route) ⇒ Object
- #filtered_routes ⇒ Object
- #grouped_routes ⇒ Object
-
#initialize(config) ⇒ Routes
constructor
A new instance of Routes.
- #reduce_methods(routes) ⇒ Object
- #routes ⇒ Object
Constructor Details
#initialize(config) ⇒ Routes
Returns a new instance of Routes.
11 12 13 |
# File 'lib/enroute/routes.rb', line 11 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/enroute/routes.rb', line 5 def config @config end |
Class Method Details
.call(config = {}) ⇒ Object
7 8 9 |
# File 'lib/enroute/routes.rb', line 7 def self.call(config = {}) new(config).call end |
Instance Method Details
#build_payload(route) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# 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 ⇒ Object
15 16 17 18 19 20 21 22 23 |
# 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) ⇒ Object
48 49 50 |
# File 'lib/enroute/routes.rb', line 48 def camelize_map(list) list.map {|item| item.camelize(:lower) } end |
#camelize_pattern(route) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/enroute/routes.rb', line 37 def camelize_pattern(route) route .ast .to_s .gsub(/_(.)/) { Regexp.last_match(1).upcase } end |
#filtered_routes ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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|turbo/) || config.fetch(:ignore, []).include?(route.name) end end |
#grouped_routes ⇒ Object
52 53 54 55 56 |
# 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) ⇒ Object
44 45 46 |
# File 'lib/enroute/routes.rb', line 44 def reduce_methods(routes) routes.map(&:verb).flatten.map(&:downcase).uniq.reject(&:empty?) end |
#routes ⇒ Object
80 81 82 |
# File 'lib/enroute/routes.rb', line 80 def routes Rails.application.routes.routes end |