-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add response validator add corresponding test
- Loading branch information
Showing
6 changed files
with
141 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
lib/ruby_api_pack_cloudways/handlers/response_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module RubyApiPackCloudways | ||
module Handlers | ||
module ResponseValidator | ||
def validate_response(response, expected_key) | ||
raise "Unexpected response format: #{response.inspect}" unless response.is_a?(Hash) && response.key?(expected_key) | ||
|
||
result = response[expected_key] | ||
raise "Expected '#{expected_key}' to be an Array, got #{result.class}: #{result.inspect}" unless result.is_a?(Array) | ||
|
||
result | ||
rescue StandardError => e | ||
log_error("Error validating response: #{e.message}") | ||
raise "An error occurred while processing the response: #{e.message}" | ||
end | ||
|
||
private | ||
|
||
def log_error(message) | ||
if defined?(Rails) | ||
Rails.logger.error(message) | ||
else | ||
puts "[ERROR] #{message}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.