class Porteo::Message
A message which will be send by any protocol and gateway.
The content of a message will be defined in a template, a file that contain differents sections each being one part of the message. This templates will be processed with ERB so its can contain ruby code to get more flexibility.
The configuration options (for protocols and gateways) it set trought emitter files, special files in YAML format.
Attributes
Path to configuration directory. It have to end in a slash.
File used to load the configuration information.
Profile used to recover the gateway configuration.
The name of the protocol used to send the message.
The one who should receive the message.
Name of template used to send the message.
A hash containing message sections defined in the template.
Parameters to set the fields defined in the template.
Path to templates directory. It have to end in a slash.
An array containing required fields to define the template.
Public Class Methods
Creates a new message. @param [String] emitter File used to load the configuration information. @param [String] protocol Protocol
to be used (mail, sms, twitter). @param [String] profile Profile to load gateway information. @param [Hash] opts Options. @option opts :config_path (“./config/”) Configuration path. @option opts :template_path (“./config/templates/”) Templates path.
# File lib/message/message.rb, line 71 def initialize( emitter = "", protocol = "", profile = "default", template = "", opts = {} ) # config_path value should end in a trailing slash opts[:config_path] ||= CONFIG_ROOT @config_path = opts[:config_path] # template_path value should end in a trailing slash opts[:template_path] ||= TEMPLATES_ROOT @template_path = opts[:template_path] # Instance variables initilization @template = template @template_params = {} @template_content = "" @template_requires = [] @receiver = nil # Assign instance variables @emitter = emitter @profile = profile @protocol = protocol end
Public Instance Methods
Convenience method to allow configuration options to be set in a block. @return [nil]
# File lib/message/message.rb, line 96 def configure yield self end
Method missing is used to allow set params one by one. @param [Symbol] method param to be set. @param [Array] params params in the call. @param [Block] block block code in method.
# File lib/message/message.rb, line 153 def method_missing( method, *params, &block ) # We only allow one param to be passed # so we check that only one param is passed # and block is nil # We want to use the prefered configuration style # so we expect to use a call like this: # my_obj.method_name = value if method[-1] == "=" and params.size == 1 and block == nil @template_params[method.to_s.chop.to_sym] = params[0] else super( method, params, block ) end end
Send a message using protocol, content and configuration set before. @return [nil] @raise [ArgumentError] If emitter file is not valid or if protocol is not defined.
# File lib/message/message.rb, line 112 def send_message load_template( @template ) # Load configuration information for the gateway begin config = YAML.load_file( "#{@config_path}#{@emitter}.emitter" ) rescue Errno::ENOENT raise ArgumentError, "Message Error. Invalid emitter file '#{@config_path}#{@emitter}.emitter'. Check emitter name is correct. Emitter path can also be set throught config_path." end raise ArgumentError, "Message Error. Profile '#{@profile}' not found." unless config[@protocol.to_sym][@profile.to_sym] begin # Creates a new instance of defined protocol @protocol_obj = Porteo.const_get( "#{@protocol}_protocol".capitalize.to_sym ).new( config[@protocol.to_sym][@profile.to_sym] ) rescue NameError raise ArgumentError, "Message Error. Undefined protocol. Check if '#{@protocol}_protocol.rb' is created and is valid." end # Set template values @protocol_obj.set_template( @template_content, @template_requires ) @protocol_obj.set_template_params( @template_params ) # Set receiver @protocol_obj.receiver = @receiver # Send the message @protocol_obj.send_message end
Assign values to fields defined in the template. Overwrite all params set before. @param [Hash] params The keys are the fields defined in the
template which will be set to the hash value.
@return [nil]
# File lib/message/message.rb, line 105 def set_template_params( params ) @template_params = params end
Method to see the complete message by sections, once it has been sent. @return [String] the message sections
# File lib/message/message.rb, line 145 def show_message @protocol_obj.message unless @protocol_obj == nil end
Private Instance Methods
Recover the information defined in a template file. The template is defined using YAML. It has the format of a hash, containing a key named requires, which value is an array of required fields for the template, and a key named template which contain the template itself. @param [String] template Template name to be used. Various templates may
have the same name (alert.mail, alert.sms, alert.twitter) but the one named as protocol used will be used. So template name it just the first part of the template filename.
@return [nil]
# File lib/message/message.rb, line 177 def load_template( template ) begin content = YAML.load_file( "#{@template_path}#{template}.#{@protocol}" ) rescue Errno::ENOENT raise ArgumentError, "Message Error. Invalid template file '#{@template_path}#{template}.#{@protocol}'. Check if template name is correct and you are using a valid protocol. Template path can also be set throught template_path." end if( content ) @template_content = content[:template].to_s @template_requires = content[:requires] if content[:requires] != nil end end