Skip to content

Commit

Permalink
add response validator add corresponding test
Browse files Browse the repository at this point in the history
  • Loading branch information
bradpotts committed Nov 17, 2024
1 parent a38e0b1 commit 5dbace0
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 98 deletions.
24 changes: 13 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (7.2.2)
activesupport (8.0.0)
base64
benchmark (>= 0.3)
bigdecimal
Expand All @@ -20,11 +20,12 @@ GEM
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
uri (>= 0.13.1)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
base64 (0.2.0)
benchmark (0.3.0)
benchmark (0.4.0)
bigdecimal (3.1.8)
capybara (3.40.0)
addressable
Expand All @@ -49,32 +50,32 @@ GEM
activesupport (>= 5.0.0)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
hashdiff (1.1.1)
hashdiff (1.1.2)
httparty (0.22.0)
csv
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
json (2.7.5)
json (2.8.2)
language_server-protocol (3.17.0.3)
logger (1.6.1)
matrix (0.4.2)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
mini_portile2 (2.8.8)
minitest (5.25.1)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
nio4r (2.7.4)
nokogiri (1.16.7)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
oj (3.16.6)
oj (3.16.7)
bigdecimal (>= 3.0)
ostruct (>= 0.2)
ostruct (0.6.0)
ostruct (0.6.1)
parallel (1.26.3)
parser (3.3.5.1)
parser (3.3.6.0)
ast (~> 2.4.1)
racc
public_suffix (6.0.1)
Expand Down Expand Up @@ -111,17 +112,17 @@ GEM
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.33.0)
rubocop-ast (1.36.1)
parser (>= 3.3.1.0)
rubocop-performance (1.22.1)
rubocop-performance (1.23.0)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (3.2.0)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
securerandom (0.3.1)
securerandom (0.3.2)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -133,6 +134,7 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)
uri (1.0.2)
vcr (6.3.1)
base64
webmock (3.24.0)
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_api_pack_cloudways.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'oj'

require_relative 'ruby_api_pack_cloudways/configuration'
require_relative 'ruby_api_pack_cloudways/handlers/response_validator'
require_relative 'ruby_api_pack_cloudways/connection/cw_token'
require_relative 'ruby_api_pack_cloudways/connection/cw_connect'
require_relative 'ruby_api_pack_cloudways/api/cw_lists'
Expand Down
24 changes: 14 additions & 10 deletions lib/ruby_api_pack_cloudways/api/cw_lists.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# frozen_string_literal: true

require 'ruby_api_pack_cloudways/handlers/response_validator'

module RubyApiPackCloudways
module Api
class CwLists
extend RubyApiPackCloudways::Handlers::ResponseValidator

# List endpoints
ENDPOINTS = {
apps: '/apps',
Expand All @@ -19,52 +23,52 @@ class CwLists

# Fetch list of applications available
def self.app_list
fetch_resource(ENDPOINTS[:apps])['apps']
validate_response(fetch_resource(ENDPOINTS[:apps]), 'apps')
end

# Fetch list of available backup frequencies
def self.backup_frequency_list
fetch_resource(ENDPOINTS[:backup_frequencies])['frequencies']
validate_response(fetch_resource(ENDPOINTS[:backup_frequencies]), 'frequencies')
end

# Fetch list of supported countries
def self.country_list
fetch_resource(ENDPOINTS[:countries])['countries']
validate_response(fetch_resource(ENDPOINTS[:countries]), 'countries')
end

# Fetch list of monitoring durations
def self.monitor_duration_list
fetch_resource(ENDPOINTS[:monitor_durations])['durations']
validate_response(fetch_resource(ENDPOINTS[:monitor_durations]), 'durations')
end

# Fetch list of monitoring targets
def self.monitor_target_list
fetch_resource(ENDPOINTS[:monitor_targets])['targets']
validate_response(fetch_resource(ENDPOINTS[:monitor_targets]), 'targets')
end

# Fetch list of available packages
def self.package_list
fetch_resource(ENDPOINTS[:packages])['packages']
validate_response(fetch_resource(ENDPOINTS[:packages]), 'packages')
end

# Fetch list of providers
def self.provider_list
fetch_resource(ENDPOINTS[:providers])['providers']
validate_response(fetch_resource(ENDPOINTS[:providers]), 'providers')
end

# Fetch list of regions
def self.region_list
fetch_resource(ENDPOINTS[:regions])['regions']
validate_response(fetch_resource(ENDPOINTS[:regions]), 'regions')
end

# Fetch list of server sizes
def self.server_size_list
fetch_resource(ENDPOINTS[:server_sizes])['sizes']
validate_response(fetch_resource(ENDPOINTS[:server_sizes]), 'sizes')
end

# Fetch list of available settings
def self.setting_list
fetch_resource(ENDPOINTS[:settings])['settings']
validate_response(fetch_resource(ENDPOINTS[:settings]), 'settings')
end

# Private method to fetch resources from a specified endpoint
Expand Down
29 changes: 29 additions & 0 deletions lib/ruby_api_pack_cloudways/handlers/response_validator.rb
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
95 changes: 18 additions & 77 deletions spec/ruby_api_pack_cloudways/api/cw_lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,113 +10,54 @@
allow(RubyApiPackCloudways::Connection::CwConnect).to receive(:new).and_return(cw_connect_instance)
end

describe '.provider_list' do
it 'returns a list of providers' do
shared_examples 'a list fetcher' do |method, endpoint_key, expected_result|
it "returns a list of #{endpoint_key}" do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'providers' => ['provider1']
endpoint_key => expected_result
)

