Class: CoreLibrary::ResponseHandler
- Inherits:
-
Object
- Object
- CoreLibrary::ResponseHandler
- Defined in:
- lib/apimatic-core/response_handler.rb
Overview
Creates an instance of ResponseHandler.
Instance Method Summary collapse
-
#apply_api_response(response, deserialized_value) ⇒ Object
Applies API response.
-
#apply_convertor(deserialized_value) ⇒ Object
Applies converter to the response.
-
#apply_deserializer(response, should_symbolize_hash) ⇒ Object
Applies deserializer to the response.
-
#apply_xml_deserializer(response) ⇒ Object
Applies xml deserializer to the response.
-
#convertor(convertor) ⇒ ResponseHandler
Sets converter for the response.
-
#datetime_format(datetime_format) ⇒ ResponseHandler
Sets the datetime format.
-
#deserialize_into(deserialize_into) ⇒ ResponseHandler
Sets the model to deserialize into.
-
#deserializer(deserializer) ⇒ ResponseHandler
Sets deserializer for the response.
-
#handle(response, global_errors, should_symbolize_hash = false) ⇒ Object
Main method to handle the response with all the set properties.
-
#initialize ⇒ ResponseHandler
constructor
Creates an instance of ResponseHandler.
-
#is_api_response(is_api_response) ⇒ ResponseHandler
Sets the is_api_response property.
-
#is_date_response(is_date_response) ⇒ ResponseHandler
Sets the is_date_response property.
-
#is_nullify404(is_nullify404) ⇒ ResponseHandler
Sets the is_nullify404 property.
-
#is_primitive_response(is_primitive_response) ⇒ ResponseHandler
Sets the is_primitive_response property.
-
#is_response_array(is_response_array) ⇒ ResponseHandler
Sets the is_response_array property.
-
#is_response_void(is_response_void) ⇒ ResponseHandler
Sets the is_response_void property.
-
#is_xml_response(is_xml_response) ⇒ ResponseHandler
Set the is_xml_response property.
-
#local_error(error_code, error_message, exception_type) ⇒ ResponseHandler
Registers an entry with error message in the local errors hash.
-
#local_error_template(error_code, error_message_template, exception_type) ⇒ ResponseHandler
Registers an entry with error template in the local errors hash.
-
#validate(response, global_errors) ⇒ Object
Validates the response provided and throws an error against the configured status code.
-
#validate_against_error_cases(response, error_cases) ⇒ Object
Validates the response against the provided error cases hash, if matches, it raises the exception.
-
#xml_attribute(xml_attribute) ⇒ ResponseHandler
Set the xml_attribute property.
Constructor Details
#initialize ⇒ ResponseHandler
Creates an instance of ResponseHandler.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/apimatic-core/response_handler.rb', line 5 def initialize @deserializer = nil @convertor = nil @deserialize_into = nil @is_api_response = false @is_nullify404 = false @local_errors = {} @datetime_format = nil @is_xml_response = false @xml_attribute = nil @is_primitive_response = false @is_date_response = false @is_response_array = false @is_response_void = false end |
Instance Method Details
#apply_api_response(response, deserialized_value) ⇒ Object
Applies API response.
211 212 213 214 215 216 217 218 |
# File 'lib/apimatic-core/response_handler.rb', line 211 def apply_api_response(response, deserialized_value) if @is_api_response errors = ApiHelper.map_response(deserialized_value, ['errors']) return ApiResponse.new(response, data: deserialized_value, errors: errors) end deserialized_value end |
#apply_convertor(deserialized_value) ⇒ Object
Applies converter to the response.
222 223 224 225 226 |
# File 'lib/apimatic-core/response_handler.rb', line 222 def apply_convertor(deserialized_value) return @convertor.call(deserialized_value) unless @convertor.nil? deserialized_value end |
#apply_deserializer(response, should_symbolize_hash) ⇒ Object
Applies deserializer to the response.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/apimatic-core/response_handler.rb', line 193 def apply_deserializer(response, should_symbolize_hash) return apply_xml_deserializer(response) if @is_xml_response return response.raw_body if @deserializer.nil? if @datetime_format @deserializer.call(response.raw_body, @datetime_format, @is_response_array, should_symbolize_hash) elsif @is_date_response @deserializer.call(response.raw_body, @is_response_array, should_symbolize_hash) elsif !@deserialize_into.nil? || @is_primitive_response @deserializer.call(response.raw_body, @deserialize_into, @is_response_array, should_symbolize_hash) else @deserializer.call(response.raw_body, should_symbolize_hash) end end |
#apply_xml_deserializer(response) ⇒ Object
Applies xml deserializer to the response.
182 183 184 185 186 187 188 189 |
# File 'lib/apimatic-core/response_handler.rb', line 182 def apply_xml_deserializer(response) unless @xml_attribute.get_array_item_name.nil? return @deserializer.call(response.raw_body, @xml_attribute.get_root_element_name, @xml_attribute.get_array_item_name, @deserialize_into, @datetime_format) end @deserializer.call(response.raw_body, @xml_attribute.get_root_element_name, @deserialize_into, @datetime_format) end |
#convertor(convertor) ⇒ ResponseHandler
Sets converter for the response.
32 33 34 35 |
# File 'lib/apimatic-core/response_handler.rb', line 32 def convertor(convertor) @convertor = convertor self end |
#datetime_format(datetime_format) ⇒ ResponseHandler
Sets the datetime format.
72 73 74 75 |
# File 'lib/apimatic-core/response_handler.rb', line 72 def datetime_format(datetime_format) @datetime_format = datetime_format self end |
#deserialize_into(deserialize_into) ⇒ ResponseHandler
Sets the model to deserialize into.
40 41 42 43 |
# File 'lib/apimatic-core/response_handler.rb', line 40 def deserialize_into(deserialize_into) @deserialize_into = deserialize_into self end |
#deserializer(deserializer) ⇒ ResponseHandler
Sets deserializer for the response.
24 25 26 27 |
# File 'lib/apimatic-core/response_handler.rb', line 24 def deserializer(deserializer) @deserializer = deserializer self end |
#handle(response, global_errors, should_symbolize_hash = false) ⇒ Object
Main method to handle the response with all the set properties.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/apimatic-core/response_handler.rb', line 148 def handle(response, global_errors, should_symbolize_hash = false) # checking Nullify 404 return nil if response.status_code == 404 && @is_nullify404 # validating response if configured validate(response, global_errors) return if @is_response_void # applying deserializer if configured deserialized_value = apply_deserializer(response, should_symbolize_hash) # applying api_response if configured deserialized_value = apply_api_response(response, deserialized_value) # applying convertor if configured deserialized_value = apply_convertor(deserialized_value) deserialized_value end |
#is_api_response(is_api_response) ⇒ ResponseHandler
Sets the is_api_response property.
97 98 99 100 |
# File 'lib/apimatic-core/response_handler.rb', line 97 def is_api_response(is_api_response) @is_api_response = is_api_response self end |
#is_date_response(is_date_response) ⇒ ResponseHandler
Sets the is_date_response property.
121 122 123 124 |
# File 'lib/apimatic-core/response_handler.rb', line 121 def is_date_response(is_date_response) @is_date_response = is_date_response self end |
#is_nullify404(is_nullify404) ⇒ ResponseHandler
Sets the is_nullify404 property.
105 106 107 108 |
# File 'lib/apimatic-core/response_handler.rb', line 105 def is_nullify404(is_nullify404) @is_nullify404 = is_nullify404 self end |
#is_primitive_response(is_primitive_response) ⇒ ResponseHandler
Sets the is_primitive_response property. rubocop:disable Naming/PredicateName
89 90 91 92 |
# File 'lib/apimatic-core/response_handler.rb', line 89 def is_primitive_response(is_primitive_response) @is_primitive_response = is_primitive_response self end |
#is_response_array(is_response_array) ⇒ ResponseHandler
Sets the is_response_array property.
129 130 131 132 |
# File 'lib/apimatic-core/response_handler.rb', line 129 def is_response_array(is_response_array) @is_response_array = is_response_array self end |
#is_response_void(is_response_void) ⇒ ResponseHandler
Sets the is_response_void property.
137 138 139 140 |
# File 'lib/apimatic-core/response_handler.rb', line 137 def is_response_void(is_response_void) @is_response_void = is_response_void self end |
#is_xml_response(is_xml_response) ⇒ ResponseHandler
Set the is_xml_response property.
113 114 115 116 |
# File 'lib/apimatic-core/response_handler.rb', line 113 def is_xml_response(is_xml_response) @is_xml_response = is_xml_response self end |
#local_error(error_code, error_message, exception_type) ⇒ ResponseHandler
Registers an entry with error message in the local errors hash.
50 51 52 53 54 55 |
# File 'lib/apimatic-core/response_handler.rb', line 50 def local_error(error_code, , exception_type) @local_errors[error_code.to_s] = ErrorCase.new .() .exception_type(exception_type) self end |
#local_error_template(error_code, error_message_template, exception_type) ⇒ ResponseHandler
Registers an entry with error template in the local errors hash.
62 63 64 65 66 67 |
# File 'lib/apimatic-core/response_handler.rb', line 62 def local_error_template(error_code, , exception_type) @local_errors[error_code.to_s] = ErrorCase.new .() .exception_type(exception_type) self end |
#validate(response, global_errors) ⇒ Object
Validates the response provided and throws an error against the configured status code.
173 174 175 176 177 178 179 |
# File 'lib/apimatic-core/response_handler.rb', line 173 def validate(response, global_errors) return unless response.status_code < 200 || response.status_code > 299 validate_against_error_cases(response, @local_errors) validate_against_error_cases(response, global_errors) end |
#validate_against_error_cases(response, error_cases) ⇒ Object
Validates the response against the provided error cases hash, if matches, it raises the exception.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/apimatic-core/response_handler.rb', line 232 def validate_against_error_cases(response, error_cases) actual_status_code = response.status_code.to_s # Handling error case when configured as explicit error code error_case = error_cases[actual_status_code] error_case&.raise_exception(response) # Handling error case when configured as explicit error codes range default_range_entry = error_cases&.filter do |error_code, _| error_code.match?("^#{actual_status_code[0]}XX$") end default_range_error_case = default_range_entry&.map { |_, error_case_instance| error_case_instance } default_range_error_case[0].raise_exception(response) unless default_range_error_case.nil? || default_range_error_case.empty? # Handling default error case if configured default_error_case = error_cases['default'] default_error_case&.raise_exception(response) end |
#xml_attribute(xml_attribute) ⇒ ResponseHandler
Set the xml_attribute property.
80 81 82 83 |
# File 'lib/apimatic-core/response_handler.rb', line 80 def xml_attribute(xml_attribute) @xml_attribute = xml_attribute self end |