lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.2.16 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.2.17
- old
+ new
@@ -93,17 +93,10 @@
[header_name, ERB.new(header_value).result(binding)]
end]
end
- # Convert snakecase to PascalCase
- def convert_to_pascal_case(key)
- return key if /[[:upper:]]/ =~ key[0] # If first character already capital, don't do conversion
-
- key.split('_').map(&:capitalize).join
- end
-
# Initialize value of merged options
# @return [Hash] Hash of merged options
def init_merge_options
options = rest_resource_options
options.merge! basic_auth_params if respond_to? :basic_auth_params
@@ -165,37 +158,35 @@
# @return [Enumerable] List of values matching JSON path
def json_path_values_for(response, path, attribute: nil)
raise 'JSON does not support attributes' if attribute
- if path[0] != '$'
- path = convert_to_pascal_case(path) if pascal_keys?
- path = '$..' + path
- end
JsonPath.on(response.body, path)
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 [RestClient::Response] response Response from API
# @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, attribute: nil)
path = path.to_s
case Interpreter.response_type_for(response)
when :xml
result = xpath_elements_for(response: response, xpath: path, attribute: attribute).first
- raise NoElementAtPath, "No value at Xpath '#{prefix_xpath(path, attribute)}'" unless result
+ raise NoElementAtPath, "No value at Xpath '#{prefix_xpath(path, attribute)}' in '#{response.body}'" unless result
return result.inner_text if attribute.nil?
return result.attributes[attribute].inner_text
when :json
+ path = add_pascal_path(path)
paths_to_check = path.split(',')
+ paths_to_check = paths_to_check.map { |path_to_check| prefix_json_path(path_to_check) }
matching_values = paths_to_check.collect do |path_to_check|
json_path_values_for(response, path_to_check, attribute: attribute)
end.reject(&:empty?)
- raise NoElementAtPath, "Path '#{path}' not found in '#{response.body}'" if matching_values.empty?
+ raise NoElementAtPath, "No value at JSONPath '#{paths_to_check}' in '#{response.body}'" if matching_values.empty?
matching_values.first.first
when :hash
response.dig(path.split('.')) # Use path as Hash dig expression separating params via '.' TODO: Unit test
else