Class: Jets::Router::Route
- Inherits:
-
Object
- Object
- Jets::Router::Route
- Includes:
- Util
- Defined in:
- lib/jets/router/route.rb
Constant Summary collapse
- CAPTURE_REGEX =
as string
"([^/]*)"
Instance Attribute Summary collapse
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
- #account_on(prefix) ⇒ Object
- #account_scope(prefix) ⇒ Object
-
#action_name ⇒ Object
IE: index.
- #authorization_type ⇒ Object
- #compute_as ⇒ Object
- #compute_path ⇒ Object
- #compute_to ⇒ Object
-
#controller_name ⇒ Object
IE: PostsController.
-
#extract_parameters(actual_path) ⇒ Object
Extracts the path parameters from the actual path Only supports extracting 1 parameter.
- #extract_parameters_capture(actual_path) ⇒ Object
- #extract_parameters_proxy(actual_path) ⇒ Object
- #homepage? ⇒ Boolean
-
#initialize(options, scope = Scope.new) ⇒ Route
constructor
A new instance of Route.
- #internal? ⇒ Boolean
- #method ⇒ Object
-
#path(format = :jets) ⇒ Object
IE: standard: posts/:id/edit api_gateway: posts/id/edit.
-
#valid? ⇒ Boolean
Checks to see if the corresponding controller exists.
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(, scope=Scope.new) @options, @scope = , scope @path = compute_path @to = compute_to @as = compute_as end |
Instance Attribute Details
#as ⇒ Object (readonly)
Returns the value of attribute as.
12 13 14 |
# File 'lib/jets/router/route.rb', line 12 def as @as end |
#to ⇒ Object (readonly)
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) # Tricky @scope.from == :resources since the account_scope already has accounted for it 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
IE: index
108 109 110 |
# File 'lib/jets/router/route.rb', line 108 def action_name to.sub(/.*#/,'') end |
#authorization_type ⇒ Object
186 187 188 |
# File 'lib/jets/router/route.rb', line 186 def @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 # Index, Show, Edit, New 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 # Note: The @options[:prefix] is missing prefix and is not support via direct create_route. # This is because it can be added directly to the path. IE: # # get "myprefix/posts", to: "posts#index" # # Also, this helps to keep the method creator logic more simple. # 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('/') # add module "#{controller}##{action}" end |
#controller_name ⇒ Object
IE: PostsController
103 104 105 |
# File 'lib/jets/router/route.rb', line 103 def controller_name to.sub(/#.*/,'').camelize + "Controller" end |
#extract_parameters(actual_path) ⇒ Object
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 extract_parameters(actual_path) if path.include?(':') extract_parameters_capture(actual_path) elsif path.include?('*') extract_parameters_proxy(actual_path) else # Lambda AWS_PROXY sets null to the input request when there are no path parmeters nil end end |
#extract_parameters_capture(actual_path) ⇒ Object
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 extract_parameters_capture(actual_path) # changes path to a string used for a regexp # posts/:id/edit => posts\/(.*)\/edit labels = [] regexp_string = path.split('/').map do |s| if s.start_with?(':') labels << s.delete_prefix(':') CAPTURE_REGEX else s end end.join('\/') # make sure beginning and end of the string matches 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 |
#extract_parameters_proxy(actual_path) ⇒ Object
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 extract_parameters_proxy(actual_path) # changes path to a string used for a regexp # others/*proxy => others\/(.*) # nested/others/*proxy => nested/others\/(.*) if path.include?('/') leading_path = path.split('/')[0..-2].join('/') # drop last segment # leading_path: nested/others # capture everything after the leading_path as the value regexp = Regexp.new("#{leading_path}/(.*)") value = actual_path.match(regexp)[1] else value = actual_path end # the last segment without the '*' is the key proxy_segment = path.split('/').last # last segment is the proxy segment # proxy_segment: *proxy 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 # jets format 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 |