Class: Porteo::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/message/message.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(emitter = "", protocol = "", profile = "default", template = "", opts = {}) ⇒ Message

Creates a new message.

Parameters:

  • emitter (String) (defaults to: "")

    File used to load the configuration information.

  • protocol (String) (defaults to: "")

    Protocol to be used (mail, sms, twitter).

  • profile (String) (defaults to: "default")

    Profile to load gateway information.

  • opts (Hash) (defaults to: {})

    Options.

Options Hash (opts):

  • :config_path (Object) — default: "./config/"

    Configuration path.

  • :template_path (Object) — default: "./config/templates/"

    Templates path.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params, &block) ⇒ Object

Method missing is used to allow set params one by one.

Parameters:

  • method (Symbol)

    param to be set.

  • params (Array)

    params in the call.

  • block (Block)

    block code in method.



153
154
155
156
157
158
159
160
161
162
163
164
165
# 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

Instance Attribute Details

#config_pathObject

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



53
54
55
# File 'lib/message/message.rb', line 53

def config_path
  @config_path
end

#emitterObject

File used to load the configuration information.



61
62
63
# File 'lib/message/message.rb', line 61

def emitter
  @emitter
end

#profileObject

Profile used to recover the gateway configuration.



59
60
61
# File 'lib/message/message.rb', line 59

def profile
  @profile
end

#protocolObject

The name of the protocol used to send the message.



47
48
49
# File 'lib/message/message.rb', line 47

def protocol
  @protocol
end

#receiverObject

The one who should receive the message.



57
58
59
# File 'lib/message/message.rb', line 57

def receiver
  @receiver
end

#templateObject

Name of template used to send the message.



49
50
51
# File 'lib/message/message.rb', line 49

def template
  @template
end

#template_contentObject (readonly)

A hash containing message sections defined in the template.



42
43
44
# File 'lib/message/message.rb', line 42

def template_content
  @template_content
end

#template_paramsObject

Parameters to set the fields defined in the template.



51
52
53
# File 'lib/message/message.rb', line 51

def template_params
  @template_params
end

#template_pathObject

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



55
56
57
# File 'lib/message/message.rb', line 55

def template_path
  @template_path
end

#template_requiresObject (readonly)

An array containing required fields to define the template.



44
45
46
# File 'lib/message/message.rb', line 44

def template_requires
  @template_requires
end

Instance Method Details

#configure {|_self| ... } ⇒ nil

Convenience method to allow configuration options to be set in a block.

Yields:

  • (_self)

Yield Parameters:

Returns:

  • (nil)


96
97
98
# File 'lib/message/message.rb', line 96

def configure
  yield self
end

#send_messagenil

Send a message using protocol, content and configuration set before.

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    If emitter file is not valid or if protocol is not defined.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ nil

Assign values to fields defined in the template. Overwrite all params set before.

Parameters:

  • params (Hash)

    The keys are the fields defined in the template which will be set to the hash value.

Returns:

  • (nil)


105
106
107
# File 'lib/message/message.rb', line 105

def set_template_params( params )
  @template_params = params 
end

#show_messageString

Method to see the complete message by sections, once it has been sent.

Returns:

  • (String)

    the message sections



145
146
147
# File 'lib/message/message.rb', line 145

def show_message
  @protocol_obj.message unless @protocol_obj == nil
end