Module: XmlParsingHelper

Included in:
Rubillow::Models::Base, Rubillow::Models::DeepSearchResult
Defined in:
lib/rubillow/helpers/xml_parsing_helper.rb

Overview

Helper methods for parsing XML.

Instance Method Summary collapse

Instance Method Details

#xpath_if_present(path, attribute, xml, nil_value = nil) ⇒ String

is nil or if the node is not present.

If the node is not present, return nil or nil_value if specified.

Parameters:

  • path (String)

    Xpath query to find the node in XML.

  • attribute (Symbol)

    Attribute on the node to call and return as the value.

  • xml (Nokogiri::XML)

    A nokogiri parser object to call #xpath.

  • nil_value (optional) (defaults to: nil)

    A value to return if the attribute on the node

Returns:

  • (String)

    Value from an attribute of an xpath node, if present.



11
12
13
14
# File 'lib/rubillow/helpers/xml_parsing_helper.rb', line 11

def xpath_if_present(path, attribute, xml, nil_value = nil)
  text = xml.xpath(path).first.send(attribute) unless xml.xpath(path).empty?
  text ||= nil_value
end