Class: Soaspec::SoapHandler
- Inherits:
-
ExchangeHandler
- Object
- ExchangeHandler
- Soaspec::SoapHandler
- Extended by:
- Forwardable, SoapAccessors
- Defined in:
- lib/soaspec/exchange_handlers/soap_handler.rb
Overview
Wraps around Savon client defining default values dependent on the soap request
Instance Attribute Summary collapse
-
#client ⇒ Object
Savon client used to make SOAP calls.
-
#operation ⇒ Object
SOAP Operation to use by default.
Attributes inherited from ExchangeHandler
Class Method Summary collapse
-
.method_missing(method_name, *args, &block) ⇒ Object
Implement undefined setter with []= for FactoryBot to use without needing to define params to set.
- .respond_to_missing?(method_name, *args) ⇒ Boolean
Instance Method Summary collapse
-
#convert_to_lower_case(xml_doc) ⇒ Object
Convert all XML nodes to lowercase.
-
#default_options ⇒ Hash
Default Savon options.
-
#found?(response) ⇒ Boolean
Whether the request found the desired value or not.
-
#include_in_body?(response, expected) ⇒ Boolean
Whether response includes provided string within it.
-
#include_key?(response, expected) ⇒ Boolean
Whether response body contains expected key.
-
#include_value?(response, expected_value) ⇒ Boolean
Whether any of the keys of the Body Hash include value.
-
#initialize(name = self.class.to_s, options = {}) ⇒ SoapHandler
constructor
Setup object to handle communicating with a particular SOAP WSDL.
-
#logging_options ⇒ Object
Options to log xml request and response.
-
#make_request(request_parameters) ⇒ Object
Used in together with Exchange request that passes such override parameters.
-
#request_body_params(request_parameters) ⇒ Object
Used in making request via hash or in template via Erb.
-
#request_parameters(override_parameters) ⇒ Hash
Parameters used in making a request.
-
#request_root_attributes ⇒ Object
Attributes set at the root XML element of SOAP request.
-
#response_body(response, format: :hash) ⇒ Object
Generic body to be displayed in error messages.
-
#savon_options ⇒ Hash
Add values to here when extending this class to have default Savon options.
-
#status_code_for(response) ⇒ Integer
Response status code for response.
-
#to_hash(response) ⇒ Object
Hash of response body.
-
#value_from_path(response, path, attribute: nil) ⇒ String
Based on a exchange, return the value at the provided xpath If the path does not begin with a ‘/’, a ‘//’ is added to it.
-
#values_from_path(response, path, attribute: nil) ⇒ Enumerable
List of values returned from path.
-
#xpath_elements_for(response: nil, xpath: nil, attribute: nil) ⇒ Enumerable
Returns the value at the provided xpath.
Methods included from SoapAccessors
Methods inherited from ExchangeHandler
#default_hash=, #elements, #set_remove_key, #set_remove_keys, #store, #to_s, use, #use
Methods included from HandlerAccessors
#attribute, #convert_to_lower, #default_hash, #element, #mandatory_elements, #mandatory_json_values, #mandatory_xpath_values, #strip_namespaces, #template_name
Methods included from ExchangeHandlerDefaults
#convert_to_lower?, #expected_mandatory_elements, #expected_mandatory_json_values, #expected_mandatory_xpath_values, #request, #retry_exception_limit, #retry_on_exceptions, #retry_pause_time, #strip_namespaces?
Constructor Details
#initialize(name = self.class.to_s, options = {}) ⇒ SoapHandler
Setup object to handle communicating with a particular SOAP WSDL
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 73 def initialize(name = self.class.to_s, = {}) if name.is_a?(Hash) && == {} # If name is not set = name name = self.class.to_s end super set_remove_keys(, %i[operation default_hash template_name]) = Soaspec::SpecLogger.log_api_traffic? ? .merge() : .merge! .merge!() self.client = Savon.client() end |
Instance Attribute Details
#client ⇒ Object
Savon client used to make SOAP calls
29 30 31 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 29 def client @client end |
#operation ⇒ Object
SOAP Operation to use by default
31 32 33 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 31 def operation @operation end |
Class Method Details
.method_missing(method_name, *args, &block) ⇒ Object
Implement undefined setter with []= for FactoryBot to use without needing to define params to set
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 220 def method_missing(method_name, *args, &block) tmp_class = new(method_name) operations = tmp_class.operations if operations.include? method_name tmp_class.operation = method_name exchange = Exchange.new(method_name, *args) exchange.exchange_handler = tmp_class yield exchange if block_given? exchange else super end end |
.respond_to_missing?(method_name, *args) ⇒ Boolean
234 235 236 237 238 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 234 def respond_to_missing?(method_name, *args) tmp_class = new(args) operations = tmp_class.operations operations.include?(method_name) || super end |
Instance Method Details
#convert_to_lower_case(xml_doc) ⇒ Object
Convert all XML nodes to lowercase
158 159 160 161 162 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 158 def convert_to_lower_case(xml_doc) xml_doc.traverse do |node| node.name = node.name.downcase if node.is_a?(Nokogiri::XML::Element) end end |
#default_options ⇒ Hash
Default Savon options. See savonrb.com/version2/globals.html for details
54 55 56 57 58 59 60 61 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 54 def { ssl_verify_mode: :none, # Easier for testing. Not so secure follow_redirects: true, # Necessary for many API calls soap_version: 2, # use SOAP 1.2. You will get 415 error if this is incorrect raise_errors: false # HTTP errors not cause failure as often negative test scenarios expect not 200 response } end |
#found?(response) ⇒ Boolean
Returns Whether the request found the desired value or not.
132 133 134 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 132 def found?(response) status_code_for(response) != 404 end |
#include_in_body?(response, expected) ⇒ Boolean
Returns Whether response includes provided string within it.
144 145 146 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 144 def include_in_body?(response, expected) response.to_xml.to_s.include? expected end |
#include_key?(response, expected) ⇒ Boolean
Returns Whether response body contains expected key.
150 151 152 153 154 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 150 def include_key?(response, expected) body = response.body body.extend Hashie::Extensions::DeepFind !body.deep_find_all(expected).empty? end |
#include_value?(response, expected_value) ⇒ Boolean
Returns Whether any of the keys of the Body Hash include value.
205 206 207 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 205 def include_value?(response, expected_value) response.body.include_value?(expected_value) end |
#logging_options ⇒ Object
Options to log xml request and response
37 38 39 40 41 42 43 44 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 37 def { log: true, # See request and response. (Put this in traffic file) log_level: :debug, logger: Soaspec::SpecLogger.create, pretty_print_xml: true # Prints XML pretty } end |
#make_request(request_parameters) ⇒ Object
Used in together with Exchange request that passes such override parameters
108 109 110 111 112 113 114 115 116 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 108 def make_request(request_parameters) # Call the SOAP operation with the request XML provided request = request_parameters(request_parameters) begin client.call request.operation, request.body # request_body_params(request_parameters) rescue Savon::HTTPError => e soap_error end end |
#request_body_params(request_parameters) ⇒ Object
Used in making request via hash or in template via Erb
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 95 def request_body_params(request_parameters) test_values = request_parameters[:body] || request_parameters test_values.transform_keys_to_symbols if Soaspec.always_use_keys? if @request_option == :template test_values = IndifferentHash.new(test_values) # Allow test_values to be either Symbol or String { xml: Soaspec::TemplateReader.new.render_body(template_name, binding) } elsif @request_option == :hash { message: @default_hash.merge(test_values), attributes: request_root_attributes } end end |
#request_parameters(override_parameters) ⇒ Hash
Returns Parameters used in making a request.
88 89 90 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 88 def request_parameters(override_parameters) SoapRequest.new(operation, request_body_params(override_parameters), @request_option) end |
#request_root_attributes ⇒ Object
Attributes set at the root XML element of SOAP request
34 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 34 def request_root_attributes; end |
#response_body(response, format: :hash) ⇒ Object
Returns Generic body to be displayed in error messages.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 120 def response_body(response, format: :hash) case format when :hash response.body when :raw response.xml else response.body end end |
#savon_options ⇒ Hash
Add values to here when extending this class to have default Savon options. See savonrb.com/version2/globals.html for details
66 67 68 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 66 def {} end |
#status_code_for(response) ⇒ Integer
Response status code for response. ‘200’ indicates a success
139 140 141 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 139 def status_code_for(response) response.http.code end |
#to_hash(response) ⇒ Object
Hash of response body
210 211 212 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 210 def to_hash(response) response.body end |
#value_from_path(response, path, attribute: nil) ⇒ String
Based on a exchange, return the value at the provided xpath If the path does not begin with a ‘/’, a ‘//’ is added to it
189 190 191 192 193 194 195 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 189 def value_from_path(response, path, attribute: nil) results = xpath_elements_for(response: response, xpath: path, attribute: attribute) raise NoElementAtPath, "No value at Xpath '#{path}' in XML #{response.doc}" if results.empty? return results.first.inner_text if attribute.nil? results.first.attributes[attribute].inner_text end |
#values_from_path(response, path, attribute: nil) ⇒ Enumerable
Returns List of values returned from path.
198 199 200 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 198 def values_from_path(response, path, attribute: nil) xpath_elements_for(response: response, xpath: path, attribute: attribute).map(&:inner_text) end |
#xpath_elements_for(response: nil, xpath: nil, attribute: nil) ⇒ Enumerable
Returns the value at the provided xpath
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 168 def xpath_elements_for(response: nil, xpath: nil, attribute: nil) raise ArgumentError('response and xpath must be passed to method') unless response && xpath xpath = "//*[@#{attribute}]" unless attribute.nil? xpath = '//' + xpath if xpath[0] != '/' temp_doc = response.doc.dup convert_to_lower_case(temp_doc) if convert_to_lower? if strip_namespaces? && !xpath.include?(':') temp_doc.remove_namespaces! temp_doc.xpath(xpath) else temp_doc.xpath(xpath, temp_doc.collect_namespaces) end end |