Class: Rails::Swagger::Router
- Inherits:
-
Object
- Object
- Rails::Swagger::Router
- Defined in:
- lib/rails/swagger/router.rb
Instance Attribute Summary collapse
-
#endpoints ⇒ Object
Returns the value of attribute endpoints.
Instance Method Summary collapse
-
#<<(route) ⇒ Object
Adds an individual endpoint to the routing tree.
-
#[](path) ⇒ Object
Returns a specific branch of the routing tree.
-
#_debug_routing_tree ⇒ Object
Outputs a visual representation of the routing tree.
-
#action_for(route) ⇒ Object
Determines the action for a specific route.
-
#action_mode ⇒ Object
Returns the mode used for actions in this router.
-
#draw(map) ⇒ Object
Draws the routes for this router.
-
#initialize(prefix = [], parent = nil) ⇒ Router
constructor
A new instance of Router.
-
#path ⇒ Object
Returns the routing path.
-
#route_mode ⇒ Object
Returns the mode used for collecting routes.
-
#to_s ⇒ Object
Returns the routing tree in text format.
Constructor Details
#initialize(prefix = [], parent = nil) ⇒ Router
Returns a new instance of Router.
33 34 35 36 37 38 39 40 |
# File 'lib/rails/swagger/router.rb', line 33 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
#endpoints ⇒ Object
Returns the value of attribute endpoints.
31 32 33 |
# File 'lib/rails/swagger/router.rb', line 31 def endpoints @endpoints end |
Instance Method Details
#<<(route) ⇒ Object
Adds an individual endpoint to the routing tree
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rails/swagger/router.rb', line 43 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
56 57 58 |
# File 'lib/rails/swagger/router.rb', line 56 def [] path @subroutes[path] end |
#_debug_routing_tree ⇒ Object
Outputs a visual representation of the routing tree
156 157 158 159 160 161 162 163 164 |
# File 'lib/rails/swagger/router.rb', line 156 def _debug_routing_tree puts self.path + " - #{self.route_mode}" @endpoints.each do |route| puts "\t#{route[:method].to_s.upcase} to ##{self.action_for route} (#{self.action_mode})" end @subroutes.each do |k, subroute| subroute._debug_routing_tree end end |
#action_for(route) ⇒ Object
Determines the action for a specific route
84 85 86 87 88 89 90 |
# File 'lib/rails/swagger/router.rb', line 84 def action_for route raise "Argument must be an Endpoint" unless Endpoint === route action = @prefix[-1] action = PARAM_ROUTES[route[:method]] if self.action_mode == :param action = RESOURCE_ROUTES[route[:method]] if self.route_mode == :resource && self.action_mode == :collection action end |
#action_mode ⇒ Object
Returns the mode used for actions in this router
75 76 77 78 79 80 81 |
# File 'lib/rails/swagger/router.rb', line 75 def action_mode if /^:/ === @prefix[-1] :param else :collection end end |
#draw(map) ⇒ Object
Draws the routes for this router
93 94 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 |
# File 'lib/rails/swagger/router.rb', line 93 def draw map case self.route_mode when :resource # Find collection-level resource actions actions = @endpoints.map{ |r| self.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 # Draw a resource map.resources @prefix.last.to_sym, only: actions do draw_actions! map draw_subroutes! map 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 |
#path ⇒ Object
Returns the routing path
61 62 63 |
# File 'lib/rails/swagger/router.rb', line 61 def path "/" + @prefix.join("/") end |
#route_mode ⇒ Object
Returns the mode used for collecting routes
66 67 68 69 70 71 72 |
# File 'lib/rails/swagger/router.rb', line 66 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 /^:/ === @prefix.last mode end |
#to_s ⇒ Object
Returns the routing tree in text format
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rails/swagger/router.rb', line 139 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 |