Class: NonCrudEndpoints
- Inherits:
-
Object
- Object
- NonCrudEndpoints
- Defined in:
- lib/non_crud_endpoints.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
-
#initialize(m, params) ⇒ NonCrudEndpoints
constructor
Add a validation method which will be inherited by all the instances, and automatically run before any method call.
- #validate_request(params) ⇒ Object
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
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/non_crud_endpoints.rb', line 6 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
#result ⇒ Object
Returns the value of attribute result.
2 3 4 |
# File 'lib/non_crud_endpoints.rb', line 2 def result @result end |
Instance Method Details
#validate_request(params) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/non_crud_endpoints.rb', line 23 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 |