Class: Jets::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/route.rb

Overview

route = Jets::Route.new(

path: "posts",
method: :get,
to: "posts#index",

)

Constant Summary collapse

CAPTURE_REGEX =

as string

"([^/]*)"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Route

Returns a new instance of Route.



9
10
11
# File 'lib/jets/route.rb', line 9

def initialize(options)
  @options = options
end

Instance Method Details

#action_nameObject

IE: index



49
50
51
# File 'lib/jets/route.rb', line 49

def action_name
  to.sub(/.*#/,'')
end

#authorization_typeObject



127
128
129
# File 'lib/jets/route.rb', line 127

def authorization_type
  @options[:authorization_type]
end

#controller_nameObject

IE: PostsController



44
45
46
# File 'lib/jets/route.rb', line 44

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" }


72
73
74
75
76
77
78
79
80
81
# File 'lib/jets/route.rb', line 72

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jets/route.rb', line 105

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jets/route.rb', line 83

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

Returns:

  • (Boolean)


39
40
41
# File 'lib/jets/route.rb', line 39

def homepage?
  path == ''
end

#internal?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/jets/route.rb', line 35

def internal?
  !!@options[:internal]
end

#methodObject



26
27
28
# File 'lib/jets/route.rb', line 26

def method
  @options[:method].to_s.upcase
end

#path(format = :jets) ⇒ Object

IE: standard: posts/:id/edit

api_gateway: posts/{id}/edit


15
16
17
18
19
20
21
22
23
24
# File 'lib/jets/route.rb', line 15

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

#toObject

IE: posts#index



31
32
33
# File 'lib/jets/route.rb', line 31

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.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/jets/route.rb', line 55

def valid?
  controller_class = begin
    controller_name.constantize
  rescue NameError
    return false
  end
  controller_class.lambda_functions.include?(action_name.to_sym)
end