lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.1.9 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.1.10

- old
+ new

@@ -270,23 +270,30 @@ else raise "Unable to interpret type of #{response.body}" end end + # Request of API call. Either intended request or actual request + def request(response) + return 'Request not yet sent' if response.nil? + response.request + end + private # Work out data to send based upon payload, template_name # @return [String] Payload to send in REST request def post_data(test_values) - if test_values[:body] - test_values[:payload] = JSON.generate(hash_used_in_request(test_values[:body])).to_s - elsif @request_option == :template - request_body = File.read(File.join(Soaspec.template_folder, template_name)) - ERB.new(request_body).result(binding) - else - test_values[:payload] - end + data = if test_values[:body] + test_values[:payload] = JSON.generate(hash_used_in_request(test_values[:body])).to_s + elsif @request_option == :template + Soaspec::TemplateReader.new.render_body(template_name, binding) + else + test_values[:payload] + end + # Soaspec::SpecLogger.info "Request Empty for '#{@request_option}'" if data.strip.empty? + data end # @return [Hash] Hash used in REST request based on data conversion def hash_used_in_request(override_hash) request = @default_hash.merge(override_hash) @@ -305,12 +312,16 @@ methods.each do |rest_method| # Make REST Exchange within this Handler context # @param [Hash] params Exchange parameters # @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body define_method(rest_method) do |params = {}| - # params ||= {} params[:name] ||= rest_method - new(params[:name]) + exchange_params = { name: params[:name] } + if params[:template_name] + exchange_params[:template_name] = params[:template_name] + params.delete :template_name + end + new(exchange_params) Exchange.new(params[:name], method: rest_method.to_sym, **params) end end end \ No newline at end of file