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.



192
193
194
195
196
197
198
199
200
# File 'lib/belt/route_dsl.rb', line 192

def initialize(name, options = {})
  @name = name.to_s
  @routes = []
  @default_auth = options[:auth] || :none
  @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.



190
191
192
# File 'lib/belt/route_dsl.rb', line 190

def default_auth
  @default_auth
end

#default_corsObject (readonly)

Returns the value of attribute default_cors.



190
191
192
# File 'lib/belt/route_dsl.rb', line 190

def default_cors
  @default_cors
end

#default_lambdaObject (readonly)

Returns the value of attribute default_lambda.



190
191
192
# File 'lib/belt/route_dsl.rb', line 190

def default_lambda
  @default_lambda
end

#default_tablesObject (readonly)

Returns the value of attribute default_tables.



190
191
192
# File 'lib/belt/route_dsl.rb', line 190

def default_tables
  @default_tables
end

#nameObject (readonly)

Returns the value of attribute name.



190
191
192
# File 'lib/belt/route_dsl.rb', line 190

def name
  @name
end

#routesObject (readonly)

Returns the value of attribute routes.



190
191
192
# File 'lib/belt/route_dsl.rb', line 190

def routes
  @routes
end

Instance Method Details

#lambda(name) ⇒ Object



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

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/belt/route_dsl.rb', line 240

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)

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

  add_route(:post, "/#{resource_name}",
            resolve_request_model_for(resource_options, :create))
end

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



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/belt/route_dsl.rb', line 215

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_resource_routes(resource_name, param_name, resource_options, actions)

  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
  inherited_lambda = options[:lambda]
  nested_builder = NestedResourceBuilder.new(self, member_prefix, collection_prefix,
                                             inherited_tables: inherited_tables,
                                             inherited_auth: inherited_auth,
                                             inherited_lambda: inherited_lambda)
  nested_builder.instance_eval(&)
end