Class: Soaspec::ExchangeHandler

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

Overview

Inherit this for a class describing how to implement a particular exchange. Has methods common to Soaspec framework for working with Exchange/Handler pair

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

Methods included from ExchangeHandlerDefaults

#convert_to_lower?, #expected_mandatory_elements, #expected_mandatory_json_values, #expected_mandatory_xpath_values, #request, #retry_exception_limit, #retry_on_exceptions, #retry_pause_time, #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)


36
37
38
39
40
41
42
43
44
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 36

def initialize(name = self.class.to_s, _options = {})
  use
  self.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

#exceptionException

Returns Exception if raised.

Returns:

  • (Exception)

    Exception if raised



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

def exception
  @exception
end

#request_optionSymbol

Returns Option used to generate Request Body. Either :hash or :template.

Returns:

  • (Symbol)

    Option used to generate Request Body. Either :hash or :template



26
27
28
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 26

def request_option
  @request_option
end

#template_nameString

Returns Name of the template file to be used in the API request.

Returns:

  • (String)

    Name of the template file to be used in the API request



24
25
26
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 24

def template_name
  @template_name
end

Class Method Details

.useExchangeHandler

Use an instance of this ExchangeHandler in any further Exchange’s This is a convenience method as it creates an ExchangeHandler behind the scenes

Returns:



19
20
21
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 19

def self.use
  new.use
end

Instance Method Details

#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

Parameters:

  • hash (Hash)

    Hash to send in request body by default



63
64
65
66
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 63

def default_hash=(hash)
  self.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



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

def elements
  public_methods.select { |i| i[/__custom_path_.+/] }
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



93
94
95
96
97
98
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 93

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



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

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



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

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

#to_sString

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

Returns:

  • (String)

    Name set upon initialisation



55
56
57
58
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 55

def to_s
  use
  @name.to_s
end

#useSelf

Set Api handler used by Exchange class to this handler

Returns:

  • (Self)


48
49
50
51
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 48

def use
  Soaspec.api_handler = self
  self
end