Created
August 3, 2022 13:55
-
-
Save sadfuzzy/4e6a036d91b35b359f54050213217b58 to your computer and use it in GitHub Desktop.
XMLResultParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class XMLResultParser | |
class << self | |
delegate :parse_xml, :xml_result?, to: :new | |
end | |
def parse_xml(result) | |
parsed_result = parse_result(result) | |
if xml_result?(result) | |
result | |
elsif xml_result?(parsed_result) | |
parsed_result | |
else | |
result | |
end | |
end | |
def xml_result?(result) | |
xml = Nokogiri::XML(result) | |
xml.errors.empty? && xml.root.present? | |
end | |
private | |
def parse_result(result) | |
string = String(result) | |
string = unescape_unicode_escape_sequences(string) | |
string = remove_line_breaks(string) | |
string = remove_backlashes(string) | |
string = remove_whitespace(string) | |
string | |
end | |
# https://stackoverflow.com/a/7017226 | |
def unescape_unicode_escape_sequences(string) | |
string.gsub(/\\u([\da-fA-F]{4})/) { [$1].pack('H*').unpack('n*').pack('U*') } | |
end | |
def remove_line_breaks(string) | |
string.gsub(/\\n/, '') | |
end | |
def remove_backlashes(string) | |
string.delete('\\') | |
end | |
# https://stackoverflow.com/questions/65025151/remove-whitespace-from-xml-document-using-ruby#comment114961832_65025151 | |
def remove_whitespace(string) | |
string.split.join(' ').gsub('> <', '><') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment