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
readonly
Class of Api Handler for which this exchange is made.
-
#retry_count ⇒ Object
How many times to retry for a success.
Instance Method Summary collapse
-
#[](path) ⇒ String
Extract value from path api class.
-
#dummy_request ⇒ Boolean
Dummy request used to make a request without verifying it and ignoring WSDL errors.
-
#initialize(name, 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.
-
#response ⇒ Object
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.
-
#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.
-
#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, override_parameters = {}) ⇒ Exchange
Returns a new instance of Exchange.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/soaspec/exchange.rb', line 25 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 @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 @api_class.__send__(element, response) # Forward the call onto handler to retrieve the element for the response end end end |
Instance Attribute Details
#api_class ⇒ Object (readonly)
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 |
Instance Method Details
#[](path) ⇒ String
Extract value from path api class
102 103 104 |
# File 'lib/soaspec/exchange.rb', line 102 def [](path) @api_class.value_from_path(response, path.to_s) end |
#dummy_request ⇒ Boolean
Dummy request used to make a request without verifying it and ignoring WSDL errors
90 91 92 93 94 95 96 97 |
# File 'lib/soaspec/exchange.rb', line 90 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 |
#make_request ⇒ Response
Make request to handler with parameters defined Will retry until success code reached if retry_for_success? is set
41 42 43 44 45 46 47 48 49 |
# File 'lib/soaspec/exchange.rb', line 41 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 (200..299).cover? @api_class.status_code_for(response) response end end |
#response ⇒ Object
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)
78 79 80 |
# File 'lib/soaspec/exchange.rb', line 78 def response @response ||= make_request end |
#retrieve(name) ⇒ Object
Retrieve the stored value from the Api Handler
61 62 63 64 65 |
# File 'lib/soaspec/exchange.rb', line 61 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
13 14 15 16 |
# File 'lib/soaspec/exchange.rb', line 13 def retry_for_success @retry_for_success = true self end |
#retry_for_success? ⇒ Bool
Returns Whether to keep making request until success code reached.
19 20 21 |
# File 'lib/soaspec/exchange.rb', line 19 def retry_for_success? @retry_for_success end |
#status_code ⇒ Integer
Get status code from api class. This is http response for Web Api
84 85 86 |
# File 'lib/soaspec/exchange.rb', line 84 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
54 55 56 |
# File 'lib/soaspec/exchange.rb', line 54 def store(name, value) @api_class.store(name, self[value]) end |
#to_s ⇒ String
Name describing this class when used with ‘RSpec.describe` This will make the request and store the response
70 71 72 |
# File 'lib/soaspec/exchange.rb', line 70 def to_s @test_name end |