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.
32 33 34 35 36 37 38 39 |
# File 'lib/rails/swagger/router.rb', line 32 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.
30 31 32 |
# File 'lib/rails/swagger/router.rb', line 30 def endpoints @endpoints end |
Instance Method Details
#<<(route) ⇒ Object
Adds an individual endpoint to the routing tree
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rails/swagger/router.rb', line 42 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
55 56 57 |
# File 'lib/rails/swagger/router.rb', line 55 def [] path @subroutes[path] end |
#_debug_routing_tree ⇒ Object
Outputs a visual representation of the routing tree
149 150 151 152 153 154 155 156 157 |
# File 'lib/rails/swagger/router.rb', line 149 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
73 74 75 76 77 78 79 80 81 |
# File 'lib/rails/swagger/router.rb', line 73 def action_mode if /^:/ === @prefix[-2] :member elsif /^:/ === @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 |
# 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 :action # Draw actions directly draw_actions! map end end |
#path ⇒ Object
Returns the routing path
60 61 62 |
# File 'lib/rails/swagger/router.rb', line 60 def path "/" + @prefix.join("/") end |
#route_mode ⇒ Object
Returns the mode used for collecting routes
65 66 67 68 69 70 |
# File 'lib/rails/swagger/router.rb', line 65 def route_mode mode = :resource mode = :namespace if @endpoints.count == 0 mode = :action if @subroutes.count == 0 && @parent && @parent.route_mode == :resource mode end |
#to_s ⇒ Object
Returns the routing tree in text format
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rails/swagger/router.rb', line 132 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 |