Class: Exchange
- Inherits:
-
Object
- Object
- Exchange
- Defined in:
- lib/soaspec/exchange.rb
Overview
This represents a request / response pair
Instance Attribute Summary collapse
-
#api_class ⇒ Object
Class of Api Handler for which this exchange is made.
-
#retry_count ⇒ Object
How many times to retry for a success.
-
#test_name ⇒ Object
Name used for displaying class.
Instance Method Summary collapse
-
#[](path) ⇒ String
Extract value from path api class.
-
#[]=(key, value) ⇒ Object
Set a parameter request in the request body.
-
#dummy_request ⇒ Boolean
Dummy request used to make a request without verifying it and ignoring WSDL errors.
-
#element?(path) ⇒ Boolean
Whether an element exists at the path.
-
#initialize(name = self.class.to_s, override_parameters = {}) ⇒ Exchange
constructor
A new instance of Exchange.
-
#make_request ⇒ Response
Make request to handler with parameters defined Will retry until success code reached if retry_for_success? is set.
-
#method=(method) ⇒ Object
Specify HTTP method to use.
-
#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
Used for setters that are not defined.
-
#response ⇒ Object
(also: #call)
Returns response object from Api.
-
#retrieve(name) ⇒ Object
Retrieve the stored value from the Api Handler.
-
#retry_for_success ⇒ Object
Set retry for success variable to true so that request will be retried for retry_count until it’s true.
-
#retry_for_success? ⇒ Bool
Whether to keep making request until success code reached.
-
#save! ⇒ Self
Makes request, caching the response and returning self Used by FactoryBot.
-
#status_code ⇒ Integer
Get status code from api class.
-
#store(name, value) ⇒ Object
Stores a value in the api handler that can be accessed by the provided name.
-
#suburl=(url) ⇒ Object
Specify a url to add onto the base_url of the ExchangeHandler used.
-
#to_s ⇒ String
Name describing this class when used with ‘RSpec.describe` This will make the request and store the response.
Constructor Details
#initialize(name = self.class.to_s, override_parameters = {}) ⇒ Exchange
Returns a new instance of Exchange.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/soaspec/exchange.rb', line 27 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| element_name = element.to_s.split('__custom_path_').last define_singleton_method(element_name) do @api_class.__send__(element, response) # Forward the call onto handler to retrieve the element for the response end define_singleton_method("#{element_name}?") do begin __send__ element_name true rescue NoElementAtPath false end end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Implement undefined setter with []= for FactoryBot to use without needing to define params to set
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/soaspec/exchange.rb', line 152 def method_missing(method_name, *args, &block) if method_name[-1] == '=' # A setter method 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 |
Instance Attribute Details
#api_class ⇒ Object
Class of Api Handler for which this exchange is made
7 8 9 |
# File 'lib/soaspec/exchange.rb', line 7 def api_class @api_class end |
#retry_count ⇒ Object
How many times to retry for a success
9 10 11 |
# File 'lib/soaspec/exchange.rb', line 9 def retry_count @retry_count end |
#test_name ⇒ Object
Name used for displaying class
11 12 13 |
# File 'lib/soaspec/exchange.rb', line 11 def test_name @test_name end |
Instance Method Details
#[](path) ⇒ String
Extract value from path api class
136 137 138 |
# File 'lib/soaspec/exchange.rb', line 136 def [](path) @api_class.value_from_path(response, path.to_s) end |
#[]=(key, value) ⇒ Object
Set a parameter request in the request body. Can be used to build a request over several steps (e.g Cucumber) Will be used with FactoryBot
143 144 145 146 |
# File 'lib/soaspec/exchange.rb', line 143 def []=(key, value) @override_parameters[:body] ||= {} @override_parameters[:body][key] = value end |
#dummy_request ⇒ Boolean
Dummy request used to make a request without verifying it and ignoring WSDL errors
116 117 118 119 120 121 122 123 |
# File 'lib/soaspec/exchange.rb', line 116 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 |
#element?(path) ⇒ Boolean
Returns Whether an element exists at the path.
126 127 128 129 130 131 |
# File 'lib/soaspec/exchange.rb', line 126 def element?(path) [path] true rescue NoElementAtPath false end |
#make_request ⇒ Response
Make request to handler with parameters defined Will retry until success code reached if retry_for_success? is set
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/soaspec/exchange.rb', line 64 def make_request Soaspec::SpecLogger.add_to 'Example ' + test_name request_params = @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) response end end |
#method=(method) ⇒ Object
Specify HTTP method to use. Default is :post
57 58 59 |
# File 'lib/soaspec/exchange.rb', line 57 def method=(method) @override_parameters[:method] = method end |
#respond_to_missing?(method_name, *args) ⇒ Boolean
Used for setters that are not defined
167 168 169 |
# File 'lib/soaspec/exchange.rb', line 167 def respond_to_missing?(method_name, *args) method_name[-1] == '=' || super end |
#response ⇒ Object Also known as: call
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)
102 103 104 |
# File 'lib/soaspec/exchange.rb', line 102 def response @response ||= make_request end |
#retrieve(name) ⇒ Object
Retrieve the stored value from the Api Handler
85 86 87 88 89 |
# File 'lib/soaspec/exchange.rb', line 85 def retrieve(name) method = '__stored_val__' + name.to_s raise ArgumentError('Value not stored at ') unless api_class.respond_to? method @api_class.send(method) end |
#retry_for_success ⇒ Object
Set retry for success variable to true so that request will be retried for retry_count until it’s true
15 16 17 18 |
# File 'lib/soaspec/exchange.rb', line 15 def retry_for_success @retry_for_success = true self end |
#retry_for_success? ⇒ Bool
Returns Whether to keep making request until success code reached.
21 22 23 |
# File 'lib/soaspec/exchange.rb', line 21 def retry_for_success? @retry_for_success end |
#save! ⇒ Self
Makes request, caching the response and returning self Used by FactoryBot
174 175 176 177 |
# File 'lib/soaspec/exchange.rb', line 174 def save! call self end |
#status_code ⇒ Integer
Get status code from api class. This is http response for Web Api
110 111 112 |
# File 'lib/soaspec/exchange.rb', line 110 def status_code @api_class.status_code_for(response) end |
#store(name, value) ⇒ Object
Stores a value in the api handler that can be accessed by the provided name
78 79 80 |
# File 'lib/soaspec/exchange.rb', line 78 def store(name, value) @api_class.store(name, self[value]) end |
#suburl=(url) ⇒ Object
Specify a url to add onto the base_url of the ExchangeHandler used
51 52 53 |
# File 'lib/soaspec/exchange.rb', line 51 def suburl=(url) @override_parameters[:suburl] = url end |
#to_s ⇒ String
Name describing this class when used with ‘RSpec.describe` This will make the request and store the response
94 95 96 |
# File 'lib/soaspec/exchange.rb', line 94 def to_s test_name end |