Class: BetterAuth::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/endpoint.rb

Defined Under Namespace

Classes: Context, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, method: nil, body_schema: nil, query_schema: nil, headers_schema: nil, metadata: {}, use: [], &handler) ⇒ Endpoint

Returns a new instance of Endpoint.



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

def initialize(path: nil, method: nil, body_schema: nil, query_schema: nil, headers_schema: nil, metadata: {}, use: [], &handler)
  @path = path
  @methods = Array(method || "*").map { |value| value.to_s.upcase }
  @body_schema = body_schema
  @query_schema = query_schema
  @headers_schema = headers_schema
  @metadata =  || {}
  @use = Array(use)
  @handler = handler || ->(_ctx) {}
end

Instance Attribute Details

#body_schemaObject (readonly)

Returns the value of attribute body_schema.



7
8
9
# File 'lib/better_auth/endpoint.rb', line 7

def body_schema
  @body_schema
end

#handlerObject (readonly)

Returns the value of attribute handler.



7
8
9
# File 'lib/better_auth/endpoint.rb', line 7

def handler
  @handler
end

#headers_schemaObject (readonly)

Returns the value of attribute headers_schema.



7
8
9
# File 'lib/better_auth/endpoint.rb', line 7

def headers_schema
  @headers_schema
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/better_auth/endpoint.rb', line 7

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/better_auth/endpoint.rb', line 7

def path
  @path
end

#query_schemaObject (readonly)

Returns the value of attribute query_schema.



7
8
9
# File 'lib/better_auth/endpoint.rb', line 7

def query_schema
  @query_schema
end

#useObject (readonly)

Returns the value of attribute use.



7
8
9
# File 'lib/better_auth/endpoint.rb', line 7

def use
  @use
end

Instance Method Details

#call(context) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/better_auth/endpoint.rb', line 34

def call(context)
  apply_schemas!(context)

  use.each do |middleware|
    middleware_result = middleware.call(context)
    return Result.from_value(middleware_result, context) if middleware_result
  end

  Result.from_value(handler.call(context), context)
end

#matches_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/better_auth/endpoint.rb', line 30

def matches_method?(method)
  methods.include?("*") || methods.include?(method.to_s.upcase)
end

#methodsObject



26
27
28
# File 'lib/better_auth/endpoint.rb', line 26

def methods
  @methods.empty? ? ["*"] : @methods
end