Class: APIMatchers::ResponseBody::HaveXmlNode

Inherits:
Base
  • Object
show all
Defined in:
lib/api_matchers/response_body/have_xml_node.rb

Instance Attribute Summary

Attributes inherited from Base

#actual, #expected_node, #setup

Instance Method Summary collapse

Methods inherited from Base

#added_message, #failure_message, #failure_message_when_negated, #including_text, #initialize, #response_body, #with

Constructor Details

This class inherits a constructor from APIMatchers::ResponseBody::Base

Instance Method Details

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/api_matchers/response_body/have_xml_node.rb', line 6

def matches?(actual)
  value = false
  @actual = actual
  xml = Nokogiri::XML(response_body)

  node_set = xml.xpath("//#{@expected_node}")
  if node_set
    node_set.each do |node|
      if @with_value
        value = (node.text == @with_value.to_s)
      elsif @expected_including_text
        value = (node.text.to_s.include?(@expected_including_text))
      else
        value = node.text.present?
      end
      # if value is true, time to return
      return value if value
    end
  end
  # at this point, it failed to match
  return value
end