lib/soaspec/exchange_handlers/soap_handler.rb in soaspec-0.0.78 vs lib/soaspec/exchange_handlers/soap_handler.rb in soaspec-0.0.79

- old
+ new

@@ -156,43 +156,48 @@ end # Returns the value at the provided xpath # @param [Savon::Response] response # @param [String] xpath - # @param [String] attribute Generic attribute to find - # @return [String] Value inside element found through Xpath - def xpath_value_for(response: nil, xpath: nil, attribute: nil) + # @return [Enumerable] Elements found through Xpath + def xpath_elements_for(response: nil, xpath: nil, attribute: nil) raise ArgumentError('response and xpath must be passed to method') unless response && xpath + xpath = "//*[@#{attribute}]" unless attribute.nil? + xpath = '//' + xpath if xpath[0] != '/' temp_doc = response.doc.dup convert_to_lower_case(temp_doc) if convert_to_lower? - result = if Soaspec.strip_namespaces? && !xpath.include?(':') - temp_doc.remove_namespaces! - temp_doc.at_xpath(xpath) - else - temp_doc.at_xpath(xpath, temp_doc.collect_namespaces) - end - raise NoElementAtPath, "No value at Xpath '#{xpath}' in XML #{response.doc}" unless result - return result.inner_text if attribute.nil? - result.attributes[attribute].inner_text + if Soaspec.strip_namespaces? && !xpath.include?(':') + temp_doc.remove_namespaces! + temp_doc.xpath(xpath) + else + temp_doc.xpath(xpath, temp_doc.collect_namespaces) + end end # Based on a exchange, return the value at the provided xpath # If the path does not begin with a '/', a '//' is added to it # @param [Savon::Response] response # @param [String] path Xpath # @param [String] attribute Generic attribute to find. Will override path # @return [String] Value at Xpath def value_from_path(response, path, attribute: nil) - path = "//*[@#{attribute}]" unless attribute.nil? - path = '//' + path if path[0] != '/' - xpath_value_for(response: response, xpath: path, attribute: attribute) + results = xpath_elements_for(response: response, xpath: path, attribute: attribute) + raise NoElementAtPath, "No value at Xpath '#{path}' in XML #{response.doc}" if results.empty? + return results.first.inner_text if attribute.nil? + results.first.attributes[attribute].inner_text end - # Whether any of the keys of the Body Hash include value + # @return [Enumerable] List of values returned from path + def values_from_path(response, path, attribute: nil) + xpath_elements_for(response: response, xpath: path, attribute: attribute).map(&:inner_text) # { |e| e.inner_text } + end + + # alias elements xpath_elements_for + + # @return [Boolean] Whether any of the keys of the Body Hash include value def include_value?(response, expected_value) response.body.include_value?(expected_value) end - end # Deprecated class name. Will be removed in the future class BasicSoapHandler < SoapHandler \ No newline at end of file