I am writing a Rails application that has only a REST API (no web interface or such). I need to check if requests are being executed with the correct parameters and return different error codes if not. For example, all API endpoints require user_access_token and client_id. Some other endpoints require various other parameters.
In all controllers, I have code that performs this check, but the code is duplicated, and there are many if conditions that can be extracted and put somewhere else. So I thought about adding before_filter to my ApplicationController , which performs this check. I defined a hash that contains the entire endpoint to display the required_pairs, and this filter is triggered before the control transfers to the actual controller in question.
But for some endpoints, it becomes a little complicated because some parameters are required if some other parameters are present, and in some cases one of two parameters is required. So now I am wondering if there is a better way to do this.
Am I doing it right? Is there a better / standardized way to do this? Or some kind of stone that does this?
source share