lib/solusvm/base.rb in solusvm-1.0.0.beta2 vs lib/solusvm/base.rb in solusvm-1.0.0.beta3
- old
+ new
@@ -26,11 +26,11 @@
end
http.start do |http|
request = Net::HTTP::Get.new("#{api_endpoint.path}?#{options.to_query}")
response = http.request(request)
- handle_errors(response.body)
+ handle_errors(response)
@returned_parameters = parse_response(response.body, force_array)
log_messages(options)
end
successful?
end
@@ -43,24 +43,36 @@
force_array = Array(force_array) if force_array
body = "<solusrequest>#{body}</solusrequest>"
XmlSimple.xml_in(body, 'ForceArray' => force_array)
end
+ # Parses a returned_parameters value as a list, if present.
+ def parse_returned_params_as_list(attribute)
+ if returned_parameters[attribute] && !returned_parameters[attribute].empty?
+ returned_parameters[attribute].to_s.split(',')
+ end
+ end
+
# Look for known error messages
- def handle_errors(body)
- case body.downcase
- when /invalid ipaddress/i
- raise "This IP is not authorized to use the API"
- when /Invalid id or key/i
- raise "Invalid ID or key"
- when /Node not found/i
- raise "Node does not exist"
+ def handle_errors(response)
+ if (200..299).include? response.code.to_i
+ # Checks for application errors
+ case response.body.downcase
+ when /invalid ipaddress/i
+ raise "This IP is not authorized to use the API"
+ when /Invalid id or key/i
+ raise "Invalid ID or key"
+ when /Node not found/i
+ raise "Node does not exist"
+ end
+ else
+ raise SolusvmError, "Bad HTTP Status: #{response.code}"
end
end
# Returns true when a request has been successful
- #
+ #
# my_class = MyClass.new
# my_class.create_server('example.com')
# my_class.successful? # => true
def successful?
returned_parameters['status'] == 'success'
@@ -75,11 +87,11 @@
{:id => Solusvm.api_id, :key => Solusvm.api_key}
end
def log_messages(options)
logger, logger_method = Solusvm.api_options[:logger], Solusvm.api_options[:logger_method]
-
+
if logger && logger.respond_to?(logger_method)
logger.send(logger_method, "[Start] => #{options[:action]}")
returned_parameters.each do |k,v|
logger.send(logger_method, " #{k} => #{v}")
end
@@ -92,10 +104,10 @@
returned_parameters['statusmsg']
end
# Raises an exception unless a valid type is specified
def validate_server_type!(type)
- type.strip!
+ type = type.strip
unless VALID_SERVER_TYPES.include?(type)
raise SolusvmError, "Invalid Virtual Server type: #{type}"
end
end
end