Module: Soaspec::RequestBuilder

Included in:
Exchange
Defined in:
lib/soaspec/exchange/request_builder.rb

Overview

Methods to help build a Request

Instance Method Summary collapse

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)

    Name of method not defined

  • args (Object)

    Arguments passed to method

  • block (Object)

    Block passed to method



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/soaspec/exchange/request_builder.rb', line 34

def method_missing(method_name, *args, &block)
  set_value = args.first
  if method_name[-1] == '=' # A setter method
    getter_name = method_name[0..-2]
    if set_value.class < Exchange # This would be prerequisite exchange
      define_singleton_method(getter_name) { set_value }
      self[getter_name] = set_value.id if set_value.respond_to?(:id)
    else
      self[getter_name] = set_value
    end
  else
    super
  end
end

Instance Method Details

#[]=(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

Examples:

exchange['name'] = 'tester'
# Will set { name: tester } in the response, formatting as JSON or XML depending on REST / SOAP

Parameters:

  • key (String, Symbol)

    Name of request element to set

  • value (String)

    Value to set request element to



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

def []=(key, value)
  @override_parameters[:body] ||= {}
  @override_parameters[:body][key] = value
end

#method=(method) ⇒ Object

Specify HTTP method to use. Default is :post

Parameters:

  • method (Symbol)

    HTTP method. E.g, :get, :patch



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

def method=(method)
  @override_parameters[:method] = method
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Used for setters that are not defined

Parameters:

  • method_name (String)

    Name of method

  • args (Array)

    List of arguments to method

Returns:

  • (Boolean)


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

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

#save!Self

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

Returns:

  • (Self)

    Return itself so methods can be called on itself afterwards



59
60
61
62
63
# File 'lib/soaspec/exchange/request_builder.rb', line 59

def save!
  @retry_for_success = @fail_factory ? false : true
  call
  self
end

#suburl=(url) ⇒ Object

Specify a url to add onto the base_url of the ExchangeHandler used

Parameters:

  • url (String)

    Url to add onto the base_url of the ExchangeHandler used



6
7
8
# File 'lib/soaspec/exchange/request_builder.rb', line 6

def suburl=(url)
  @override_parameters[:suburl] = url
end