Class: Rails::Swagger::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Router.



29
30
31
32
33
34
35
36
# File 'lib/rails/swagger/router.rb', line 29

def initialize prefix = [], parent = nil
	@parent = parent
	@prefix = prefix
	@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.



27
28
29
# File 'lib/rails/swagger/router.rb', line 27

def endpoints
  @endpoints
end

Instance Method Details

#<<(route) ⇒ Object

Adds an individual endpoint to the routing tree



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails/swagger/router.rb', line 39

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



52
53
54
# File 'lib/rails/swagger/router.rb', line 52

def [] path
	@subroutes[path]
end

#action_for(route) ⇒ Object

Determines the action for a specific route



81
82
83
84
85
86
87
# File 'lib/rails/swagger/router.rb', line 81

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_modeObject

Returns the mode used for actions in this router



70
71
72
73
74
75
76
77
78
# File 'lib/rails/swagger/router.rb', line 70

def action_mode
	if /^:/ === @prefix[-2]
		:member
	elsif /^:/ === @prefix[-1]
		:param
	else
		:collection
	end
end

#draw(map) ⇒ Object

Draws the routes for this router



90
91
92
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
# File 'lib/rails/swagger/router.rb', line 90

def draw map
	case self.route_mode
	when :resource

		# Find collection-level resource actions
		actions = @endpoints.map{ |route| self.action_for route }.select{ |action| Symbol === action }

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

		map.resources @prefix.last.to_sym, only: actions do
			draw_actions! map
			draw_subroutes! map
		end

	when :namespace
		if @prefix.join("/").blank?
			draw_subroutes! map
		else
			map.namespace @prefix.last do
				draw_subroutes! map
			end
		end
	when :action
		draw_actions! map
	end

end

#pathObject

Returns the routing path



57
58
59
# File 'lib/rails/swagger/router.rb', line 57

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

#route_modeObject

Returns the mode used for collecting routes



62
63
64
65
66
67
# File 'lib/rails/swagger/router.rb', line 62

def route_mode
	mode = :resource
	mode = :namespace if @endpoints.count == 0
	mode = :action if @subroutes.count == 0 && @parent && @parent.route_mode == :resource
	mode
end

#routing_treeObject



121
122
123
124
125
126
127
128
129
# File 'lib/rails/swagger/router.rb', line 121

def 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.routing_tree end

end

#to_sObject

Outputs 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