Class: Exchange
- Inherits:
-
Object
- Object
- Exchange
- Extended by:
- Soaspec::ExchangeProperties
- Includes:
- Soaspec::ExchangeExtractor, Soaspec::ExchangeRepeater, Soaspec::RequestBuilder, Soaspec::VariableStorer
- Defined in:
- lib/soaspec/exchange/exchange.rb
Overview
This represents a request / response pair Essentially, params in the exchange that are set are related to the request What is returned is related to the response
Instance Attribute Summary collapse
-
#exchange_handler ⇒ Object
Instance of ExchangeHandler for which this exchange is made.
-
#fail_factory ⇒ Object
writeonly
Expect Factory to fail upon trying to create.
-
#override_parameters ⇒ Object
Parameters to override for default params.
-
#retry_count ⇒ Object
How many times to retry for a success.
-
#test_name ⇒ Object
Name used for displaying class.
Instance Method Summary collapse
-
#default_handler_used ⇒ Boolean
Soaspec::ExchangeHandler used by this exchange.
-
#initialize(name = self.class.to_s, override_parameters = {}) ⇒ Exchange
constructor
Create new Exchange according to parameters set.
-
#make_request ⇒ Response
Make request to handler with parameters defined Will retry until success code reached if retry_for_success? is set.
-
#response ⇒ Object
(also: #call)
Returns response object from Api.
-
#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.
-
#to_s ⇒ String
Name describing this class when used with ‘RSpec.describe` This will make the request and store the response.
Methods included from Soaspec::ExchangeProperties
default_handler, expect_positive_status
Methods included from Soaspec::VariableStorer
Methods included from Soaspec::ExchangeRepeater
Methods included from Soaspec::RequestBuilder
#[]=, #method=, #method_missing, #respond_to_missing?, #save!, #suburl=
Methods included from Soaspec::ExchangeExtractor
#[], #element?, #methods_for_element, #request, #status_code, #to_hash, #values_from_path
Constructor Details
#initialize(name = self.class.to_s, override_parameters = {}) ⇒ Exchange
Create new Exchange according to parameters set. A response will be made if called explicitly with ‘response’ method or through other methods that use it like ‘status_code’
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/soaspec/exchange/exchange.rb', line 50 def initialize(name = self.class.to_s, override_parameters = {}) self.test_name ||= name.to_s # As a last resort this uses the global parameter. The handler should be set straight before an exchange is made to use this @exchange_handler ||= default_handler_used || Soaspec.api_handler raise '@exchange_handler not set. Set either with `Soaspec.api_handler = Handler.new` or within the exchange' unless @exchange_handler @fail_factory = nil @override_parameters = override_parameters @retry_for_success = false self.retry_count = 3 @exchange_handler.elements.each { |element| methods_for_element(element) } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Soaspec::RequestBuilder
Instance Attribute Details
#exchange_handler ⇒ Object
Instance of ExchangeHandler for which this exchange is made
19 20 21 |
# File 'lib/soaspec/exchange/exchange.rb', line 19 def exchange_handler @exchange_handler end |
#fail_factory=(value) ⇒ Object (writeonly)
Expect Factory to fail upon trying to create
25 26 27 |
# File 'lib/soaspec/exchange/exchange.rb', line 25 def fail_factory=(value) @fail_factory = value end |
#override_parameters ⇒ Object
Parameters to override for default params
27 28 29 |
# File 'lib/soaspec/exchange/exchange.rb', line 27 def override_parameters @override_parameters end |
#retry_count ⇒ Object
How many times to retry for a success
21 22 23 |
# File 'lib/soaspec/exchange/exchange.rb', line 21 def retry_count @retry_count end |
#test_name ⇒ Object
Name used for displaying class
23 24 25 |
# File 'lib/soaspec/exchange/exchange.rb', line 23 def test_name @test_name end |
Instance Method Details
#default_handler_used ⇒ Boolean
Returns Soaspec::ExchangeHandler used by this exchange.
42 43 44 |
# File 'lib/soaspec/exchange/exchange.rb', line 42 def default_handler_used nil end |
#make_request ⇒ Response
Make request to handler with parameters defined Will retry until success code reached if retry_for_success? is set
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/soaspec/exchange/exchange.rb', line 66 def make_request Soaspec::SpecLogger.info 'Example ' + test_name request_params = @override_parameters (1..retry_count).each do |count| response = exchange_handler.make_request(request_params) return response unless retry_for_success? return response if (200..299).cover? exchange_handler.status_code_for(response) sleep 0.5 break response if count == retry_count end 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
91 92 93 94 95 96 |
# File 'lib/soaspec/exchange/exchange.rb', line 91 def response Soaspec.last_exchange = self @response ||= make_request @response.define_singleton_method(:exchange) { Soaspec.last_exchange } unless @response.respond_to?(:exchange) @response 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
31 32 33 34 |
# File 'lib/soaspec/exchange/exchange.rb', line 31 def retry_for_success @retry_for_success = true self end |
#retry_for_success? ⇒ Bool
Returns Whether to keep making request until success code reached.
37 38 39 |
# File 'lib/soaspec/exchange/exchange.rb', line 37 def retry_for_success? @retry_for_success end |
#to_s ⇒ String
Name describing this class when used with ‘RSpec.describe` This will make the request and store the response
82 83 84 |
# File 'lib/soaspec/exchange/exchange.rb', line 82 def to_s test_name end |