require_relative '../soaspec' # This represents a request / response pair class Exchange attr_reader :api_class # How many times to retry for a success attr_accessor :retry_count # Set retry for success variable to true so that request will be retried # for retry_count until it's true def retry_for_success @retry_for_success = true self end # @return [Bool] Whether to keep making request until success code reached def retry_for_success? @retry_for_success end # @param [Symbol, String] name Name shown in RSpec run # @param [Hash] override_parameters Parameters to override for default params def initialize(name, override_parameters = {}) @test_name = name.to_s @api_class = Soaspec.api_handler @override_parameters = override_parameters @retry_for_success = false self.retry_count = 3 end # Make request to handler with parameters defined # Will retry until success code reached if retry_for_success? is set # @return [Response] Response from Api handler def make_request Soaspec::SpecLogger.add_to 'Example ' + @test_name retry_count.times do response = @api_class.make_request(@override_parameters) return response unless retry_for_success? return response if @api_class.status_code_for(response) == 200 response end end # Name describing this class when used with `RSpec.describe` # This will make the request and store the response # @return [String] Name given when initializing def to_s @test_name end # Returns response object from Api. Will make the request if not made and then cache it for later on # For example for SOAP it will be a Savon response # response.body (body of response as Hash) # response.header (head of response as Hash) def response @response ||= make_request end # Get status code from api class. This is http response for Web Api # @return [Integer] Status code from api class def status_code @api_class.status_code_for(response) end # Dummy request used to make a request without verifying it and ignoring WSDL errors # @return [Boolean] Always returns true. Unless of course an unexpected exception occurs def dummy_request make_request true rescue Savon::HTTPError puts 'Resolver error' # This seems to occur first time IP address asks for WSDL true end # Extract value from path api class # @param [Object] path Path to return element for api class E.g - for SOAP this is XPath string. For JSON, this is Hash dig Array # @return [String] Value at path def [](path) @api_class.value_from_path(self, path) end end