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

config_path[RW]

Path to configuration directory. It have to end in a slash.

emitter[RW]

File used to load the configuration information.

profile[RW]

Profile used to recover the gateway configuration.

protocol[RW]

The name of the protocol used to send the message.

receiver[RW]

The one who should receive the message.

template[RW]

Name of template used to send the message.

template_content[R]

A hash containing message sections defined in the template.

template_params[RW]

Parameters to set the fields defined in the template.

template_path[RW]

Path to templates directory. It have to end in a slash.

template_requires[R]

An array containing required fields to define the template.

Public Class Methods

new( emitter = "", protocol = "", profile = "default", template = "", opts = {} ) click to toggle source

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

configure() { |self| ... } click to toggle source

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( method, *params, &block ) click to toggle source

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.

Calls superclass 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_message() click to toggle source

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
set_template_params( params ) click to toggle source

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
show_message() click to toggle source

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

load_template( template ) click to toggle source

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