lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.65 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.66

- old
+ new

@@ -82,10 +82,17 @@ define_method('rest_client_headers') do headers end end + # Convert each key from snake_case to PascalCase + def pascal_keys(set) + define_method('pascal_keys?') do + set + end + end + end # Wraps around Savon client defining default values dependent on the soap request class RestHandler < ExchangeHandler extend Soaspec::RestAccessors @@ -136,10 +143,30 @@ merged_options[:headers].merge! parse_headers merged_options.merge!(options) @resource = RestClient::Resource.new(base_url_value, merged_options) # @resource[url_extension].get end + def convert_to_pascal_case(key) + key.split('_').map(&:capitalize).join + end + + # @return Whether to convert each key in the request to PascalCase + # It will also auto convert simple XPath, JSONPath where '//' or '..' not specified + def pascal_keys? + false + end + + # @return [Hash] + def hash_used_in_request(override_hash) + request = @default_hash.merge(override_hash) + if pascal_keys? + request.map { |k, v| [convert_to_pascal_case(k.to_s), v] }.to_h + else + request + end + end + # Used in together with Exchange request that passes such override parameters # @param [Hash] override_parameters Params to characterize REST request # @param_value [params] Extra parameters (E.g. headers) # @param_value [suburl] URL appended to base_url of class # @param_value [method] REST method (get, post, etc) @@ -154,11 +181,11 @@ begin response = case test_values[:method] when :post, :patch, :put unless test_values[:payload] - test_values[:payload] = JSON.generate(@default_hash.merge(test_values[:body])).to_s if test_values[:body] + test_values[:payload] = JSON.generate(hash_used_in_request(test_values[:body])).to_s if test_values[:body] end @resource_used.send(test_values[:method].to_s, test_values[:payload], test_values[:params]) else @resource_used.send(test_values[:method].to_s, test_values[:params]) end @@ -168,13 +195,13 @@ Soaspec::SpecLogger.add_to('response_headers: ' + response.headers.to_s) Soaspec::SpecLogger.add_to('response_body: ' + response.to_s) response end - # @param [Hash] format Format of expected result. Ignored for this + # @param [Hash] _format Format of expected result. Ignored for this # @return [Object] Generic body to be displayed in error messages - def response_body(response, format: :hash) + def response_body(response, _format: :hash) extract_hash response end def include_in_body?(response, expected) response.body.include? expected @@ -268,14 +295,20 @@ path = path.to_s case Interpreter.response_type_for(response) when :xml path = "//*[@#{attribute}]" unless attribute.nil? - path = '//' + path if path[0] != '/' + if path[0] != '/' + path = convert_to_pascal_case(path) if pascal_keys? + path = '//' + path + end xpath_value_for(response: response, xpath: path, attribute: attribute) when :json raise 'JSON does not support attributes' if attribute - path = '$..' + path if path[0] != '$' + if path[0] != '$' + path = convert_to_pascal_case(path) if pascal_keys? + path = '$..' + path + end 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 \ No newline at end of file