module LUSI::API::Core::XML

Constants

NAMESPACE

Public Class Methods

lookup(xml = nil, lookup = nil, service = nil, path = nil, default = nil) { |result| ... } click to toggle source
# File lib/lusi_api/core/xml.rb, line 12
def self.lookup(xml = nil, lookup = nil, service = nil,  path = nil, default = nil, &block)
  return default if xml.nil? || lookup.nil? || service.nil?
  key = xml_content_at(xml, path)
  result = lookup.lookup(service, key, default)
  yield(result) if block_given?
  result
end
xml(xml = nil, path = nil, default = nil, filter: nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 20
def self.xml(xml = nil, path = nil, default = nil, filter: nil, &block)
  _xml(xml, path, default, single: false, content: false, filter: filter, &block)
end
xml_at(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 24
def self.xml_at(xml = nil, path = nil, default = nil, &block)
  _xml(xml, path, default, single: true, content: false, &block)
end
xml_boolean(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 28
def self.xml_boolean(xml = nil, path = nil, default = nil, &block)
  content = xml_content(xml, path, default)
  content.map { |str| xml_boolean_parse(str, default) }
end
xml_boolean_at(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 33
def self.xml_boolean_at(xml = nil, path = nil, default = nil, &block)
  xml_boolean_parse(xml_content_at(xml, path, default), default)
end
xml_boolean_parse(boolstr = nil, default = nil, true_values = nil, false_values = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 37
def self.xml_boolean_parse(boolstr = nil, default = nil, true_values = nil, false_values = nil, &block)
  boolstr = boolstr.to_s.downcase
  false_values ||= ['false', 'n', 'no']
  true_values ||= ['true', 'y', 'yes']
  if true_values.include?(boolstr)
    true
  elsif false_values.include?(boolstr)
    false
  else
    default
  end
end
xml_content(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 50
def self.xml_content(xml = nil, path = nil, default = nil, &block)
  _xml(xml, path, default, single: false, content: true, &block)
end
xml_content_at(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 54
def self.xml_content_at(xml = nil, path = nil, default = nil, &block)
  _xml(xml, path, default, single: true, content: true, &block)
end
xml_datetime(xml = nil, path = nil, default = nil, format = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 58
def self.xml_datetime(xml = nil, path = nil, default = nil, format = nil, &block)
  content = xml_content(xml, path, default)
  content.map { |str| xml_datetime_parse(str, default, format) }
end
xml_datetime_at(xml = nil, path = nil, default = nil, format = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 63
def self.xml_datetime_at(xml = nil, path = nil, default = nil, format = nil, &block)
  xml_datetime_parse(xml_content_at(xml, path, default), default, format)
end
xml_datetime_parse(datestr = nil, default = nil, format = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 67
def self.xml_datetime_parse(datestr = nil, default = nil, format = nil, &block)
  return default if datestr.nil?
  format ||= '%Y-%m-%dT%H:%M:%S'
  begin
    DateTime.strptime(datestr, format)
  rescue Exception
    default
  end
end
xml_float(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 77
def self.xml_float(xml = nil, path = nil, default = nil, &block)
  content = xml_content(xml, path, default)
  content.map { |str| xml_float_parse(str, default) }
end
xml_float_at(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 82
def self.xml_float_at(xml = nil, path = nil, default = nil, &block)
  xml_float_parse(xml_content_at(xml, path, default), default)
end
xml_float_parse(floatstr, default = nil) click to toggle source
# File lib/lusi_api/core/xml.rb, line 86
def self.xml_float_parse(floatstr, default = nil)
  begin
    floatstr.to_f
  rescue
    default
  end
end
xml_int(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 94
def self.xml_int(xml = nil, path = nil, default = nil, &block)
  content = xml_content(xml, path, default)
  content.map { |str| xml_int_parse(str, default) }
end
xml_int_at(xml = nil, path = nil, default = nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 99
def self.xml_int_at(xml = nil, path = nil, default = nil, &block)
  xml_int_parse(xml_content_at(xml, path, default), default)
end
xml_int_parse(intstr, default = nil) click to toggle source
# File lib/lusi_api/core/xml.rb, line 103
def self.xml_int_parse(intstr, default = nil)
  begin
    intstr.to_i
  rescue
    default
  end
end

Protected Class Methods

_xml(xml = nil, path = nil, default = nil, single: false, content: true, filter: nil, &block) click to toggle source
# File lib/lusi_api/core/xml.rb, line 113
def self._xml(xml = nil, path = nil, default = nil, single: false, content: true, filter: nil, &block)

  # Return the default value if no XML is given
  return default if xml.nil?

  # Return the XML value itself if it's not an accepted XML instance
  return xml unless xml.is_a?(Nokogiri::XML::Node) or xml.is_a?(Nokogiri::XML::NodeSet)

  if single
    # Return the first matching node
    result = xml.at_xpath(path)
    if result.nil?
      default
    else
      content ? result.content.to_s : result
    end
  else
    # If a code block is provided, return an array of results of applying the block to each node, or an empty
    # array if no matches exist.
    # Otherwise return the matching nodeset, or nil of no matches exist
    nodeset = xml.xpath(path)
    if block
      if nodeset.nil? then
        # Return an empty array for an empty nodeset
        []
      elsif filter.nil?
        # Apply the block to every node
        if content
          nodeset.to_a.map { |node| block.call(node.content.to_s) }
        else
          nodeset.to_a.map(&block)
        end
      else
        # Apply the block only to nodes for which the filter returns true
        result = []
        if content
          nodeset.each { |node| result.push(node.content.to_s) if filter.call(node) }
        else
          nodeset.each { |node| result.push(block.call(node)) if filter.call(node) }
        end
        result
      end
    else
      if content
        nodeset.to_a.map { |node| node.content.to_s }
      else
        nodeset
      end
    end
  end
end