porteo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +674 -0
- data/README.markdown +63 -0
- data/VERSION +1 -0
- data/lib/gateways/gateway.rb +126 -0
- data/lib/gateways/mensario_gateway.rb +62 -0
- data/lib/gateways/pony_gateway.rb +97 -0
- data/lib/gateways/twitter_gateway.rb +57 -0
- data/lib/message/message.rb +192 -0
- data/lib/porteo.rb +32 -0
- data/lib/protocols/mail_protocol.rb +64 -0
- data/lib/protocols/protocol.rb +167 -0
- data/lib/protocols/sms_protocol.rb +68 -0
- data/lib/protocols/twitter_protocol.rb +42 -0
- data/porteo.gemspec +76 -0
- metadata +171 -0
data/README.markdown
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Porteo
|
2
|
+
A Ruby gem that send messages via any protocol/gateway with programable templates and emitters.
|
3
|
+
|
4
|
+
# Usage
|
5
|
+
All you have to do is to create a new Message object like this:
|
6
|
+
|
7
|
+
my_msg = Porteo::Message.new()
|
8
|
+
|
9
|
+
my_msg.configure do |m|
|
10
|
+
# set the template named foo
|
11
|
+
# The template must be set in the template path (./config/templates/ by default)
|
12
|
+
m.template = "foo"
|
13
|
+
# set the emitter named bar
|
14
|
+
# The emitter must be set in config path (./config/ by default)
|
15
|
+
# by default get the "default" profile in emitter
|
16
|
+
m.emitter = "bar"
|
17
|
+
# Set the protocol
|
18
|
+
m.protocol = "desired_protocol"
|
19
|
+
end
|
20
|
+
|
21
|
+
Now you only have to set the params on the template (if you required it) like this:
|
22
|
+
|
23
|
+
my_msg.params = { :name_of_the_param => value_of_the_param }
|
24
|
+
|
25
|
+
Or this:
|
26
|
+
|
27
|
+
my_msg.name_of_the_param = value_of_the_param
|
28
|
+
|
29
|
+
# Configuration Files
|
30
|
+
The files that are strictly necesaries are: a emitter, and a template. Their construction are the folloing:
|
31
|
+
|
32
|
+
## Emitter
|
33
|
+
An emitter must be constructed using YAML like this:
|
34
|
+
|
35
|
+
:protocolName:
|
36
|
+
:profileName:
|
37
|
+
:configParam1: :valueParamSymbol
|
38
|
+
:configParamFoo: 'valueParamString'
|
39
|
+
:configParambar: 'etc'
|
40
|
+
:otherProfile:
|
41
|
+
:configParam: :value
|
42
|
+
:bar: 'foo'
|
43
|
+
:otherProtocol:
|
44
|
+
:profileName:
|
45
|
+
:protocolconfigparam: 'value'
|
46
|
+
|
47
|
+
## Template
|
48
|
+
A template file must be constructed using YAML + ERB like this:
|
49
|
+
|
50
|
+
:requires: [:parameterName]
|
51
|
+
:template:
|
52
|
+
:fieldOfTemplate: "This is a Example of a template file, a parameter must be given, its value is: <%= param[:parameterName] %>"
|
53
|
+
|
54
|
+
The template file must be named with "templateName.protocolName", ONLY templateName must be given to the Message class to know which template to use.
|
55
|
+
|
56
|
+
# About
|
57
|
+
Porteo is developed by [NoSoloSoftware](http://nosolosoftware.biz).
|
58
|
+
|
59
|
+
# License
|
60
|
+
Porteo is Copyright 2011 NoSoloSoftware, it is free software.
|
61
|
+
|
62
|
+
Porteo is distributed under GPLv3 license. More details can be found at COPYING file.
|
63
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# Copyright 2011 NoSoloSoftware
|
2
|
+
#
|
3
|
+
# This file is part of Porteo.
|
4
|
+
#
|
5
|
+
# Porteo is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Porteo is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Porteo. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
# Porteo is an integrated message sending service.
|
19
|
+
# It allows you to send messages by various protocols (sms, email, twitter)
|
20
|
+
# using differents gateways (mensario, pony, twitter API). You can also
|
21
|
+
# integrate new protocols and gateways for your favorite messenger
|
22
|
+
# service.
|
23
|
+
module Porteo
|
24
|
+
|
25
|
+
# Base class to implement any gateway needed to connect protocols
|
26
|
+
# and sending service gems.
|
27
|
+
#
|
28
|
+
# It should not be used by itself but creating a child class which
|
29
|
+
# defines specific behavior based on a certain protocol and a gem.
|
30
|
+
class Gateway
|
31
|
+
|
32
|
+
# Set required connection parameters for a specific gateway.
|
33
|
+
# This class method creates a new instance method which overwritte
|
34
|
+
# connection_argument.
|
35
|
+
# @param [Array] argument Required connection arguments.
|
36
|
+
# @return [nil]
|
37
|
+
def self.connection_argument( *argument )
|
38
|
+
define_method( :connection_arguments ) do
|
39
|
+
argument
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Required connection parameters.
|
44
|
+
# Its define which fields have to be present in order to
|
45
|
+
# send a valid message.
|
46
|
+
# This method is overwritten dynamically when a child gateway
|
47
|
+
# class is created and self.connection_arguments is called.
|
48
|
+
# @return [Array] Required connection parameters.
|
49
|
+
def connection_arguments
|
50
|
+
[]
|
51
|
+
end
|
52
|
+
|
53
|
+
# Create a new instance of a gateway.
|
54
|
+
# @param [Hash] gw_config Configuration options. This options
|
55
|
+
# set the sending parameters not the content of the message.
|
56
|
+
def initialize( gw_config = {} )
|
57
|
+
@config = gw_config
|
58
|
+
end
|
59
|
+
|
60
|
+
# @abstract
|
61
|
+
# Send a message defined in parameter
|
62
|
+
# @param [Hash] message_sections Message content.
|
63
|
+
# @return [nil]
|
64
|
+
# @raise [Exception] This method is not meant to be call but to be overwritten.
|
65
|
+
# @note This method has to be overwritten.
|
66
|
+
def send_message( message_sections )
|
67
|
+
raise Exception, "Gateway Error. This method has to be overwritten. You are trying to send a message with a generic gateway."
|
68
|
+
end
|
69
|
+
|
70
|
+
# Initialize the send message process.
|
71
|
+
# Before sending the message it check if all required params are present.
|
72
|
+
# @param [Hash] message Message sections to be sent.
|
73
|
+
# @return [nil]
|
74
|
+
def init_send( message )
|
75
|
+
send_message_hook( message )
|
76
|
+
end
|
77
|
+
|
78
|
+
# PRIVATE METHODS
|
79
|
+
private
|
80
|
+
|
81
|
+
# Defines send message process.
|
82
|
+
# It first look for all required connection arguments
|
83
|
+
# and then it send the message.
|
84
|
+
# @param [Hash] message Message section to be sent.
|
85
|
+
def send_message_hook( message )
|
86
|
+
check_connection_arguments
|
87
|
+
send_message( message )
|
88
|
+
end
|
89
|
+
|
90
|
+
# Looks for a key in a specified hash.
|
91
|
+
# If an argument is not present, an ArgumentError is raised.
|
92
|
+
# @param [Hash] config Configuration hash.
|
93
|
+
# @param [Symbol] argument Key that should be contain by hash.
|
94
|
+
# @return [nil]
|
95
|
+
# @raise [ArgumentError] If the argument is not present in configuration hash.
|
96
|
+
def check_argument( config, argument )
|
97
|
+
raise ArgumentError, "Gateway Error. Too few arguments to connect." unless config[argument] != nil
|
98
|
+
end
|
99
|
+
|
100
|
+
# Checks the required connection parameters to any gateway.
|
101
|
+
# Those parameters are defined by class method connection_argument.
|
102
|
+
# @return [nil]
|
103
|
+
def check_connection_arguments
|
104
|
+
# Iterate over arguments
|
105
|
+
connection_arguments.each do |argument|
|
106
|
+
|
107
|
+
# If argument is a hash, we need to search for the parameter
|
108
|
+
# in a sublevel of config. That sublevel is defined by the hash
|
109
|
+
# key so there should be only one key. The value of that key is an
|
110
|
+
# array containing the sublevel arguments.
|
111
|
+
if argument.class == Hash
|
112
|
+
argument.values[0].each do |value|
|
113
|
+
|
114
|
+
# Once we know where and what look for, we do the check
|
115
|
+
check_argument( @config[argument.keys[0]], value )
|
116
|
+
end
|
117
|
+
else
|
118
|
+
|
119
|
+
# Check the argument in top level configuration array.
|
120
|
+
check_argument( @config, argument )
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Copyright 2011 NoSoloSoftware
|
2
|
+
#
|
3
|
+
# This file is part of Porteo.
|
4
|
+
#
|
5
|
+
# Porteo is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Porteo is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Porteo. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'mensario'
|
19
|
+
require 'gateways/gateway'
|
20
|
+
|
21
|
+
# Porteo is an integrated message sending service.
|
22
|
+
# It allows you to send messages by various protocols (sms, email, twitter)
|
23
|
+
# using differents gateways (mensario, pony, twitter API). You can also
|
24
|
+
# integrate new protocols and gateways for your favorite messenger
|
25
|
+
# service.
|
26
|
+
module Porteo
|
27
|
+
|
28
|
+
# Gateway to use Mensario service
|
29
|
+
# In Porteo this class acts as a link between SMS protocol
|
30
|
+
# and Mensario gem
|
31
|
+
#
|
32
|
+
# This class inherits from Gateway and just overwrite
|
33
|
+
# the send_message method
|
34
|
+
class Mensario_gateway < Gateway
|
35
|
+
|
36
|
+
connection_argument :license,
|
37
|
+
:password,
|
38
|
+
:username
|
39
|
+
|
40
|
+
# Send the SMS using Mensario gem
|
41
|
+
# @param [Hash] msg The message sections to send
|
42
|
+
# @return [nil]
|
43
|
+
def send_message( msg )
|
44
|
+
|
45
|
+
Mensario.set_config do
|
46
|
+
Mensario.license( @config[:license] )
|
47
|
+
Mensario.username( @config[:username] )
|
48
|
+
Mensario.password( @config[:password] )
|
49
|
+
end
|
50
|
+
Mensario.send_message( {:text => msg[:text],
|
51
|
+
:sender => msg[:sender],
|
52
|
+
:code => msg[:code],
|
53
|
+
:phone => msg[:phone],
|
54
|
+
:timezone => msg[:timezone],
|
55
|
+
:date => msg[:date] }
|
56
|
+
)
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# Copyright 2011 NoSoloSoftware
|
2
|
+
#
|
3
|
+
# This file is part of Porteo.
|
4
|
+
#
|
5
|
+
# Porteo is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Porteo is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Porteo. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'pony'
|
19
|
+
require 'gateways/gateway'
|
20
|
+
|
21
|
+
# Porteo is an integrated message sending service.
|
22
|
+
# It allows you to send messages by various protocols (sms, email, twitter)
|
23
|
+
# using differents gateways (mensario, pony, twitter API). You can also
|
24
|
+
# integrate new protocols and gateways for your favorite messenger
|
25
|
+
# service.
|
26
|
+
module Porteo
|
27
|
+
|
28
|
+
# Gateway to use an email service with Pony gems.
|
29
|
+
# In Porteo system, this class acts as a link between any email
|
30
|
+
# protocol and Pony gem.
|
31
|
+
#
|
32
|
+
# This class inherits from Gateway class and just overwrite
|
33
|
+
# the method send_message.
|
34
|
+
class Pony_gateway < Gateway
|
35
|
+
|
36
|
+
connection_argument :via_options => [:address, :port, :user_name, :password]
|
37
|
+
|
38
|
+
# Options allowed for Pony API.
|
39
|
+
PONY_OPTIONS = [
|
40
|
+
:to,
|
41
|
+
:cc,
|
42
|
+
:bcc,
|
43
|
+
:from,
|
44
|
+
:body,
|
45
|
+
:html_body,
|
46
|
+
:subject,
|
47
|
+
:charset,
|
48
|
+
:attachments,
|
49
|
+
:headers,
|
50
|
+
:message_id,
|
51
|
+
:sender
|
52
|
+
]
|
53
|
+
|
54
|
+
# Via configuration options.
|
55
|
+
# The sending method is configured with these options.
|
56
|
+
VIA_OPTIONS = [
|
57
|
+
:address,
|
58
|
+
:port,
|
59
|
+
:user_name,
|
60
|
+
:password,
|
61
|
+
:enable_starttls_auto,
|
62
|
+
:authentication,
|
63
|
+
:domain,
|
64
|
+
:location,
|
65
|
+
:argument
|
66
|
+
]
|
67
|
+
|
68
|
+
# Send the message defined in parameter.
|
69
|
+
# @param [Hash] message_sections Differents parts of message. Allowed keys
|
70
|
+
# are defined in PONY_OPTIONS.
|
71
|
+
# @return [nil]
|
72
|
+
def send_message( message_sections )
|
73
|
+
# Create options hash to Pony
|
74
|
+
pony_config = {}
|
75
|
+
|
76
|
+
# Recover data from template
|
77
|
+
# We look for each option defined before in the message content
|
78
|
+
PONY_OPTIONS.each do |opt|
|
79
|
+
pony_config[opt] = message_sections[opt] if message_sections[opt] != nil
|
80
|
+
end
|
81
|
+
|
82
|
+
# Recover data from send options
|
83
|
+
# First we get the via used to send the message
|
84
|
+
pony_config[:via] = @config[:via]
|
85
|
+
|
86
|
+
# Then we look for the other configuration options
|
87
|
+
pony_config[:via_options] = {}
|
88
|
+
VIA_OPTIONS.each do |opt|
|
89
|
+
pony_config[:via_options][opt] = @config[:via_options][opt] if @config[:via_options][opt] != nil
|
90
|
+
end
|
91
|
+
|
92
|
+
# Send the message
|
93
|
+
Pony.mail( pony_config )
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Copyright 2011 NoSoloSoftware
|
2
|
+
#
|
3
|
+
# This file is part of Porteo.
|
4
|
+
#
|
5
|
+
# Porteo is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Porteo is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Porteo. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'twitter'
|
19
|
+
require 'gateways/gateway'
|
20
|
+
|
21
|
+
# Porteo is an integrated message sending service.
|
22
|
+
# It allows you to send messages by various protocols (sms, email, twitter)
|
23
|
+
# using differents gateways (mensario, pony, twitter API). You can also
|
24
|
+
# integrate new protocols and gateways for your favorite messenger
|
25
|
+
# service.
|
26
|
+
module Porteo
|
27
|
+
|
28
|
+
# Gateway to use Twitter service with Twitter gem.
|
29
|
+
# In Porteo this class acts as a link between any twitter protocol
|
30
|
+
# and Twitter gem.
|
31
|
+
#
|
32
|
+
# This class inherits from Gateway and just overwrite
|
33
|
+
# the send_message method.
|
34
|
+
class Twitter_gateway < Gateway
|
35
|
+
|
36
|
+
connection_argument :consumer_key,
|
37
|
+
:consumer_secret,
|
38
|
+
:oauth_token,
|
39
|
+
:oauth_token_secret
|
40
|
+
|
41
|
+
# Send the twitt using Twitter gem.
|
42
|
+
# @param [Hash] msg The message sections to send
|
43
|
+
# @return [nil]
|
44
|
+
def send_message( msg )
|
45
|
+
Twitter.configure do |config|
|
46
|
+
config.consumer_key = @config[:consumer_key]
|
47
|
+
config.consumer_secret = @config[:consumer_secret]
|
48
|
+
config.oauth_token = @config[:oauth_token]
|
49
|
+
config.oauth_token_secret = @config[:oauth_token_secret]
|
50
|
+
end
|
51
|
+
|
52
|
+
Twitter.update( msg[:body] )
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,192 @@
|
|
1
|
+
# Copyright 2011 NoSoloSoftware
|
2
|
+
#
|
3
|
+
# This file is part of Porteo.
|
4
|
+
#
|
5
|
+
# Porteo is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Porteo is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Porteo. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'yaml'
|
19
|
+
|
20
|
+
require 'protocols/twitter_protocol'
|
21
|
+
require 'protocols/mail_protocol'
|
22
|
+
require 'protocols/sms_protocol'
|
23
|
+
|
24
|
+
# Porteo is an integrated message sending service.
|
25
|
+
# It allows you to send messages by various protocols (sms, email, twitter)
|
26
|
+
# using differents gateways (mensario, pony, twitter API). You can also
|
27
|
+
# integrate new protocols and gateways for your favorite messenger
|
28
|
+
# service.
|
29
|
+
module Porteo
|
30
|
+
|
31
|
+
# A message which will be send by any protocol and gateway.
|
32
|
+
#
|
33
|
+
# The content of a message will be defined in a template, a file
|
34
|
+
# that contain differents sections each being one part of the message.
|
35
|
+
# This templates will be processed with ERB so its can contain ruby
|
36
|
+
# code to get more flexibility.
|
37
|
+
#
|
38
|
+
# The configuration options (for protocols and gateways) it set trought
|
39
|
+
# emitter files, special files in YAML format.
|
40
|
+
class Message
|
41
|
+
# A hash containing message sections defined in the template.
|
42
|
+
attr_reader :template_content
|
43
|
+
# An array containing required fields to define the template.
|
44
|
+
attr_reader :template_requires
|
45
|
+
|
46
|
+
# The name of the protocol used to send the message.
|
47
|
+
attr_accessor :protocol
|
48
|
+
# Name of template used to send the message.
|
49
|
+
attr_accessor :template
|
50
|
+
# Parameters to set the fields defined in the template.
|
51
|
+
attr_accessor :template_params
|
52
|
+
# Path to configuration directory. It have to end in a slash.
|
53
|
+
attr_accessor :config_path
|
54
|
+
# Path to templates directory. It have to end in a slash.
|
55
|
+
attr_accessor :template_path
|
56
|
+
# The one who should receive the message.
|
57
|
+
attr_accessor :receiver
|
58
|
+
# Profile used to recover the gateway configuration.
|
59
|
+
attr_accessor :profile
|
60
|
+
# File used to load the configuration information.
|
61
|
+
attr_accessor :emitter
|
62
|
+
|
63
|
+
|
64
|
+
# Creates a new message.
|
65
|
+
# @param [String] emitter File used to load the configuration information.
|
66
|
+
# @param [String] protocol Protocol to be used (mail, sms, twitter).
|
67
|
+
# @param [String] profile Profile to load gateway information.
|
68
|
+
# @param [Hash] opts Options.
|
69
|
+
# @option opts :config_path ("./config/") Configuration path.
|
70
|
+
# @option opts :template_path ("./config/templates/") Templates path.
|
71
|
+
def initialize( emitter = "", protocol = "", profile = "default", template = "", opts = {} )
|
72
|
+
# config_path value should end in a trailing slash
|
73
|
+
opts[:config_path] ||= CONFIG_ROOT
|
74
|
+
@config_path = opts[:config_path]
|
75
|
+
|
76
|
+
# template_path value should end in a trailing slash
|
77
|
+
opts[:template_path] ||= TEMPLATES_ROOT
|
78
|
+
@template_path = opts[:template_path]
|
79
|
+
|
80
|
+
# Instance variables initilization
|
81
|
+
@template = template
|
82
|
+
@template_params = {}
|
83
|
+
@template_content = ""
|
84
|
+
@template_requires = []
|
85
|
+
|
86
|
+
@receiver = nil
|
87
|
+
|
88
|
+
# Assign instance variables
|
89
|
+
@emitter = emitter
|
90
|
+
@profile = profile
|
91
|
+
@protocol = protocol
|
92
|
+
end
|
93
|
+
|
94
|
+
# Convenience method to allow configuration options to be set in a block.
|
95
|
+
# @return [nil]
|
96
|
+
def configure
|
97
|
+
yield self
|
98
|
+
end
|
99
|
+
|
100
|
+
# Assign values to fields defined in the template.
|
101
|
+
# Overwrite all params set before.
|
102
|
+
# @param [Hash] params The keys are the fields defined in the
|
103
|
+
# template which will be set to the hash value.
|
104
|
+
# @return [nil]
|
105
|
+
def set_template_params( params )
|
106
|
+
@template_params = params
|
107
|
+
end
|
108
|
+
|
109
|
+
# Send a message using protocol, content and configuration set before.
|
110
|
+
# @return [nil]
|
111
|
+
# @raise [ArgumentError] If emitter file is not valid or if protocol is not defined.
|
112
|
+
def send_message
|
113
|
+
load_template( @template )
|
114
|
+
|
115
|
+
# Load configuration information for the gateway
|
116
|
+
begin
|
117
|
+
config = YAML.load_file( "#{@config_path}#{@emitter}.emitter" )
|
118
|
+
rescue Errno::ENOENT
|
119
|
+
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."
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
raise ArgumentError, "Message Error. Profile '#{@profile}' not found." unless config[@protocol.to_sym][@profile.to_sym]
|
124
|
+
|
125
|
+
begin
|
126
|
+
# Creates a new instance of defined protocol
|
127
|
+
@protocol_obj = Porteo.const_get( "#{@protocol}_protocol".capitalize.to_sym ).new( config[@protocol.to_sym][@profile.to_sym] )
|
128
|
+
rescue NameError
|
129
|
+
raise ArgumentError, "Message Error. Undefined protocol. Check if '#{@protocol}_protocol.rb' is created and is valid."
|
130
|
+
end
|
131
|
+
|
132
|
+
# Set template values
|
133
|
+
@protocol_obj.set_template( @template_content, @template_requires )
|
134
|
+
@protocol_obj.set_template_params( @template_params )
|
135
|
+
|
136
|
+
# Set receiver
|
137
|
+
@protocol_obj.receiver = @receiver
|
138
|
+
|
139
|
+
# Send the message
|
140
|
+
@protocol_obj.send_message
|
141
|
+
end
|
142
|
+
|
143
|
+
# Method to see the complete message by sections, once it has been sent.
|
144
|
+
# @return [String] the message sections
|
145
|
+
def show_message
|
146
|
+
@protocol_obj.message unless @protocol_obj == nil
|
147
|
+
end
|
148
|
+
|
149
|
+
# Method missing is used to allow set params one by one.
|
150
|
+
# @param [Symbol] method param to be set.
|
151
|
+
# @param [Array] params params in the call.
|
152
|
+
# @param [Block] block block code in method.
|
153
|
+
def method_missing( method, *params, &block )
|
154
|
+
# We only allow one param to be passed
|
155
|
+
# so we check that only one param is passed
|
156
|
+
# and block is nil
|
157
|
+
# We want to use the prefered configuration style
|
158
|
+
# so we expect to use a call like this:
|
159
|
+
# my_obj.method_name = value
|
160
|
+
if method[-1] == "=" and params.size == 1 and block == nil
|
161
|
+
@template_params[method.to_s.chop.to_sym] = params[0]
|
162
|
+
else
|
163
|
+
super( method, params, block )
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
private
|
168
|
+
# Recover the information defined in a template file. The template is
|
169
|
+
# defined using YAML. It has the format of a hash, containing a key named
|
170
|
+
# requires, which value is an array of required fields for the template,
|
171
|
+
# and a key named template which contain the template itself.
|
172
|
+
# @param [String] template Template name to be used. Various templates may
|
173
|
+
# have the same name (alert.mail, alert.sms, alert.twitter) but the one
|
174
|
+
# named as protocol used will be used. So template name it just the first
|
175
|
+
# part of the template filename.
|
176
|
+
# @return [nil]
|
177
|
+
def load_template( template )
|
178
|
+
begin
|
179
|
+
content = YAML.load_file( "#{@template_path}#{template}.#{@protocol}" )
|
180
|
+
rescue Errno::ENOENT
|
181
|
+
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."
|
182
|
+
end
|
183
|
+
|
184
|
+
if( content )
|
185
|
+
@template_content = content[:template].to_s
|
186
|
+
@template_requires = content[:requires] if content[:requires] != nil
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|
data/lib/porteo.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright 2011 NoSoloSoftware
|
2
|
+
#
|
3
|
+
# This file is part of Porteo.
|
4
|
+
#
|
5
|
+
# Porteo is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Porteo is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Porteo. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'message/message'
|
19
|
+
|
20
|
+
# Porteo is an integrated message sending service.
|
21
|
+
# It allows you to send messages by various protocols (sms, email, twitter)
|
22
|
+
# using differents gateways (mensario, pony, twitter API). You can also
|
23
|
+
# integrate new protocols and gateways for your favorite messenger
|
24
|
+
# service.
|
25
|
+
module Porteo
|
26
|
+
# Default configuration path
|
27
|
+
CONFIG_ROOT = "./config/"
|
28
|
+
# Default templates path
|
29
|
+
TEMPLATES_ROOT = "./config/templates/"
|
30
|
+
# Default locales path
|
31
|
+
LOCALES_ROOT = "./config/locales/"
|
32
|
+
end
|