lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.45 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.46

- old
+ new

@@ -172,11 +172,11 @@ extract_hash(response).include_value? expected end # @return [Boolean] Whether response body contains expected key def include_key?(response, expected) - extract_hash(response).include_key? expected + value_from_path(response, expected) end # @return [Integer] HTTP Status code for response def status_code_for(response) response.code @@ -200,47 +200,46 @@ def root_attributes nil end # Returns the value at the provided xpath - # @param [Exchange] exchange + # @param [RestClient::Response] response # @param [String] xpath # @return [String] Value inside element found through Xpath - def xpath_value_for(exchange: nil, xpath: nil) - raise ArgumentError unless exchange && xpath - response = exchange.response + def xpath_value_for(response: nil, xpath: 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 temp_doc.remove_namespaces! temp_doc.xpath(xpath).first else Nokogiri.parse(response.body).xpath(xpath).first end - raise NoElementAtXpath, "No value at Xpath '#{xpath}'" unless result + raise NoElementAtPath, "No value at Xpath '#{xpath}'" unless result result.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 [Exchange] exchange - # @param [Object] path Xpath or other path identifying how to find element + # @param [Object] path Xpath, JSONPath or other path identifying how to find element # @return [String] Value at Xpath - def value_from_path(exchange, path) - case Interpreter.response_type_for(exchange.response) + def value_from_path(response, path) + path = path.to_s + case Interpreter.response_type_for(response) when :xml path = '//' + path if path[0] != '/' - xpath_value_for(exchange: exchange, xpath: path) + xpath_value_for(response: response, xpath: path) when :json path = '$..' + path if path[0] != '$' - matching_values = JsonPath.on(exchange.response.body, path) - raise NoElementInHash, "Element in #{exchange.response.body} not found with path '#{path}'" if matching_values.empty? + 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 else raise 'Unrecognised response message. Neither xml nor json detected' end - end # Convenience methods for once off usage of a REST request class << self \ No newline at end of file