lib/aptly_command.rb in aptly_cli-0.5.0 vs lib/aptly_command.rb in aptly_cli-0.6.0

- old
+ new

@@ -1,6 +1,10 @@ module AptlyCli + class HttpError < StandardError; end + class HttpNotFoundError < HttpError; end + class HttpInternalServerError < HttpError; end + class AptlyCommand include HTTParty attr_accessor :config def initialize(config, options = nil) @@ -61,8 +65,44 @@ self.class.basic_auth @config[:username].to_s, @config[:password].to_s end end self.class.debug_output @config[:debug] ? $stdout : nil + end + + def delete(path, options = {}) + response = self.class.delete(path, options) + process_response(response) + end + + def get(path, options = {}) + response = self.class.get(path, options) + process_response(response) + end + + def post(path, options = {}) + response = self.class.post(path, options) + process_response(response) + end + + def put(path, options = {}) + response = self.class.put(path, options) + process_response(response) + end + + def process_response(response) + json_response = JSON.parse response.body + + raise "[Server] #{json_response['error']}" unless !json_response.is_a?(Hash) || json_response.dig('error').nil? + + raise HttpNotFoundError, "#{json_response}" if response.code == 404 + raise HttpInternalServerError, "#{json_response}" if response.code == 500 + + response + rescue JSON::ParserError + raise HttpNotFoundError, "#{json_response}" if response.code == 404 + raise HttpInternalServerError, "#{json_response}" if response.code == 500 + + response end end end