Class: Porteo::Protocol
- Inherits:
-
Object
- Object
- Porteo::Protocol
- Defined in:
- lib/protocols/protocol.rb
Overview
Base class to implement common funcionality for all protocols.
In Porteo system, the protocol creates an appropiate gateway to send the message through it.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#gw_config ⇒ Object
Hash with that contain gateway configuration parameters.
-
#receiver ⇒ Object
Who the message is going to be sent to.
Instance Method Summary collapse
-
#initialize(gw_config) ⇒ Protocol
constructor
Creates a new instance of a protocol.
-
#message ⇒ String
The raw message sections hash to be sent.
-
#send_message ⇒ nil
Send the message defined by template and template parameters.
-
#set_template(template, requires) ⇒ nil
Set the template to be used.
-
#set_template_params(param) ⇒ nil
Set the values of template parameters.
Constructor Details
#initialize(gw_config) ⇒ Protocol
Creates a new instance of a protocol
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/protocols/protocol.rb', line 44 def initialize( gw_config ) # Initilization @gw_config = gw_config @param = {} @template = "" @requires = [] @message_sections = [] @receiver = nil end |
Instance Attribute Details
#gw_config ⇒ Object
Hash with that contain gateway configuration parameters
40 41 42 |
# File 'lib/protocols/protocol.rb', line 40 def gw_config @gw_config end |
#receiver ⇒ Object
Who the message is going to be sent to
38 39 40 |
# File 'lib/protocols/protocol.rb', line 38 def receiver @receiver end |
Instance Method Details
#message ⇒ String
As sections can be dynamic (because of ERB preprocessing) this method
The raw message sections hash to be sent. may not show some sections present in a template, depending on the parameters passed.
83 84 85 86 |
# File 'lib/protocols/protocol.rb', line 83 def # Call to expand_template .to_s end |
#send_message ⇒ nil
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.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/protocols/protocol.rb', line 93 def # Expand the template, here we also check if template is well-formatted @message_sections = # As we can define instance variables with highest priority than template # tags, we may want to override those tags for a certain protocol # Check if a well-formatted template contains all fields necessaries # to send this kind of message. 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_template(template, requires) ⇒ nil
Set the template to be used.
63 64 65 66 |
# File 'lib/protocols/protocol.rb', line 63 def set_template( template, requires ) @template = template @requires = requires end |
#set_template_params(param) ⇒ nil
Set the values of template parameters
72 73 74 |
# File 'lib/protocols/protocol.rb', line 72 def set_template_params( param ) @param = param end |