Class: Jets::Router::Route
- Inherits:
-
Object
- Object
- Jets::Router::Route
show all
- Includes:
- Util
- Defined in:
- lib/jets/router/route.rb
Constant Summary
collapse
- CAPTURE_REGEX =
"([^/]*)"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Util
#get_controller_action, #handle_on!, #join, #underscore
Constructor Details
#initialize(options, scope = Scope.new) ⇒ Route
Returns a new instance of Route.
13
14
15
16
17
18
|
# File 'lib/jets/router/route.rb', line 13
def initialize(options, scope=Scope.new)
@options, @scope = options, scope
@path = compute_path
@to = compute_to
@as = compute_as
end
|
Instance Attribute Details
#as ⇒ Object
Returns the value of attribute as.
12
13
14
|
# File 'lib/jets/router/route.rb', line 12
def as
@as
end
|
#to ⇒ Object
Returns the value of attribute to.
12
13
14
|
# File 'lib/jets/router/route.rb', line 12
def to
@to
end
|
Instance Method Details
#account_on(prefix) ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/jets/router/route.rb', line 46
def account_on(prefix)
if @options[:on] == :collection && @scope.from == :resources
prefix = prefix.split('/')[0..-2].join('/')
end
prefix == '' ? nil : prefix
end
|
#account_scope(prefix) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/jets/router/route.rb', line 35
def account_scope(prefix)
return unless prefix
return prefix unless @options[:from_scope]
if @options[:singular_resource]
prefix.split('/')[0..-2].join('/')
else
prefix.split('/')[0..-3].join('/')
end
end
|
#action_name ⇒ Object
108
109
110
|
# File 'lib/jets/router/route.rb', line 108
def action_name
to.sub(/.*#/,'')
end
|
#api_key_required ⇒ Object
190
191
192
|
# File 'lib/jets/router/route.rb', line 190
def api_key_required
@options[:api_key_required]
end
|
#authorization_type ⇒ Object
186
187
188
|
# File 'lib/jets/router/route.rb', line 186
def authorization_type
@options[:authorization_type]
end
|
#compute_as ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/jets/router/route.rb', line 61
def compute_as
return unless @options[:method] == :get || @options[:root]
controller, action = get_controller_action(@options)
klass = if @options[:root]
Jets::Router::MethodCreator::Root
elsif %w[index edit show new].include?(action.to_s)
class_name = "Jets::Router::MethodCreator::#{action.camelize}"
class_name.constantize else
Jets::Router::MethodCreator::Generic
end
klass.new(@options, @scope, controller).full_meth_name(nil)
end
|
#compute_path ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/jets/router/route.rb', line 20
def compute_path
prefix = @scope.full_prefix
prefix = account_scope(prefix)
prefix = account_on(prefix)
[prefix, @options[:path]].compact.join('/')
end
|
#compute_to ⇒ Object
54
55
56
57
58
59
|
# File 'lib/jets/router/route.rb', line 54
def compute_to
controller, action = get_controller_action(@options)
mod = @options[:module] || @scope.full_module
controller = [mod, controller].compact.join('/') "#{controller}##{action}"
end
|
#controller_name ⇒ Object
103
104
105
|
# File 'lib/jets/router/route.rb', line 103
def controller_name
to.sub(/#.*/,'').camelize + "Controller"
end
|
Extracts the path parameters from the actual path Only supports extracting 1 parameter. So:
actual_path: posts/tung/edit
route.path: posts/:id/edit
Returns:
{ id: "tung" }
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/jets/router/route.rb', line 131
def (actual_path)
if path.include?(':')
(actual_path)
elsif path.include?('*')
(actual_path)
else
nil
end
end
|
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/jets/router/route.rb', line 164
def (actual_path)
labels = []
regexp_string = path.split('/').map do |s|
if s.start_with?(':')
labels << s.delete_prefix(':')
CAPTURE_REGEX
else
s
end
end.join('\/')
regexp_string = "^#{regexp_string}$"
regexp = Regexp.new(regexp_string)
values = regexp.match(actual_path).captures
labels.map do |next_label|
[next_label, values.delete_at(0)]
end.to_h
end
|
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/jets/router/route.rb', line 142
def (actual_path)
if path.include?('/')
leading_path = path.split('/')[0..-2].join('/') regexp = Regexp.new("#{leading_path}/(.*)")
value = actual_path.match(regexp)[1]
else
value = actual_path
end
proxy_segment = path.split('/').last key = proxy_segment.sub('*','')
{ key => value }
end
|
#homepage? ⇒ Boolean
98
99
100
|
# File 'lib/jets/router/route.rb', line 98
def homepage?
path == ''
end
|
#internal? ⇒ Boolean
94
95
96
|
# File 'lib/jets/router/route.rb', line 94
def internal?
!!@options[:internal]
end
|
#method ⇒ Object
90
91
92
|
# File 'lib/jets/router/route.rb', line 90
def method
@options[:method].to_s.upcase
end
|
#path(format = :jets) ⇒ Object
IE: standard: posts/:id/edit
api_gateway: posts/{id}/edit
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/jets/router/route.rb', line 79
def path(format=:jets)
case format
when :api_gateway
api_gateway_format(@path)
when :raw
@path
else ensure_jets_format(@path)
end
end
|
#valid? ⇒ Boolean
Checks to see if the corresponding controller exists. Useful to validate routes before deploying to CloudFormation and then rolling back.
114
115
116
117
118
119
120
121
|
# File 'lib/jets/router/route.rb', line 114
def valid?
controller_class = begin
controller_name.constantize
rescue NameError
return false
end
controller_class.lambda_functions.include?(action_name.to_sym)
end
|