lib/soaspec/basic_soap_handler.rb in soaspec-0.0.5 vs lib/soaspec/basic_soap_handler.rb in soaspec-0.0.6

- old
+ new

@@ -1,85 +1,105 @@ -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 - 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 +require 'yaml' +require_relative 'common' +require_relative 'tester' +require_relative 'hash_methods' + +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 + + # Options to log xml request and response + def logging_options + { + log: true, # See request and response. (Put this in traffic file) + log_level: :debug, + logger: file_logger, + pretty_print_xml: true # Prints XML pretty + } + end + + # Default Savon options + def default_options + { + ssl_verify_mode: :none, + follow_redirects: true, # Necessary for many API calls + soap_version: 2, # use SOAP 1.2. You will get 415 error if this set to default + raise_errors: false + } + end + + # Add values to here when extending this class + def class_options + { + } + end + + # 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 = default_options.merge logging_options + options.merge! class_options + 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 + + # Used in together with Exchange request that passes such override parameters + 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 + if @request_option == :template + test_values = test_values.transform_keys_to_symbols # Either string or symbol + 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.merge(test_values)) + 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 \ No newline at end of file