Module: Mindee::HTTP::ResponseValidation
- Defined in:
- lib/mindee/http/response_validation.rb
Overview
Module dedicated to the validation & sanitizing of HTTP responses.
Class Method Summary collapse
-
.clean_request!(response) ⇒ Object
Checks and correct the response object depending on the possible kinds of returns.
-
.valid_async_response?(response) ⇒ Boolean
Checks if the asynchronous response is valid.
-
.valid_sync_response?(response) ⇒ Boolean
Checks if the synchronous response is valid.
Class Method Details
.clean_request!(response) ⇒ Object
Checks and correct the response object depending on the possible kinds of returns.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mindee/http/response_validation.rb', line 44 def self.clean_request!(response) return response if (response.code.to_i < 200) || (response.code.to_i > 302) return response if response.body.empty? hashed_response = JSON.parse(response.body, object_class: Hash) if hashed_response.dig('api_request', 'status_code').to_i > 399 response.instance_variable_set(:@code, hashed_response['api_request']['status_code'].to_s) end return if !hashed_response.dig('job', 'error').empty? && (hashed_response.dig('job', 'status') != Mindee::Parsing::Common::JobStatus::FAILURE.to_s) response.instance_variable_set(:@code, '500') end |
.valid_async_response?(response) ⇒ Boolean
Checks if the asynchronous response is valid. Also checks if it is a valid synchronous response. Returns true if the response is valid.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mindee/http/response_validation.rb', line 28 def self.valid_async_response?(response) return false unless valid_sync_response?(response) return false unless (200..302).cover?(response.code.to_i) hashed_response = JSON.parse(response.body, object_class: Hash) return false if hashed_response.dig('job', 'status') == Mindee::Parsing::Common::JobStatus::FAILURE return false if hashed_response.dig('job', 'error') && !hashed_response.dig('job', 'error').empty? true end |
.valid_sync_response?(response) ⇒ Boolean
Checks if the synchronous response is valid. Returns True if the response is valid.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mindee/http/response_validation.rb', line 13 def self.valid_sync_response?(response) return false unless (200..399).cover?(response.code.to_i) begin JSON.parse(response.body, object_class: Hash) rescue StandardError return false end true end |