providers = described_class.provider_list
expect(providers).to eq(['provider1'])
result = described_class.public_send(method)
expect(result).to eq(expected_result)
end
end

describe '.server_size_list' do
it 'returns a list of server sizes' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'sizes' => ['size1']
)
describe '.provider_list' do
it_behaves_like 'a list fetcher', :provider_list, 'providers', ['provider1']
end

sizes = described_class.server_size_list
expect(sizes).to eq(['size1'])
end
describe '.server_size_list' do
it_behaves_like 'a list fetcher', :server_size_list, 'sizes', ['size1']
end

describe '.app_list' do
it 'returns a list of apps' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'apps' => ['app1']
)

apps = described_class.app_list
expect(apps).to eq(['app1'])
end
it_behaves_like 'a list fetcher', :app_list, 'apps', ['app1']
end

describe '.package_list' do
it 'returns a list of packages' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'packages' => ['package1']
)

packages = described_class.package_list
expect(packages).to eq(['package1'])
end
it_behaves_like 'a list fetcher', :package_list, 'packages', ['package1']
end

describe '.backup_frequency_list' do
it 'returns a list of backup frequencies' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'frequencies' => %w[daily weekly]
)

frequencies = described_class.backup_frequency_list
expect(frequencies).to eq(%w[daily weekly])
end
it_behaves_like 'a list fetcher', :backup_frequency_list, 'frequencies', %w[daily weekly]
end

describe '.country_list' do
it 'returns a list of countries' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'countries' => %w[Country1 Country2]
)

countries = described_class.country_list
expect(countries).to eq(%w[Country1 Country2])
end
it_behaves_like 'a list fetcher', :country_list, 'countries', %w[Country1 Country2]
end

describe '.monitor_duration_list' do
it 'returns a list of monitor durations' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'durations' => %w[5min 15min]
)

durations = described_class.monitor_duration_list
expect(durations).to eq(%w[5min 15min])
end
it_behaves_like 'a list fetcher', :monitor_duration_list, 'durations', %w[5min 15min]
end

describe '.monitor_target_list' do
it 'returns a list of monitor targets' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'targets' => %w[target1 target2]
)

targets = described_class.monitor_target_list
expect(targets).to eq(%w[target1 target2])
end
it_behaves_like 'a list fetcher', :monitor_target_list, 'targets', %w[target1 target2]
end

describe '.region_list' do
it 'returns a list of regions' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'regions' => %w[region1 region2]
)

regions = described_class.region_list
expect(regions).to eq(%w[region1 region2])
end
it_behaves_like 'a list fetcher', :region_list, 'regions', %w[region1 region2]
end

describe '.setting_list' do
it 'returns a list of settings' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'settings' => %w[setting1 setting2]
)

settings = described_class.setting_list
expect(settings).to eq(%w[setting1 setting2])
end
it_behaves_like 'a list fetcher', :setting_list, 'settings', %w[setting1 setting2]
end
end
Loading

0 comments on commit 5dbace0

Please sign in to comment.