Open mfenner opened 10 years ago
I use this approach (https://github.com/articlemetrics/lagotto/blob/master/app/models/concerns/networkable.rb#L198-L216):
def parse_error_response(string)
if is_json?(string)
string = JSON.parse(string)
elsif is_xml?(string)
string = Hash.from_xml(string)
end
string = string['error'] if string.is_a?(Hash) && string['error']
string
end
def is_xml?(string)
Nokogiri::XML(string).errors.empty?
end
def is_json?(string)
JSON.parse(string)
rescue JSON::ParserError
false
end
end