class Porteo::Protocol
Base class to implement common funcionality for all protocols.
In Porteo
system, the protocol creates an appropiate gateway to send the message through it.
Attributes
Hash with that contain gateway configuration parameters
Who the message is going to be sent to
Public Class Methods
Creates a new instance of a protocol @param [Hash] Gateway
configuration parameters.
# File lib/protocols/protocol.rb, line 44 def initialize( gw_config ) # Initilization @gw_config = gw_config @param = {} @template = "" @requires = [] @message_sections = [] @receiver = nil end
Public Instance Methods
The raw message sections hash to be sent. @return [String] Message
sections which will be sent. This method
returns the message as a string containing every section of the message already parsed.
@note As sections can be dynamic (because of ERB preprocessing) this method may not show some sections present in a template, depending on the parameters passed.
# File lib/protocols/protocol.rb, line 83 def message # Call to expand_template expand_template.to_s end
Send the message defined by template and template parameters. It used the gateway configuration options to send the message through a third-party ruby gem. @return [nil] @raise [ArgumentError] If gateway is not defined.
# File lib/protocols/protocol.rb, line 93 def send_message # Expand the template, here we also check if template is well-formatted @message_sections = expand_template # As we can define instance variables with highest priority than template # tags, we may want to override those tags for a certain protocol override_tags # Check if a well-formatted template contains all fields necessaries # to send this kind of message. check_message_sections begin # Create the appropiate gateway, which is defined in gw_config @gateway = Porteo.const_get( "#{@gw_config[:gateway]}_gateway".capitalize.to_sym ).new( @gw_config ) rescue NameError raise ArgumentError, "Protocol Error. Undefined gateway. Check if '#{@gw_config[:gateway]}_gateway.rb' is created and is a valid gateway" end # Send the message @gateway.init_send( @message_sections ) end
Set the template to be used. @param [Hash] template The template per se. @param [Array] requires The requires of the template. Which is the
required fields for this template. Those required fields has to be present in the template parameters or an error will be raised.
@return [nil]
# File lib/protocols/protocol.rb, line 63 def set_template( template, requires ) @template = template @requires = requires end
Set the values of template parameters @param [Hash] param Pairs of required fields for the template and
the value its take.
@return [nil]
# File lib/protocols/protocol.rb, line 72 def set_template_params( param ) @param = param end
Private Instance Methods
@abstract Check for all required sections to be defined. @return [nil] @raise [Exception] This method is not meant to be call but to be overwritten. @note This method has to be overwritten.
# File lib/protocols/protocol.rb, line 124 def check_message_sections raise Exception, "Protocol Error. This method has to be overwritten. You are trying to check for required sections in a generic protocol." end
Expand the message from template and variables. A template is a collection of sections which define the content of a message, but those sections are dynamic thanks to the use of ERB code. At the same time, the parameters are filled with it value. @return [nil] @raise [ArgumentError] If a required param is not present or it is nil.
# File lib/protocols/protocol.rb, line 142 def expand_template # Check for existence of required parameters @requires.each do |required_param| raise ArgumentError, "Protocol Error. Required parameter '#{required_param.to_s}' is not present or it is nil." if @param[required_param] == nil end param = @param erb_template = ERB.new( @template, 0, "%<>" ) # Binding get the execution context to allow the use of # param in the template template_filled = erb_template.result( binding ) # We use eval to get a Hash with message sections because # erb return a string # [:body] => "..." # [:attachments] => "..." # ... eval( template_filled ) end