Class: Exchange

Inherits:
Object
  • Object
show all
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

It is tied to an ExchangeHandler that needs to be defined either globally before it’s created or in ‘default_handler_used’

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Soaspec::ExchangeProperties

default_handler, expect_positive_status

Methods included from Soaspec::VariableStorer

#retrieve, #store

Methods included from Soaspec::ExchangeRepeater

#until

Methods included from Soaspec::RequestBuilder

#+, #[]=, #method=, #method_missing, #respond_to_missing?, #save!, #suburl=

Methods included from Soaspec::ExchangeExtractor

#[], #element?, #format, #pretty_response_body, #request, #status_code, #successful_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’

Parameters:

  • name (Symbol, String) (defaults to: self.class.to_s)

    Name shown in RSpec run

  • override_parameters (Hash) (defaults to: {})

    Parameters to override for default params (set through ExchangeHandler or Exchange class) These are the parameters that would be specific for a test



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/soaspec/exchange/exchange.rb', line 65

def initialize(name = self.class.to_s, override_parameters = {})
  if name.is_a? Hash # Name not provided
    override_parameters = name
    name = nil
  end
  self.test_name ||= name.to_s
  @override_parameters = override_parameters
  # 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
  @fail_factory = nil
  @retry_for_success = false
  self.retry_count = exchange_handler.retry_exception_limit
  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_handlerExchangeHandler

Returns Instance of ExchangeHandler for which this exchange is made.

Returns:

  • (ExchangeHandler)

    Instance of ExchangeHandler for which this exchange is made



23
24
25
# File 'lib/soaspec/exchange/exchange.rb', line 23

def exchange_handler
  @exchange_handler
end

#fail_factory=(value) ⇒ Boolean (writeonly)

Returns Expect Factory to fail upon trying to create.

Returns:

  • (Boolean)

    Expect Factory to fail upon trying to create



31
32
33
# File 'lib/soaspec/exchange/exchange.rb', line 31

def fail_factory=(value)
  @fail_factory = value
end

#override_parametersHash

Returns Parameters to override for default params defined in ExchangeHandler These are the parameters specific to the Exchange and will override, append to what’s defined in the ExchangeHandler.

Returns:

  • (Hash)

    Parameters to override for default params defined in ExchangeHandler These are the parameters specific to the Exchange and will override, append to what’s defined in the ExchangeHandler



35
36
37
# File 'lib/soaspec/exchange/exchange.rb', line 35

def override_parameters
  @override_parameters
end

#retry_countInteger

Returns How many times to retry for a success.

Returns:

  • (Integer)

    How many times to retry for a success



25
26
27
# File 'lib/soaspec/exchange/exchange.rb', line 25

def retry_count
  @retry_count
end

#test_nameString

Returns Name used for displaying class.

Returns:

  • (String)

    Name used for displaying class



29
30
31
# File 'lib/soaspec/exchange/exchange.rb', line 29

def test_name
  @test_name
end

#times_retriedInteger

Returns Times request was retried before being returned.

Returns:

  • (Integer)

    Times request was retried before being returned



27
28
29
# File 'lib/soaspec/exchange/exchange.rb', line 27

def times_retried
  @times_retried
end

Instance Method Details

#callResponseObject

Returns Currently returning response object. This will change (in 0.3) to be itself to allow easy method chaining.

Returns:

  • (ResponseObject)

    Currently returning response object. This will change (in 0.3) to be itself to allow easy method chaining



127
128
129
130
# File 'lib/soaspec/exchange/exchange.rb', line 127

def call
  response
  self
end

#default_handler_usedSoaspec::ExchangeHandler

Override this in subclass to tie that subclass to an ExchangeHandler

Returns:



58
# File 'lib/soaspec/exchange/exchange.rb', line 58

def default_handler_used; end

#invalid_exception?Boolean

Defined as general rule from ExchangeHandler

Returns:

  • (Boolean)

    Whether exception is an exception that must be retried



52
53
54
# File 'lib/soaspec/exchange/exchange.rb', line 52

def invalid_exception?
  !exchange_handler.retry_on_exceptions.find { |e| e == exchange_handler.exception.class }.nil?
end

#make_requestResponse

Make request to handler with parameters defined Will retry until success code reached if retry_for_success? is set

Returns:

  • (Response)

    Response from Api handler



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/soaspec/exchange/exchange.rb', line 88

def make_request
  Soaspec::SpecLogger.info 'Example ' + test_name
  request_params = @override_parameters
  (0..retry_count).each do |count|
    response = exchange_handler.make_request(request_params)
    return response if !retry_for_success? && !invalid_exception?
    return response if (200..299).cover? exchange_handler.status_code_for(response)

    sleep exchange_handler.retry_pause_time # Time before retrying
    self.times_retried = count
    break response if count == retry_count
  end
end

#request_parametersHash

Returns Hash representing what will be sent.

Returns:

  • (Hash)

    Hash representing what will be sent



81
82
83
# File 'lib/soaspec/exchange/exchange.rb', line 81

def request_parameters
  exchange_handler.request_parameters(@override_parameters)
end

#responseRestClient::Response, Savon::Response

Returns response object from Api. Will make the request if not made and then cache it for later on

Examples:

For SOAP it will be a Savon response

response.body (body of response as Hash)
response.header (head of response as Hash)

For REST it will be a RestClient::Response

Returns:

  • (RestClient::Response, Savon::Response)

    Returns response object from Api. Will make the request if not made and then cache it for later on



115
116
117
118
119
120
121
122
123
# File 'lib/soaspec/exchange/exchange.rb', line 115

def response
  require 'forwardable'
  Soaspec.last_exchange = self
  @response ||= make_request
  @response.define_singleton_method(:exchange) { Soaspec.last_exchange } unless @response.respond_to?(:exchange)
  @response.extend Forwardable
  @response.delegate %i[value_from_path values_from_path] => :exchange
  @response
end

#retry_for_successObject

Set retry for success variable to true so that request will be retried for retry_count until it’s true



39
40
41
42
# File 'lib/soaspec/exchange/exchange.rb', line 39

def retry_for_success
  @retry_for_success = true
  self
end

#retry_for_success?Bool

This is set on an individual Exchange marking it as one that should be retried

Returns:

  • (Bool)

    Whether to keep making request until success code reached



46
47
48
# File 'lib/soaspec/exchange/exchange.rb', line 46

def retry_for_success?
  @retry_for_success
end

#to_sString

Name describing this class when used with ‘RSpec.describe` This will make the request and store the response

Returns:

  • (String)

    Name given when initializing



105
106
107
# File 'lib/soaspec/exchange/exchange.rb', line 105

def to_s
  test_name
end