Class: Jets::Route
- Inherits:
-
Object
- Object
- Jets::Route
- Defined in:
- lib/jets/route.rb
Overview
route = Jets::Route.new(
path: "posts",
method: :get,
to: "posts#index",
)
Instance Method Summary collapse
-
#action_name ⇒ Object
IE: index.
-
#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) ⇒ 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.
-
#to ⇒ Object
IE: posts#index.
-
#valid? ⇒ Boolean
Checks to see if the corresponding controller exists.
Constructor Details
#initialize(options) ⇒ Route
Returns a new instance of Route.
7 8 9 |
# File 'lib/jets/route.rb', line 7 def initialize() @options = end |
Instance Method Details
#action_name ⇒ Object
IE: index
47 48 49 |
# File 'lib/jets/route.rb', line 47 def action_name to.sub(/.*#/,'') end |
#controller_name ⇒ Object
IE: PostsController
42 43 44 |
# File 'lib/jets/route.rb', line 42 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" }
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jets/route.rb', line 70 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/jets/route.rb', line 103 def extract_parameters_capture(actual_path) # changes path to a string used for a regexp # posts/:id/edit => posts\/(.*)\/edit regexp_string = path.split('/').map do |s| s.include?(':') ? "([a-zA-Z0-9_]*)" : s end.join('\/') # make sure beginning and end of the string matches regexp_string = "^#{regexp_string}$" regexp = Regexp.new(regexp_string) value = regexp.match(actual_path)[1] # only supports one path parameter key right now key = path.split('/').find {|s| s.include?(':') } # :id key = key.sub(':','') { key => value } end |
#extract_parameters_proxy(actual_path) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jets/route.rb', line 81 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
37 38 39 |
# File 'lib/jets/route.rb', line 37 def homepage? path == '' end |
#internal? ⇒ Boolean
33 34 35 |
# File 'lib/jets/route.rb', line 33 def internal? !!@options[:internal] end |
#method ⇒ Object
24 25 26 |
# File 'lib/jets/route.rb', line 24 def method @options[:method].to_s.upcase end |
#path(format = :jets) ⇒ Object
IE: standard: posts/:id/edit
api_gateway: posts/{id}/edit
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jets/route.rb', line 13 def path(format=:jets) case format when :api_gateway api_gateway_format(@options[:path]) when :raw @options[:path] else # jets format ensure_jets_format(@options[:path]) end end |
#to ⇒ Object
IE: posts#index
29 30 31 |
# File 'lib/jets/route.rb', line 29 def to @options[:to] end |
#valid? ⇒ Boolean
Checks to see if the corresponding controller exists. Useful to validate routes before deploying to CloudFormation and then rolling back.
53 54 55 56 57 58 59 60 |
# File 'lib/jets/route.rb', line 53 def valid? controller_class = begin controller_name.constantize rescue NameError return false end controller_class.all_tasks.keys.include?(action_name.to_sym) end |