Sha256: 8a6f0f55e8aefd4cf85c2769f9a4e09149cbe34a4fc7344a936f127035156088

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'hermod'

require 'minitest/autorun'
require 'pry-rescue/minitest'

require 'nokogiri'

# Public: Returns the value of a given node from an xml payload.
#
# node_name - xml node name, for example: FinalSubmission
#
# Returns a String
def value_of_node(node_name)
  value_of_nodes(node_name).first
end

# Public: Returns an array of the values of nodes in an xml payload.
#
# node_name - xml node name, for example: NIletter
#
# Returns an Array
def value_of_nodes(node_name)
  ns = nodes(node_name)
  ns.map do |n|
    raise "failed to find #{node_name.inspect} in #{subject.inspect}" if n.nil?
    n.content
  end
end

# Public: Returns the number of nodes with the given name in the payload
#
# node_name - xml node name, for example: NIletters
#
# Returns an Integer
def number_of_nodes(node_name)
  nodes(node_name).count
end

# Public: Returns the Nokogiri.XML node for the given name.
#
# node_name - xml node name, for example: FinalSubmission
#
# Returns a Nokogiri::XML::Node
def node(node_name)
  nodes(node_name).first
end

# Public: Returns a list of Nokogiri.XML nodes for the given name.
#
# node_name - xml node name, for example: NIletter
#
# Returns an Array of Nokogiri::XML::Node objects
def nodes(node_name)
  Nokogiri.XML(subject.to_xml.to_s).remove_namespaces!.css(node_name)
end

# Public: Get the nth node in the document
#
# index - the zero-based index of the node you wish to retrieve
#
# Returns an XML::Node (from LibXML)
def node_by_index(index)
  subject.to_xml.to_a[index]
end

# Public: Returns a hash of attributes on the node with the given name
#
# node_name - xml node name, for example: NIletter
#
# Returns a Hash
def attributes_for_node(node_name)
  node(node_name).attributes
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hermod-1.0.2 spec/minitest_helper.rb
hermod-1.0.1 spec/minitest_helper.rb