Module: Ruspider::XmlUtils
- Included in:
- InchIAPI, MassSpecAPI
- Defined in:
- lib/ruspider/xml_utils.rb
Overview
XML utilities
Instance Method Summary collapse
- #array_from_xml(xml_str, obj_name, field) ⇒ Object
- #get_first_node(xml, node_name) ⇒ Object
- #get_nodes(xml, node_name) ⇒ Object
- #hash_from_string(xml_str, obj_name, field) ⇒ Object
- #hash_from_xml(xml, field) ⇒ Object
Instance Method Details
#array_from_xml(xml_str, obj_name, field) ⇒ Object
34 35 36 37 38 |
# File 'lib/ruspider/xml_utils.rb', line 34 def array_from_xml(xml_str, obj_name, field) xml = get_nodes(Nokogiri::XML(xml_str), obj_name) arr = xml.xpath("//cs:#{field}", 'cs' => 'http://www.chemspider.com/') arr.map(&:content) end |
#get_first_node(xml, node_name) ⇒ Object
15 16 17 |
# File 'lib/ruspider/xml_utils.rb', line 15 def get_first_node(xml, node_name) get_nodes(xml, node_name).first end |
#get_nodes(xml, node_name) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/ruspider/xml_utils.rb', line 8 def get_nodes(xml, node_name) xml.xpath( "//cs:#{node_name}", 'cs' => 'http://www.chemspider.com/' ) end |
#hash_from_string(xml_str, obj_name, field) ⇒ Object
29 30 31 32 |
# File 'lib/ruspider/xml_utils.rb', line 29 def hash_from_string(xml_str, obj_name, field) xml = get_first_node(Nokogiri::XML(xml_str), obj_name) hash_from_xml(xml, field) end |
#hash_from_xml(xml, field) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/ruspider/xml_utils.rb', line 19 def hash_from_xml(xml, field) hash = {} field.each do |k, v| node = xml.xpath("//cs:#{k}", 'cs' => 'http://www.chemspider.com/') hash[v.to_sym] = node.first.content end hash end |