-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxml_parse.rb
More file actions
39 lines (33 loc) · 1.04 KB
/
xml_parse.rb
File metadata and controls
39 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def getElement(xml, query)
xml_doc = Nokogiri::XML(xml)
xml_doc.xpath(query)
end
def appendFrame(string)
"<documentfragmentcontainer>#{string}</documentfragmentcontainer>"
end
def getHTML(xml)
xml_doc = Nokogiri::XML(appendFrame(xml))
xml_doc.xpath('//documentfragmentcontainer').inner_html
end
def getJson(text)
JSON.parse(text)
end
def getGQLJSONRTE(node, item = '""')
normalized_node = node
if node.is_a?(Hash) && node["type"] != "doc" && node[:type] != "doc"
normalized_node = { "type" => "doc", "children" => [node] }
end
node_payload = normalized_node.is_a?(String) ? normalized_node : JSON.generate(normalized_node)
item_payload = item.is_a?(String) ? item : JSON.generate(item)
entry = "{
\"single_rte\": {
\"json\": #{node_payload},
\"embedded_itemsConnection\": #{item_payload}
},
\"multiple_rte\": {
\"json\": [#{node_payload}],
\"embedded_itemsConnection\": #{item_payload}
}
}"
getJson(entry)
end