Class: Porteo::Protocol

Inherits:
Object
  • Object
show all
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

Mail_protocol, Sms_protocol, Twitter_protocol

Instance Attribute Summary collapse

Instance Method Summary collapse

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_configObject

Hash with that contain gateway configuration parameters



40
41
42
# File 'lib/protocols/protocol.rb', line 40

def gw_config
  @gw_config
end

#receiverObject

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

#messageString

Note:

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 message
  # Call to expand_template
  expand_template.to_s
end

#send_messagenil

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.

Raises:

  • (ArgumentError)

    If gateway is not defined.



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 send_message
  # Expand the template, here we also check if template is well-formatted
  @message_sections = expand_template

  # As we can define instance variables with highest priority than template 
  # tags, we may want to override those tags for a certain protocol
  override_tags

  # Check if a well-formatted template contains all fields necessaries
  # to send this kind of message.
  check_message_sections
  
  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