require 'yaml' require_relative 'common' require_relative 'tester' module Soaspec # Wraps around Savon client defining default values dependent on the soap request class BasicSoapHandler < Tester # Savon client used to make SOAP calls attr_accessor :client # Namespaces used in XML body attr_accessor :namespaces # SOAP Operation to use by default attr_accessor :default_operation # Setup object to handle communicating with a particular SOAP WSDL # @param [Hash] specific_options Options defining SOAP request. WSDL, authentication, see http://savonrb.com/version2/globals.html for list of options def initialize(name, specific_options) super options = { ssl_verify_mode: :none, follow_redirects: true, # Necessary log: true, # See request and response. (Put this in traffic file) log_level: :debug, logger: file_logger, soap_version: 2, # use SOAP 1.2. You will get 415 error if this set to default pretty_print_xml: true, # Prints XML pretty raise_errors: false } options.merge!(specific_options) @client = Savon.client(options) self.namespaces = {} @name = name end # Sends a call to the API (is being made obsolete) # @param [Hash] options Dictionary of key value pairs specifying what API call to make # @return [SavonResponse] Savon response from which areas (header, body, etc) of the SOAP response can be accessed def call(options) test_values = options[:overide_values]['request'] || {} # Empty hash if no specific request values are set options[:operation] ||= self.default_operation # Erb parses template file, executing Ruby code in `<% %>` blocks to work out final request render_body = ERB.new(options[:template]).result(binding) @client.call(options[:operation], xml: render_body ) # Call the SOAP operation with the request XML provided end def name(name) @test_values = {} @test_name = name self end def override(request_parameters) @test_values = request_parameters self end # TODO: Use this together with Exchange request def make_request(override_parameters) test_values = override_parameters # Used in Erb # Erb parses template file, executing Ruby code in `<% %>` blocks to work out final request puts 'O' + override_parameters.to_s if @request_option == :template request_body = File.read('template/' + template_name + '.xml') render_body = ERB.new(request_body).result(binding) @client.call(default_operation, xml: render_body ) # Call the SOAP operation with the request XML provided elsif @request_option == :hash @client.call(default_operation, message: @default_hash) end end def to_s Soaspec::Environment.api_handler = self @name end def include?(value) @xml_response.include? value end def default_hash=(hash) @request_option = :hash @default_hash = hash end end end