lib/soaspec/exchange_handlers/exchange_handler.rb in soaspec-0.2.24 vs lib/soaspec/exchange_handlers/exchange_handler.rb in soaspec-0.2.25
- old
+ new
@@ -1,126 +1,130 @@
-require_relative 'handler_accessors'
-
-module Soaspec
- # Inherit this for a class describing how to implement a particular exchange.
- # Has basic methods common for methods defining RSpec tests in YAML
- class ExchangeHandler
- extend Soaspec::HandlerAccessors
-
- def self.use
- new.use
- end
-
- # Retrieve the name of the template file to be used in the API request
- attr_reader :template_name
-
- # Explicitly defined elements for which a path has been predefined
- def elements
- public_methods.select { |i| i[/__custom_path_.+/] }
- end
-
- # Set instance variable name
- # @param [String, Symbol] name Name used when describing API test
- # @param [Hash] _options Parameters defining handler. Used in descendants
- def initialize(name = self.class.to_s, _options = {})
- use
- @request_option = :hash
- raise ArgumentError, 'Cannot define both template_name and default_hash' if respond_to?(:template_name_value) && respond_to?(:default_hash_value)
-
- @template_name = respond_to?(:template_name_value) ? template_name_value : ''
- @default_hash = respond_to?(:default_hash_value) ? default_hash_value : {}
- @name = name
- end
-
- # Set Api handler used by Exchange class to this handler
- # @return [Self]
- def use
- Soaspec.api_handler = self
- self
- end
-
- # Sets api handler variable globally. This is used in 'Exchange' class
- # @return [String] Name set upon initialisation
- def to_s
- use
- @name.to_s
- end
-
- # Set the default hash representing data to be used in making a request
- # This will set the @request_option instance variable too
- def default_hash=(hash)
- @request_option = :hash
- @default_hash = Soaspec.always_use_keys? ? hash.transform_keys_to_symbols : hash
- end
-
- # Set the request option type and the template name
- # Erb is used to parse the template file, executing Ruby code in `<%= %>` blocks to work out the final request
- # @param [String] name Name of file inside 'template' folder excluding extension
- def template_name=(name)
- @request_option = :template
- @template_name = name
- end
-
- # Will be used in 'success_scenarios' shared examples.
- # Set though 'mandatory_elements' method
- # @return [Array] Array of symbols specifying element names
- def expected_mandatory_elements
- []
- end
-
- # Change this through 'mandatory_xpath_values' method to specify xpath results that must be present in the response
- # Will be used in 'success_scenarios' shared examples
- # @return [Hash] Hash of 'xpath' => 'expected value' pairs
- def expected_mandatory_xpath_values
- {}
- end
-
- # Change this through 'mandatory_json_values' method to specify json results that must be present in the response
- # Will be used in 'success_scenarios' shared examples
- # @return [Hash] Hash of 'json/path' => 'expected value' pairs
- def expected_mandatory_json_values
- {}
- end
-
- # @return [Boolean] Whether all xpaths will be done with XML that is converted to lower case
- def convert_to_lower?
- false
- end
-
- # Stores a value in a method that can be accessed by the provided name
- # @param [Symbol] name Name of method to use to access this value within handler
- # @param [String] value Value to store
- def store(name, value)
- define_singleton_method('__stored_val__' + name.to_s) { value }
- end
-
- # Set instance variable for each key in hash remove it from Hash
- # @param [Hash] hash with items to remove from
- # @param [Array] keys List of keys to remove from hash, setting each one
- def set_remove_keys(hash, keys)
- keys.each { |key| set_remove_key(hash, key) }
- end
-
- # Set instance variable and remove it from Hash
- # @param [Hash] hash Hash to remove/retrieve keys from
- # @param [String,Symbol] key Key to remove and to set instance variable for
- def set_remove_key(hash, key)
- return unless hash.key? key
-
- __send__("#{key}=", hash[key])
- hash.delete key
- end
-
- # @return [Boolean] Whether to remove namespaces in xpath assertion automatically
- def strip_namespaces?
- false
- end
-
- # Request of API call. Either intended request or actual request
- # @param [Object] response Response from calling exchange
- def request(response)
- return "Request not yet sent Request option is #{@request_option}" unless response
-
- 'Specific API handler should implement this'
- end
- end
-end
+require_relative 'handler_accessors'
+
+module Soaspec
+ # Inherit this for a class describing how to implement a particular exchange.
+ # Has basic methods common for methods defining RSpec tests in YAML
+ class ExchangeHandler
+ extend Soaspec::HandlerAccessors
+
+ # Use an instance of this ExchangeHandler in any further Exchange's
+ # This is a convenience method as it creates an ExchangeHandler behind the scenes
+ # @return [ExchangeHandler] Exchange handler instance created
+ def self.use
+ new.use
+ end
+
+ # @return [String] Name of the template file to be used in the API request
+ attr_reader :template_name
+
+ # Explicitly defined elements for which a path has been predefined
+ def elements
+ public_methods.select { |i| i[/__custom_path_.+/] }
+ end
+
+ # Set instance variable name
+ # @param [String, Symbol] name Name used when describing API test
+ # @param [Hash] _options Parameters defining handler. Used in descendants
+ def initialize(name = self.class.to_s, _options = {})
+ use
+ @request_option = :hash
+ raise ArgumentError, 'Cannot define both template_name and default_hash' if respond_to?(:template_name_value) && respond_to?(:default_hash_value)
+
+ @template_name = respond_to?(:template_name_value) ? template_name_value : ''
+ @default_hash = respond_to?(:default_hash_value) ? default_hash_value : {}
+ @name = name
+ end
+
+ # Set Api handler used by Exchange class to this handler
+ # @return [Self]
+ def use
+ Soaspec.api_handler = self
+ self
+ end
+
+ # Sets api handler variable globally. This is used in 'Exchange' class
+ # @return [String] Name set upon initialisation
+ def to_s
+ use
+ @name.to_s
+ end
+
+ # Set the default hash representing data to be used in making a request
+ # This will set the @request_option instance variable too
+ # @param [Hash] hash Hash to send in request body by default
+ def default_hash=(hash)
+ @request_option = :hash
+ @default_hash = Soaspec.always_use_keys? ? hash.transform_keys_to_symbols : hash
+ end
+
+ # Set the request option type and the template name
+ # Erb is used to parse the template file, executing Ruby code in `<%= %>` blocks to work out the final request
+ # @param [String] name Name of file inside 'template' folder excluding extension
+ def template_name=(name)
+ @request_option = :template
+ @template_name = name
+ end
+
+ # Will be used in 'success_scenarios' shared examples.
+ # Set though 'mandatory_elements' method
+ # @return [Array] Array of symbols specifying element names
+ def expected_mandatory_elements
+ []
+ end
+
+ # Change this through 'mandatory_xpath_values' method to specify xpath results that must be present in the response
+ # Will be used in 'success_scenarios' shared examples
+ # @return [Hash] Hash of 'xpath' => 'expected value' pairs
+ def expected_mandatory_xpath_values
+ {}
+ end
+
+ # Change this through 'mandatory_json_values' method to specify json results that must be present in the response
+ # Will be used in 'success_scenarios' shared examples
+ # @return [Hash] Hash of 'json/path' => 'expected value' pairs
+ def expected_mandatory_json_values
+ {}
+ end
+
+ # @return [Boolean] Whether all xpaths will be done with XML that is converted to lower case
+ def convert_to_lower?
+ false
+ end
+
+ # Stores a value in a method that can be accessed by the provided name
+ # @param [Symbol] name Name of method to use to access this value within handler
+ # @param [String] value Value to store
+ def store(name, value)
+ define_singleton_method('__stored_val__' + name.to_s) { value }
+ end
+
+ # Set instance variable for each key in hash remove it from Hash
+ # @param [Hash] hash with items to remove from
+ # @param [Array] keys List of keys to remove from hash, setting each one
+ def set_remove_keys(hash, keys)
+ keys.each { |key| set_remove_key(hash, key) }
+ end
+
+ # Set instance variable and remove it from Hash
+ # @param [Hash] hash Hash to remove/retrieve keys from
+ # @param [String,Symbol] key Key to remove and to set instance variable for
+ def set_remove_key(hash, key)
+ return unless hash.key? key
+
+ __send__("#{key}=", hash[key])
+ hash.delete key
+ end
+
+ # @return [Boolean] Whether to remove namespaces in xpath assertion automatically
+ def strip_namespaces?
+ false
+ end
+
+ # Request of API call. Either intended request or actual request
+ # @param [Object] response Response from calling exchange
+ def request(response)
+ return "Request not yet sent Request option is #{@request_option}" unless response
+
+ 'Specific API handler should implement this'
+ end
+ end
+end