Class: Soaspec::RestHandler

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

Overview

Wraps around Savon client defining default values dependent on the soap request

Instance Attribute Summary collapse

Attributes inherited from Tester

#template_name

Instance Method Summary collapse

Methods inherited from Tester

#to_s

Constructor Details

#initialize(name, specific_options = {}) ⇒ RestHandler

Setup object to handle communicating with a particular SOAP WSDL

Parameters:



45
46
47
48
49
50
51
# File 'lib/soaspec/rest_handler.rb', line 45

def initialize(name, specific_options = {})
  options = default_options.merge logging_options
  options.merge! rest_resource_options
  options.merge!(specific_options)
  @resource = RestClient::Resource.new(base_url, options: options) # @resource[url_extension].get
  super
end

Instance Attribute Details

#clientObject

Savon client used to make SOAP calls



10
11
12
# File 'lib/soaspec/rest_handler.rb', line 10

def client
  @client
end

#operationObject

SOAP Operation to use by default



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

def operation
  @operation
end

Instance Method Details

#base_urlObject

Override in class



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

def base_url
  ''
end

#default_optionsHash

Default Savon options. See savonrb.com/version2/globals.html for details

Returns:

  • (Hash)

    Default Savon options for all BasicSoapHandler



23
24
25
26
27
28
# File 'lib/soaspec/rest_handler.rb', line 23

def default_options
  {
      # method: :get
      # headers: { content_type: 'text/plain' }
  }
end

#include_in_body?(response, expected) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/soaspec/rest_handler.rb', line 72

def include_in_body?(response, expected)
  response.body.include? expected
end

#logging_optionsObject

Options to log xml request and response



15
16
17
18
19
# File 'lib/soaspec/rest_handler.rb', line 15

def logging_options
  {
       # See request and response. (Put this in traffic file)
  }
end

#make_request(override_parameters) ⇒ Object

Used in together with Exchange request that passes such override parameters



65
66
67
68
69
70
# File 'lib/soaspec/rest_handler.rb', line 65

def make_request(override_parameters)
  test_values = override_parameters
  test_values[:params] ||= {}

  @resource[test_values[:suburl]].send(test_values[:method].to_s, test_values[:params])
end

#mandatory_elementsArray

Override this to specify elements that must be present in the response Will be used in ‘success_scenarios’ shared examples

Returns:

  • (Array)

    Array of symbols specifying element names



83
84
85
# File 'lib/soaspec/rest_handler.rb', line 83

def mandatory_elements
  []
end

#mandatory_xpath_valuesHash

Override this to specify xpath results that must be present in the response Will be used in ‘success_scenarios’ shared examples

Returns:

  • (Hash)

    Hash of ‘xpath’ => ‘expected value’ pairs



90
91
92
# File 'lib/soaspec/rest_handler.rb', line 90

def mandatory_xpath_values
  {}
end

#name(name) ⇒ Object



53
54
55
56
57
# File 'lib/soaspec/rest_handler.rb', line 53

def name(name)
  @test_values = {}
  @test_name = name
  self
end

#override(request_parameters) ⇒ Object



59
60
61
62
# File 'lib/soaspec/rest_handler.rb', line 59

def override(request_parameters)
  @test_values = request_parameters
  self
end

#rest_resource_optionsHash

Add values to here when extending this class to have default REST options. See rest client resource for details

Returns:

  • (Hash)

    Options adding to & overriding defaults



38
39
40
41
# File 'lib/soaspec/rest_handler.rb', line 38

def rest_resource_options
  {
  }
end

#root_attributesObject

Attributes set at the root XML element of SOAP request



95
96
97
# File 'lib/soaspec/rest_handler.rb', line 95

def root_attributes
  nil
end

#status_code_for(response) ⇒ Object



76
77
78
# File 'lib/soaspec/rest_handler.rb', line 76

def status_code_for(response)
  response.code
end

#value_from_path(exchange, path) ⇒ Object



114
115
116
117
# File 'lib/soaspec/rest_handler.rb', line 114

def value_from_path(exchange, path)
  path = '//' + path if path[0] != '/'
  xpath_value_for(exchange: exchange, xpath: path)
end

#xpath_value_for(param) ⇒ Object

Returns the value at the provided xpath

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/soaspec/rest_handler.rb', line 100

def xpath_value_for(param)
  result =
      if Soaspec::Environment.strip_namespaces? && !param[:xpath].include?(':')
        temp_doc = param[:exchange].response.doc
        temp_doc.remove_namespaces!
        temp_doc.xpath(param[:xpath]).first
      else
        puts 'no strip' + param[:xpath]
        param[:exchange].response.xpath(param[:xpath]).first
      end
  raise NoElementAtXpath, "No value at Xpath '#{param[:xpath]}'" unless result
  result.inner_text
end