class BWAPI::BWError
BW error class to capture BWAPI
error responses
Public Class Methods
new(response = nil)
click to toggle source
Calls superclass method
# File lib/bwapi/error.rb, line 4 def initialize(response = nil) @errors = [] valid_response?(response) @errors.empty? ? super() : super(@errors.join(', ')) end
Public Instance Methods
errors_keys?(body)
click to toggle source
Check if response has known errors keys
@param object [Object] response object to process for errors
# File lib/bwapi/error.rb, line 23 def errors_keys?(body) if body.key?('error') && body.key?('error_description') body elsif body.key?('errors') body['errors'] end end
parse_errors(body)
click to toggle source
Parses errors based on error body passed
@param body [Hash] errors
# File lib/bwapi/error.rb, line 34 def parse_errors(body) verify_type body end
split_array_errors(array)
click to toggle source
Iterates through errors in array
@param array [Array] array to iterate
# File lib/bwapi/error.rb, line 55 def split_array_errors(array) array.each_with_index { |_e, i| verify_type array[i] } end
split_hash_errors(hash)
click to toggle source
Iterates through errors in hash
@param hash [Hash] hash to iterate
# File lib/bwapi/error.rb, line 62 def split_hash_errors(hash) message = [] hash.each { |k, v| message << "#{k}: #{v}" } @errors << message.flatten.join(' with ') end
valid_response?(response)
click to toggle source
Check if response is valid
@param object [Object] response object to check for errors
# File lib/bwapi/error.rb, line 13 def valid_response?(response) return nil if response.nil? return nil unless response.body.is_a?(Object) && response.respond_to?(:body) return nil unless response.body.is_a?(Hash) parse_errors(errors_keys?(response.body)) unless response.body.nil? end
verify_type(type)
click to toggle source
Verifies type
@param object [Object] type to determine
# File lib/bwapi/error.rb, line 41 def verify_type(type) case type when Array split_array_errors(type) when Hash split_hash_errors(type) when String @errors << type end end