Class: Soaspec::ExchangeHandler

Inherits:
Object
  • Object
show all
Extended by:
HandlerAccessors
Defined in:
lib/soaspec/exchange_handlers/exchange_handler.rb

Overview

Inherit this for a class describing how to implement a particular exchange. Has basic methods common for methods defining RSpec tests in YAML

Direct Known Subclasses

RestHandler, SoapHandler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HandlerAccessors

attribute, convert_to_lower, default_hash, element, mandatory_elements, mandatory_json_values, mandatory_xpath_values, strip_namespaces

Constructor Details

#initialize(name = self.class.to_s, _options = {}) ⇒ ExchangeHandler

Set instance variable name

Parameters:

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

    Name used when describing API test

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

    Parameters defining handler. Used in descendants

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 24

def initialize(name = self.class.to_s, _options = {})
  use
  @request_option = :hash
  raise ArgumentError, 'Cannot define both template_name and default_hash' if respond_to?(:template_name_value) && respond_to?(:default_hash_value)

  @template_name = respond_to?(:template_name_value) ? template_name_value : ''
  @default_hash = respond_to?(:default_hash_value) ? default_hash_value : {}
  @name = name
end

Instance Attribute Details

#template_nameObject

Retrieve the name of the template file to be used in the API request



14
15
16
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 14

def template_name
  @template_name
end

Class Method Details

.useObject



9
10
11
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 9

def self.use
  new.use
end

Instance Method Details

#convert_to_lower?Boolean

Returns Whether all xpaths will be done with XML that is converted to lower case.

Returns:

  • (Boolean)

    Whether all xpaths will be done with XML that is converted to lower case



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

def convert_to_lower?
  false
end

#default_hash=(hash) ⇒ Object

Set the default hash representing data to be used in making a request This will set the @request_option instance variable too



50
51
52
53
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 50

def default_hash=(hash)
  @request_option = :hash
  @default_hash = Soaspec.always_use_keys? ? hash.transform_keys_to_symbols : hash
end

#elementsObject

Explicitly defined elements for which a path has been predefined



17
18
19
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 17

def elements
  public_methods.select { |i| i[/__custom_path_.+/] }
end

#expected_mandatory_elementsArray

Will be used in ‘success_scenarios’ shared examples. Set though ‘mandatory_elements’ method

Returns:

  • (Array)

    Array of symbols specifying element names



66
67
68
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 66

def expected_mandatory_elements
  []
end

#expected_mandatory_json_valuesHash

Change this through ‘mandatory_json_values’ method to specify json results that must be present in the response Will be used in ‘success_scenarios’ shared examples

Returns:

  • (Hash)

    Hash of ‘json/path’ => ‘expected value’ pairs



80
81
82
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 80

def expected_mandatory_json_values
  {}
end

#expected_mandatory_xpath_valuesHash

Change this through ‘mandatory_xpath_values’ method 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



73
74
75
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 73

def expected_mandatory_xpath_values
  {}
end

#request(response) ⇒ Object

Request of API call. Either intended request or actual request

Parameters:

  • response (Object)

    Response from calling exchange



120
121
122
123
124
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 120

def request(response)
  return "Request not yet sent Request option is #{@request_option}" unless response

  'Specific API handler should implement this'
end

#set_remove_key(hash, key) ⇒ Object

Set instance variable and remove it from Hash

Parameters:

  • hash (Hash)

    Hash to remove/retrieve keys from

  • key (String, Symbol)

    Key to remove and to set instance variable for



106
107
108
109
110
111
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 106

def set_remove_key(hash, key)
  return unless hash.key? key

  __send__("#{key}=", hash[key])
  hash.delete key
end

#set_remove_keys(hash, keys) ⇒ Object

Set instance variable for each key in hash remove it from Hash

Parameters:

  • hash (Hash)

    with items to remove from

  • keys (Array)

    List of keys to remove from hash, setting each one



99
100
101
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 99

def set_remove_keys(hash, keys)
  keys.each { |key| set_remove_key(hash, key) }
end

#store(name, value) ⇒ Object

Stores a value in a method that can be accessed by the provided name

Parameters:

  • name (Symbol)

    Name of method to use to access this value within handler

  • value (String)

    Value to store



92
93
94
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 92

def store(name, value)
  define_singleton_method('__stored_val__' + name.to_s) { value }
end

#strip_namespaces?Boolean

Returns Whether to remove namespaces in xpath assertion automatically.

Returns:

  • (Boolean)

    Whether to remove namespaces in xpath assertion automatically



114
115
116
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 114

def strip_namespaces?
  false
end

#to_sString

Sets api handler variable globally. This is used in ‘Exchange’ class

Returns:

  • (String)

    Name set upon initialisation



43
44
45
46
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 43

def to_s
  use
  @name.to_s
end

#useSelf

Set Api handler used by Exchange class to this handler

Returns:

  • (Self)


36
37
38
39
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 36

def use
  Soaspec.api_handler = self
  self
end