Class: Raas::BaseController
- Inherits:
-
Object
- Object
- Raas::BaseController
- Defined in:
- lib/raas/controllers/base_controller.rb
Direct Known Subclasses
AccountsController, CatalogController, CustomersController, ExchangeRatesController, OrdersController, StatusController
Constant Summary collapse
- @@http_client =
FaradayClient.new(timeout: 15)
- @@global_headers =
{ 'user-agent' => 'TangoCardv2NGSDK' }
Instance Attribute Summary collapse
-
#http_call_back ⇒ Object
Returns the value of attribute http_call_back.
-
#http_client ⇒ Object
Returns the value of attribute http_client.
Instance Method Summary collapse
- #execute_request(request, binary: false, name: nil) ⇒ Object
-
#initialize(http_client: nil, http_call_back: nil) ⇒ BaseController
constructor
A new instance of BaseController.
- #validate_parameters(args) ⇒ Object
- #validate_response(context) ⇒ Object
Constructor Details
#initialize(http_client: nil, http_call_back: nil) ⇒ BaseController
Returns a new instance of BaseController.
13 14 15 16 17 18 |
# File 'lib/raas/controllers/base_controller.rb', line 13 def initialize(http_client: nil, http_call_back: nil) @http_client = http_client ||= @@http_client @http_call_back = http_call_back @logger = Logging.logger[self] @logger.info("Instantiated controller class.") end |
Instance Attribute Details
#http_call_back ⇒ Object
Returns the value of attribute http_call_back.
5 6 7 |
# File 'lib/raas/controllers/base_controller.rb', line 5 def http_call_back @http_call_back end |
#http_client ⇒ Object
Returns the value of attribute http_client.
5 6 7 |
# File 'lib/raas/controllers/base_controller.rb', line 5 def http_client @http_client end |
Instance Method Details
#execute_request(request, binary: false, name: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/raas/controllers/base_controller.rb', line 28 def execute_request(request, binary: false, name: nil) @logger.info("Calling the on_before_request method of http_call_back for #{name}.") if @http_call_back @http_call_back.on_before_request(request) if @http_call_back @logger.info("Merging global headers with endpoint headers for #{name}.") APIHelper.clean_hash(request.headers) request.headers = @@global_headers.clone.merge(request.headers) @logger.debug("Raw request for #{name} is: #{request.inspect}") response = binary ? @http_client.execute_as_binary(request) : @http_client.execute_as_string(request) @logger.debug("Raw response for #{name} is: #{response.inspect}") @logger.info("Wrapping request and response in a context object for #{name}.") context = HttpContext.new(request, response) @logger.info("Calling on_after_response method of http_call_back for #{name}.") if @http_call_back @http_call_back.on_after_response(context) if @http_call_back return context end |
#validate_parameters(args) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/raas/controllers/base_controller.rb', line 20 def validate_parameters(args) args.each do |_name, value| if value.nil? raise ArgumentError.new 'Required parameter #{name} cannot be nil.' end end end |
#validate_response(context) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/raas/controllers/base_controller.rb', line 48 def validate_response(context) if context.response.status_code == 400 raise RaasClientException.new 'Bad Request', context elsif context.response.status_code == 401 raise RaasGenericException.new 'Unauthorized - Invalid Credentials', context elsif context.response.status_code == 403 raise RaasClientException.new 'Forbidden', context elsif context.response.status_code == 404 raise RaasGenericException.new 'Not Found', context elsif context.response.status_code == 409 raise RaasClientException.new 'Conflict', context elsif context.response.status_code == 500 raise RaasServerException.new 'Internal Server Error - Retry Later', context elsif context.response.status_code == 503 raise RaasServerException.new 'Service Unavailable - Retry Later', context elsif !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK raise RaasGenericException.new 'API Error', context end end |