lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.52 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.53
- old
+ new
@@ -226,11 +226,11 @@
# Returns the value at the provided xpath
# @param [RestClient::Response] response
# @param [String] xpath
# @return [String] Value inside element found through Xpath
- def xpath_value_for(response: nil, xpath: nil)
+ def xpath_value_for(response: nil, xpath: nil, attribute: nil)
raise ArgumentError unless response && xpath
raise "Can't perform XPATH if response is not XML" unless Interpreter.response_type_for(response) == :xml
result =
if Soaspec.strip_namespaces? && !xpath.include?(':')
temp_doc = Nokogiri.parse response.body
@@ -238,30 +238,37 @@
temp_doc.xpath(xpath).first
else
Nokogiri.parse(response.body).xpath(xpath).first
end
raise NoElementAtPath, "No value at Xpath '#{xpath}'" unless result
- result.inner_text
+ return result.inner_text if attribute.nil?
+ result.attributes[attribute].inner_text
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 [Response] response
# @param [Object] path Xpath, JSONPath or other path identifying how to find element
+ # @param [String] attribute Generic attribute to find. Will override path
# @return [String] Value at Xpath
- def value_from_path(response, path)
+ def value_from_path(response, path, attribute: nil)
path = path.to_s
+
case Interpreter.response_type_for(response)
when :xml
+ path = "//*[@#{attribute}]" unless attribute.nil?
path = '//' + path if path[0] != '/'
- xpath_value_for(response: response, xpath: path)
+ xpath_value_for(response: response, xpath: path, attribute: attribute)
when :json
+ raise 'JSON does not support attributes' if attribute
path = '$..' + path if path[0] != '$'
matching_values = JsonPath.on(response.body, path)
raise NoElementAtPath, "Element in #{response.body} not found with path '#{path}'" if matching_values.empty?
matching_values.first
+ when :hash
+ response.dig(path.split('.')) # Use path as Hash dig expression separating params via '.' TODO: Unit test
else
- raise 'Unrecognised response message. Neither xml nor json detected'
+ response.to_s[/path/] # Perform regular expression using path if not XML nor JSON TODO: Unit test
end
end
# Convenience methods for once off usage of a REST request
class << self
\ No newline at end of file