Skip to content

Instantly share code, notes, and snippets.

@goromlagche
Created September 1, 2021 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goromlagche/69f04033575bac9baa67bf289399953b to your computer and use it in GitHub Desktop.
Save goromlagche/69f04033575bac9baa67bf289399953b to your computer and use it in GitHub Desktop.
require 'nori'
single_comment = <<~EOF
<?xml version="1.0" encoding="UTF-8"?>
<comments>
<comment>
<description>TEST COMMENT 123</description>
</comment>
</comments>
EOF
multiple_comments = <<~EOF
<?xml version="1.0" encoding="UTF-8"?>
<comments>
<comment>
<description>TEST COMMENT 123</description>
</comment>
<comment>
<description>TEST COMMENT 456</description>
</comment>
</comments>
EOF
def fetch_descriptions_1(raw_data:)
comments = Nori.new.parse(raw_data)['comments']['comment']
if comments.is_a?(Array)
comments.map { |comment| comment['description'] }
else
[comments['description']]
end
end
def fetch_descriptions_2(raw_data:)
comments = [Nori.new.parse(raw_data)['comments']['comment']].flatten
comments.map { |comment| comment['description'] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment