Class: NonCrudEndpoints

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, params) ⇒ NonCrudEndpoints

Add a validation method which will be inherited by all the instances, and automatically run before any method call

Raises:

  • (NoMethodError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/non_crud_endpoints.rb', line 24

def initialize(m, params)
    # Rails.logger.debug "Initializing NonCrudEndpoints"
    # Showing the class name of the instance, and also, if there's a class inheriting from this one, the name of the child class
    # Rails.logger.debug "Class: #{self.class.name} - Child Class: #{self.class.superclass.name if self.class.superclass != Object}"
    # Check if self has the m method, if not, raise a NoMethodError
    raise NoMethodError, "The method #{m} does not exist in #{self.class.name}" unless self.respond_to? m
    # To avoid having conflicting keys from different classes, we will use a two levels object to store the definitions
    # the first level is the class name, and the second level is the method name
    self.definitions[self.class.name] ||= {}
    self.definitions[self.class.name][m.to_sym] ||= {}
    @definition = self.definitions[self.class.name][m.to_sym].with_indifferent_access

    # self.send(m, { explain: true }) rescue []
    validate_request(params)
    @result = self.send(m, params)
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



2
3
4
# File 'lib/non_crud_endpoints.rb', line 2

def result
  @result
end

Class Method Details

.desc(endpoint, key, definition) ⇒ Object



53
54
55
56
# File 'lib/non_crud_endpoints.rb', line 53

def self.desc(endpoint, key, definition)
    self.definitions[endpoint] ||= {}
    self.definitions[endpoint][key] = definition
end

.public_action(action_name) ⇒ Object



13
14
15
16
# File 'lib/non_crud_endpoints.rb', line 13

def self.public_action(action_name)
    self.public_action_registry[name] ||= []
    self.public_action_registry[name] << action_name.to_sym
end

.public_action?(model_name, action_name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/non_crud_endpoints.rb', line 18

def self.public_action?(model_name, action_name)
    registry = public_action_registry["Endpoints::#{model_name}"] || []
    registry.include?(action_name.to_sym)
end

Instance Method Details

#validate_request(params) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/non_crud_endpoints.rb', line 41

def validate_request(params)
    # If there is no definition, return
    return if @definition.blank?
    
    # Raise a ValidationError if the request does not match the definition of the verbs expected in the @definition hash
    #raise EndpointValidationError, "The verb \"#{params[:request_verb].presence || "No Verb Provided"}\" is not present in #{@definition.keys.join(", ")}." if @definition.keys.exclude? params[:request_verb]
    # Assuming @definition follows the openapi schema, we can check the request body and query parameters are correct
    
end