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 |
# File 'lib/non_crud_endpoints.rb', line 6 def initialize(m, params) # Check if self hase 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 @definition = self.definitions[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
16 17 18 19 20 21 22 23 24 |
# File 'lib/non_crud_endpoints.rb', line 16 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 |