Method: Soaspec::RestHandler#value_from_path

Defined in:
lib/soaspec/exchange_handlers/rest_handler.rb

#value_from_path(response, path, attribute: nil) ⇒ String

Based on a exchange, return the value at the provided xpath If the path does not begin with a ‘/’, a ‘//’ is added to it

Parameters:

  • response (Response)
  • path (Object)

    Xpath, JSONPath or other path identifying how to find element

  • attribute (String) (defaults to: nil)

    Generic attribute to find. Will override path

Returns:

  • (String)

    Value at Xpath



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 268

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, 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
    response.to_s[/path/] # Perform regular expression using path if not XML nor JSON TODO: Unit test
  end
end