Class: Porteo::Message
- Inherits:
-
Object
- Object
- Porteo::Message
- 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
-
#config_path ⇒ Object
Path to configuration directory.
-
#emitter ⇒ Object
File used to load the configuration information.
-
#profile ⇒ Object
Profile used to recover the gateway configuration.
-
#protocol ⇒ Object
The name of the protocol used to send the message.
-
#receiver ⇒ Object
The one who should receive the message.
-
#template ⇒ Object
Name of template used to send the message.
-
#template_content ⇒ Object
readonly
A hash containing message sections defined in the template.
-
#template_params ⇒ Object
Parameters to set the fields defined in the template.
-
#template_path ⇒ Object
Path to templates directory.
-
#template_requires ⇒ Object
readonly
An array containing required fields to define the template.
Instance Method Summary collapse
-
#configure {|_self| ... } ⇒ nil
Convenience method to allow configuration options to be set in a block.
-
#initialize(emitter = "", protocol = "", profile = "default", template = "", opts = {}) ⇒ Message
constructor
Creates a new message.
-
#method_missing(method, *params, &block) ⇒ Object
Method missing is used to allow set params one by one.
-
#send_message ⇒ nil
Send a message using protocol, content and configuration set before.
-
#set_template_params(params) ⇒ nil
Assign values to fields defined in the template.
-
#show_message ⇒ String
Method to see the complete message by sections, once it has been sent.
Constructor Details
#initialize(emitter = "", protocol = "", profile = "default", template = "", opts = {}) ⇒ Message
Creates a new message.
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.
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_path ⇒ Object
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 |
#emitter ⇒ Object
File used to load the configuration information.
61 62 63 |
# File 'lib/message/message.rb', line 61 def emitter @emitter end |
#profile ⇒ Object
Profile used to recover the gateway configuration.
59 60 61 |
# File 'lib/message/message.rb', line 59 def profile @profile end |
#protocol ⇒ Object
The name of the protocol used to send the message.
47 48 49 |
# File 'lib/message/message.rb', line 47 def protocol @protocol end |
#receiver ⇒ Object
The one who should receive the message.
57 58 59 |
# File 'lib/message/message.rb', line 57 def receiver @receiver end |
#template ⇒ Object
Name of template used to send the message.
49 50 51 |
# File 'lib/message/message.rb', line 49 def template @template end |
#template_content ⇒ Object (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_params ⇒ Object
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_path ⇒ Object
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_requires ⇒ Object (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.
96 97 98 |
# File 'lib/message/message.rb', line 96 def configure yield self end |
#send_message ⇒ nil
Send a message using protocol, content and configuration set before.
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 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. end |
#set_template_params(params) ⇒ nil
Assign values to fields defined in the template. Overwrite all params set before.
105 106 107 |
# File 'lib/message/message.rb', line 105 def set_template_params( params ) @template_params = params end |
#show_message ⇒ String
Method to see the complete message by sections, once it has been sent.
145 146 147 |
# File 'lib/message/message.rb', line 145 def @protocol_obj. unless @protocol_obj == nil end |