Class: Rails::Openapi::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/openapi/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix = [], parent = nil) ⇒ Router

Returns a new instance of Router.



35
36
37
38
39
40
41
42
# File 'lib/rails/openapi/router.rb', line 35

def initialize prefix = [], parent = nil
  @parent = parent
  @prefix = prefix.freeze
  @endpoints = []
  @subroutes = Hash.new do |hash, k|
    hash[k] = Router.new(@prefix + [k], self)
  end
end

Instance Attribute Details

#endpointsObject

Returns the value of attribute endpoints.



33
34
35
# File 'lib/rails/openapi/router.rb', line 33

def endpoints
  @endpoints
end

Instance Method Details

#<<(route) ⇒ Object

Adds an individual endpoint to the routing tree



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails/openapi/router.rb', line 45

def << route
  raise "Argument must be an Endpoint" unless Endpoint === route
  _base, *subroute = route[:_path].split "/" # Split out first element
  if subroute.count == 0
    route[:_path] = ""
    @endpoints << route
  else
    route[:_path] = subroute.join "/"
    self[subroute[0]] << route
  end
end

#[](path) ⇒ Object

Returns a specific branch of the routing tree



58
59
60
# File 'lib/rails/openapi/router.rb', line 58

def [] path
  @subroutes[path]
end

#_debug_routing_treeObject

Outputs a visual representation of the routing tree



173
174
175
176
177
178
179
# File 'lib/rails/openapi/router.rb', line 173

def _debug_routing_tree
  puts path + " - #{route_mode}"
  @endpoints.each do |route|
    puts "\t#{route[:method].to_s.upcase} to ##{action_for route} (#{action_mode})"
  end
  @subroutes.each { |k, subroute| subroute._debug_routing_tree }
end

#action_for(route) ⇒ Object

Determines the action for a specific route



86
87
88
89
90
91
92
# File 'lib/rails/openapi/router.rb', line 86

def action_for route
  raise "Argument must be an Endpoint" unless Endpoint === route
  action = @prefix[-1]&.underscore || ""
  action = PARAM_ROUTES[route[:method]] if action_mode == :member
  action = RESOURCE_ROUTES[route[:method]] if route_mode == :resource && action_mode == :collection
  action
end

#action_modeObject

Returns the mode used for actions in this router



77
78
79
80
81
82
83
# File 'lib/rails/openapi/router.rb', line 77

def action_mode
  if /^:/.match?(@prefix[-1])
    :member
  else
    :collection
  end
end

#draw(map) ⇒ Object

Draws the routes for this router



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rails/openapi/router.rb', line 95

def draw map
  case route_mode
  when :resource

    # Find collection-level resource actions
    actions = @endpoints.map { |r| action_for r }.select { |a| Symbol === a }

    # Find parameter-level resource actions
    @subroutes.select { |k, _| /^:/ === k }.values.each do |subroute|
      actions += subroute.endpoints.map { |r| subroute.action_for r }.select { |a| Symbol === a }
    end

    # Determine if this is a collection or a singleton resource
    type = @subroutes.any? { |k, _| /^:/ === k } ? :resources : :resource
    if type == :resource && actions.include?(:index)
      # Remap index to show for singleton resources
      actions.delete :index
      actions << :show
    end

    # Draw the resource
    map.send type, @prefix.last&.to_sym || "/", controller: @prefix.last&.underscore || "main", only: actions, as: @prefix.last&.underscore || "main", format: nil do
      # Draw custom actions
      draw_actions! map

      # Handle the edge case in which POST is used instead of PUT/PATCH
      draw_post_updates! map

      # Draw a namespace (unless at the top)
      if @prefix.join("/").blank?
        draw_subroutes! map
      else
        map.scope module: @prefix.last do
          draw_subroutes! map
        end
      end
    end

  when :namespace

    # Draw a namespace (unless at the top)
    if @prefix.join("/").blank?
      draw_subroutes! map
    else
      map.namespace @prefix.last do
        draw_subroutes! map
      end
    end

  when :param

    # Draw subroutes directly
    draw_subroutes! map
    draw_actions! map

  when :action
    # Draw actions directly
    draw_actions! map

  end
end

#pathObject

Returns the routing path



63
64
65
# File 'lib/rails/openapi/router.rb', line 63

def path
  "/" + @prefix.join("/")
end

#route_modeObject

Returns the mode used for collecting routes



68
69
70
71
72
73
74
# File 'lib/rails/openapi/router.rb', line 68

def route_mode
  mode = :resource
  mode = :namespace if @endpoints.count == 0
  mode = :action if @subroutes.count == 0 && @parent && @parent.route_mode == :resource
  mode = :param if /^:/.match?(@prefix.last)
  mode
end

#to_sObject

Returns the routing tree in text format



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rails/openapi/router.rb', line 158

def to_s
  output = ""

  path = "/" + @prefix.join("/")
  @endpoints.each do |route|
    output += "#{route[:method].to_s.upcase} #{path}\n"
  end
  @subroutes.each do |k, subroute|
    output += subroute.to_s
  end

  output
end