Class: Belt::ApiGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/route_dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ApiGateway

Returns a new instance of ApiGateway.



148
149
150
151
152
153
154
155
156
# File 'lib/belt/route_dsl.rb', line 148

def initialize(name, options = {})
  @name = name.to_s
  @routes = []
  @default_auth = options[:auth] || :cognito
  @default_lambda = options[:lambda] || name
  @default_cors = options.fetch(:cors, true)
  @default_tables = Array(options[:tables] || [])
  @current_lambda_context = nil
end

Instance Attribute Details

#default_authObject (readonly)

Returns the value of attribute default_auth.



146
147
148
# File 'lib/belt/route_dsl.rb', line 146

def default_auth
  @default_auth
end

#default_corsObject (readonly)

Returns the value of attribute default_cors.



146
147
148
# File 'lib/belt/route_dsl.rb', line 146

def default_cors
  @default_cors
end

#default_lambdaObject (readonly)

Returns the value of attribute default_lambda.



146
147
148
# File 'lib/belt/route_dsl.rb', line 146

def default_lambda
  @default_lambda
end

#default_tablesObject (readonly)

Returns the value of attribute default_tables.



146
147
148
# File 'lib/belt/route_dsl.rb', line 146

def default_tables
  @default_tables
end

#nameObject (readonly)

Returns the value of attribute name.



146
147
148
# File 'lib/belt/route_dsl.rb', line 146

def name
  @name
end

#routesObject (readonly)

Returns the value of attribute routes.



146
147
148
# File 'lib/belt/route_dsl.rb', line 146

def routes
  @routes
end

Instance Method Details

#lambda(name) ⇒ Object



158
159
160
161
162
163
# File 'lib/belt/route_dsl.rb', line 158

def lambda(name, &)
  previous_context = @current_lambda_context
  @current_lambda_context = name.to_sym
  instance_eval(&) if block_given?
  @current_lambda_context = previous_context
end

#resource(name, options = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/belt/route_dsl.rb', line 198

def resource(name, options = {})
  resource_name = name.to_s
  actions = determine_actions(options, default: %i[show update destroy])
  resource_options = options.merge(route_type: :resource)

  add_route(:get, "/#{resource_name}", resource_options) if actions.include?(:show)
  add_route(:put, "/#{resource_name}", resource_options) if actions.include?(:update)
  add_route(:delete, "/#{resource_name}", resource_options) if actions.include?(:destroy)
  add_route(:post, "/#{resource_name}", resource_options) if actions.include?(:create)
end

#resources(name, options = {}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/belt/route_dsl.rb', line 171

def resources(name, options = {}, &)
  resource_name = name.to_s
  singular = singularize(resource_name)
  param_name = options[:param] || "#{singular}_id"
  options = auto_infer_tables(resource_name, options)
  resource_options = options.merge(route_type: :resources)
  actions = determine_actions(options)

  add_route(:get, "/#{resource_name}", resource_options) if actions.include?(:index)
  add_route(:post, "/#{resource_name}", resource_options) if actions.include?(:create)
  add_route(:get, "/#{resource_name}/{#{param_name}}", resource_options) if actions.include?(:show)
  add_route(:put, "/#{resource_name}/{#{param_name}}", resource_options) if actions.include?(:update)
  add_route(:delete, "/#{resource_name}/{#{param_name}}", resource_options) if actions.include?(:destroy)

  return unless block_given?

  collection_prefix = "/#{resource_name}"
  member_prefix = "/#{resource_name}/{#{param_name}}"
  resource_tables = Array(options[:tables] || [])
  inherited_tables = (@default_tables + resource_tables).uniq
  inherited_auth = options[:auth] || @default_auth
  nested_builder = NestedResourceBuilder.new(self, member_prefix, collection_prefix,
                                             inherited_tables: inherited_tables,
                                             inherited_auth: inherited_auth)
  nested_builder.instance_eval(&)
end