Module: Mindee::HTTP::ResponseValidation

Defined in:
lib/mindee/http/response_validation.rb,
sig/mindee/http/response_validation.rbs

Overview

Module dedicated to the validation & sanitizing of HTTP responses.

Class Method Summary collapse

Class Method Details

.clean_request!(response) ⇒ void

This method returns an undefined value.

Checks and correct the response object depending on the possible kinds of returns.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mindee/http/response_validation.rb', line 63

def self.clean_request!(response)
  return response if (response.code.to_i < 200) || (response.code.to_i > 302) # : Net::HTTPResponse

  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').downcase != Mindee::V1::Parsing::Common::JobStatus::FAILURE.to_s)

  response.instance_variable_set(:@code, '500')
end

.valid_async_response?(response) ⇒ bool

Checks if the asynchronous response is valid. Also checks if it is a valid synchronous response. Returns true if the response is valid.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mindee/http/response_validation.rb', line 47

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::V1::Parsing::Common::JobStatus::FAILURE

  return false if hashed_response.dig('job', 'error') && !hashed_response.dig('job', 'error').empty?

  true
end

.valid_sync_response?(response) ⇒ bool

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

.valid_v2_response?(response) ⇒ bool

Checks if a V2 response is valid.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mindee/http/response_validation.rb', line 27

def self.valid_v2_response?(response)
  return false unless valid_sync_response?(response)

  hashed_response = JSON.parse(response.body, object_class: Hash)

  return false if hashed_response.dig('job', 'status').to_s == 'Failed'

  return false if hashed_response.dig('job',
                                      'error') && !(hashed_response.dig('job',
                                                                        'error').empty? || hashed_response.dig(
                                                                          'job', 'error'
                                                                        ).nil?)

  true
end