Class: Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/soaspec/exchange.rb

Overview

This represents a request / response pair

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = self.class.to_s, override_parameters = {}) ⇒ Exchange

Returns a new instance of Exchange.

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



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/soaspec/exchange.rb', line 29

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|
    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

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

Parameters:

  • method_name (Object)
  • args (Object)
  • block (Object)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/soaspec/exchange.rb', line 135

def method_missing(method_name, *args, &block)
  if method_name[-1] == '='
    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_classObject (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

#default_paramsObject

Params used when making a request



11
12
13
# File 'lib/soaspec/exchange.rb', line 11

def default_params
  @default_params
end

#retry_countObject

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_nameObject

Name used for displaying class



13
14
15
# File 'lib/soaspec/exchange.rb', line 13

def test_name
  @test_name
end

Instance Method Details

#[](path) ⇒ String

Extract value from path api class

Parameters:

  • path (Object)

    Path to return element for api class E.g - for SOAP this is XPath string. For JSON, this is Hash dig Array

Returns:

  • (String)

    Value at path



119
120
121
# File 'lib/soaspec/exchange.rb', line 119

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



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

def []=(key, value)
  self.default_params = { body: {} } unless default_params # Initialize as Hash if not set
  default_params[:body][key] = value
end

#dummy_requestBoolean

Dummy request used to make a request without verifying it and ignoring WSDL errors

Returns:

  • (Boolean)

    Always returns true. Unless of course an unexpected exception occurs



107
108
109
110
111
112
113
114
# File 'lib/soaspec/exchange.rb', line 107

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_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



55
56
57
58
59
60
61
62
63
64
# File 'lib/soaspec/exchange.rb', line 55

def make_request
  Soaspec::SpecLogger.add_to 'Example ' + test_name
  request_params = default_params ? merge_request_body : @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

#merge_request_bodyObject

Merge exchange initialized request params with ones set later on



43
44
45
46
47
48
49
50
# File 'lib/soaspec/exchange.rb', line 43

def merge_request_body
  if @override_parameters[:body]
    @override_parameters[:body].merge!(default_params[:body])
    @override_parameters
  else
    @override_parameters.merge(default_params[:body])
  end
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Used for setters that are not defined

Returns:

  • (Boolean)


150
151
152
# File 'lib/soaspec/exchange.rb', line 150

def respond_to_missing?(method_name, *args)
  method_name[-1] == '=' || super
end

#responseObject 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)



93
94
95
# File 'lib/soaspec/exchange.rb', line 93

def response
  @response ||= make_request
end

#retrieve(name) ⇒ Object

Retrieve the stored value from the Api Handler

Parameters:

  • name (String, Symbol)

    Name of value to retrieve

Returns:

  • (Object)

    value from the Api Handler stored previously



76
77
78
79
80
# File 'lib/soaspec/exchange.rb', line 76

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_successObject

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



17
18
19
20
# File 'lib/soaspec/exchange.rb', line 17

def retry_for_success
  @retry_for_success = true
  self
end

#retry_for_success?Bool

Returns Whether to keep making request until success code reached.

Returns:

  • (Bool)

    Whether to keep making request until success code reached



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

def retry_for_success?
  @retry_for_success
end

#save!Self

Makes request, caching the response and returning self Used by FactoryBot

Returns:

  • (Self)


157
158
159
160
# File 'lib/soaspec/exchange.rb', line 157

def save!
  call
  self
end

#status_codeInteger

Get status code from api class. This is http response for Web Api

Returns:

  • (Integer)

    Status code from api class



101
102
103
# File 'lib/soaspec/exchange.rb', line 101

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

Parameters:

  • name (Symbol)

    Name of method to use to access this value within handler

  • value (String)

    Path to value to store



69
70
71
# File 'lib/soaspec/exchange.rb', line 69

def store(name, value)
  @api_class.store(name, self[value])
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



85
86
87
# File 'lib/soaspec/exchange.rb', line 85

def to_s
  test_name
end