Class: Soaspec::ExchangeHandler
- Inherits:
-
Object
- Object
- Soaspec::ExchangeHandler
- 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
Instance Attribute Summary collapse
-
#exception ⇒ Exception
Exception if raised.
-
#request_option ⇒ Symbol
Option used to generate Request Body.
-
#template_name ⇒ String
Name of the template file to be used in the API request.
Class Method Summary collapse
-
.use ⇒ ExchangeHandler
Use an instance of this ExchangeHandler in any further Exchange’s This is a convenience method as it creates an ExchangeHandler behind the scenes.
Instance Method Summary collapse
-
#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.
-
#elements ⇒ Object
Explicitly defined elements for which a path has been predefined.
-
#initialize(name = self.class.to_s, _options = {}) ⇒ ExchangeHandler
constructor
Set instance variable name.
-
#set_remove_key(hash, key) ⇒ Object
Set instance variable and remove it from Hash.
-
#set_remove_keys(hash, keys) ⇒ Object
Set instance variable for each key in hash remove it from Hash.
-
#store(name, value) ⇒ Object
Stores a value in a method that can be accessed by the provided name.
-
#to_s ⇒ String
Sets api handler variable globally.
-
#use ⇒ Self
Set Api handler used by Exchange class to this handler.
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
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, = {}) 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
#exception ⇒ Exception
Returns Exception if raised.
14 15 16 |
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 14 def exception @exception end |
#request_option ⇒ Symbol
Returns 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_name ⇒ String
Returns 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
.use ⇒ ExchangeHandler
Use an instance of this ExchangeHandler in any further Exchange’s This is a convenience method as it creates an ExchangeHandler behind the scenes
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
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 |
#elements ⇒ Object
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
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
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
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_s ⇒ String
Sets api handler variable globally. This is used in ‘Exchange’ class
55 56 57 58 |
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 55 def to_s use @name.to_s end |
#use ⇒ Self
Set Api handler used by Exchange class to this handler
48 49 50 51 |
# File 'lib/soaspec/exchange_handlers/exchange_handler.rb', line 48 def use Soaspec.api_handler = self self end |