Class: BetterAuth::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, endpoints) ⇒ API

Returns a new instance of API.



7
8
9
10
11
# File 'lib/better_auth/api.rb', line 7

def initialize(context, endpoints)
  @context = context
  @endpoints = endpoints
  define_endpoint_methods
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/better_auth/api.rb', line 5

def context
  @context
end

#endpointsObject (readonly)

Returns the value of attribute endpoints.



5
6
7
# File 'lib/better_auth/api.rb', line 5

def endpoints
  @endpoints
end

Instance Method Details

#call_endpoint(key, input = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/better_auth/api.rb', line 13

def call_endpoint(key, input = {})
  context.reset_runtime! if context.respond_to?(:reset_runtime!)
  endpoint = endpoints.fetch(key.to_sym)
  input = symbolize_keys(input || {})
  endpoint_context = Endpoint::Context.new(
    path: endpoint.path,
    method: Array(endpoint.methods).first,
    query: input[:query] || {},
    body: input[:body] || {},
    params: input[:params] || {},
    headers: input[:headers] || {},
    context: context
  )

  result = run_endpoint_with_hooks(endpoint, endpoint_context)
  format_result(result, input)
end

#execute(endpoint, endpoint_context) ⇒ Object



31
32
33
# File 'lib/better_auth/api.rb', line 31

def execute(endpoint, endpoint_context)
  run_endpoint_with_hooks(endpoint, endpoint_context)
end