lib/soaspec/exchange.rb in soaspec-0.0.59 vs lib/soaspec/exchange.rb in soaspec-0.0.60
- old
+ new
@@ -7,10 +7,12 @@
attr_reader :api_class
# How many times to retry for a success
attr_accessor :retry_count
# Params used when making a request
attr_accessor :default_params
+ # Name used for displaying class
+ attr_accessor :test_name
# 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
@@ -22,13 +24,13 @@
@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 # This uses the global parameter. The handler should be set straight before an exchange is made
+ def initialize(name = self.class.to_s, override_parameters = {})
+ self.test_name ||= name.to_s
+ @api_class ||= Soaspec.api_handler # This uses the global parameter. The handler should be set straight before an exchange is made
@override_parameters = override_parameters
@retry_for_success = false
self.retry_count = 3
@api_class.elements.each do |element|
define_singleton_method(element.to_s.split('__custom_path_').last) do
@@ -49,11 +51,11 @@
# 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
+ Soaspec::SpecLogger.add_to 'Example ' + test_name
request_params = default_params ? merge_request_body : @override_parameters
retry_count.times do
response = @api_class.make_request(request_params)
return response unless retry_for_success?
return response if (200..299).cover? @api_class.status_code_for(response)
@@ -79,11 +81,11 @@
# 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
+ 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)
@@ -122,8 +124,39 @@
# Can be used to build a request over several steps (e.g Cucumber)
# Will be used with FactoryBot
def []=(key, value)
self.default_params = { body: {} } unless default_params # Initialize as Hash if not set
default_params[:body][key] = value
+ end
+
+ # Implement undefined setter with []= for FactoryBot to use without needing to define params to set
+ # @param [Object] method_name
+ # @param [Object] args
+ # @param [Object] block
+ def method_missing(method_name, *args, &block)
+ if method_name[-1] == '='
+ if args.first.class < Exchange # This would be prerequisite exchange
+ define_singleton_method(method_name[0..-2]) do
+ args.first
+ end
+ else
+ self[method_name[0..-2]] = args.first
+ end
+ else
+ super
+ end
+ end
+
+ # Used for setters that are not defined
+ def respond_to_missing?(method_name, *args)
+ method_name[-1] == '=' || super
+ end
+
+ # Makes request, caching the response and returning self
+ # Used by FactoryBot
+ # @return [Self]
+ def save!
+ call
+ self
end
end
\ No newline at end of file