porteo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,64 @@
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 'protocols/protocol'
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
+
27
+ # Implementation of email protocol to be used in Porteo system.
28
+ # It only define specific behavior for this protocol.
29
+ class Mail_protocol < Protocol
30
+
31
+ # Necessary fields to send a valid email
32
+ MAIL_REQUIRED_FIELDS = [:to, :body]
33
+
34
+ # Implementates the parent method to ensure that email receptor is
35
+ # the one set at receiver instance variable not the :to template tag
36
+ # @return [nil]
37
+ def override_tags
38
+ # In mail protocol, the receiver instance variable has precedence over
39
+ # :to tag in template
40
+ @message_sections[:to] = @receiver unless @receiver == nil
41
+ end
42
+
43
+ # Check for all required field to exist and contain valid values.
44
+ # If any required field is missing or its syntax is not valid
45
+ # an ArgumentException is raised.
46
+ # @return [nil]
47
+ # @raise [ArgumentError] When no template sections are present
48
+ # or no required parameter is given.
49
+ def check_message_sections
50
+ raise ArgumentError, "Protocol Error. There are no template sections. Maybe you didn't load a complete template" unless @message_sections != nil
51
+
52
+ # Check for required fields
53
+ MAIL_REQUIRED_FIELDS.each do |field|
54
+ raise ArgumentError, "Protocol Error. #{field.to_s.capitalize} is a required field for this protocol and it was not defined" unless @message_sections[field] != nil
55
+ end
56
+
57
+ # Check for correct syntax
58
+ if not @message_sections[:to] =~ /[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/
59
+ raise ArgumentError, "Protocol Error. Bad syntax in :to section"
60
+ end
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,167 @@
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 'erb'
19
+
20
+ require 'gateways/twitter_gateway'
21
+ require 'gateways/pony_gateway'
22
+ require 'gateways/mensario_gateway'
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
+ # Base class to implement common funcionality for all protocols.
32
+ #
33
+ # In Porteo system, the protocol creates an appropiate gateway to
34
+ # send the message through it.
35
+ class Protocol
36
+
37
+ # Who the message is going to be sent to
38
+ attr_accessor :receiver
39
+ # Hash with that contain gateway configuration parameters
40
+ attr_accessor :gw_config
41
+
42
+ # Creates a new instance of a protocol
43
+ # @param [Hash] Gateway configuration parameters.
44
+ def initialize( gw_config )
45
+ # Initilization
46
+ @gw_config = gw_config
47
+
48
+ @param = {}
49
+ @template = ""
50
+ @requires = []
51
+
52
+ @message_sections = []
53
+
54
+ @receiver = nil
55
+ end
56
+
57
+ # Set the template to be used.
58
+ # @param [Hash] template The template per se.
59
+ # @param [Array] requires The requires of the template. Which is the
60
+ # required fields for this template. Those required fields has to be
61
+ # present in the template parameters or an error will be raised.
62
+ # @return [nil]
63
+ def set_template( template, requires )
64
+ @template = template
65
+ @requires = requires
66
+ end
67
+
68
+ # Set the values of template parameters
69
+ # @param [Hash] param Pairs of required fields for the template and
70
+ # the value its take.
71
+ # @return [nil]
72
+ def set_template_params( param )
73
+ @param = param
74
+ end
75
+
76
+ # The raw message sections hash to be sent.
77
+ # @return [String] Message sections which will be sent. This method
78
+ # returns the message as a string containing every section of the message
79
+ # already parsed.
80
+ # @note As sections can be dynamic (because of ERB preprocessing) this method
81
+ # may not show some sections present in a template, depending on the parameters
82
+ # passed.
83
+ def message
84
+ # Call to expand_template
85
+ expand_template.to_s
86
+ end
87
+
88
+ # Send the message defined by template and template parameters. It used
89
+ # the gateway configuration options to send the message through a third-party
90
+ # ruby gem.
91
+ # @return [nil]
92
+ # @raise [ArgumentError] If gateway is not defined.
93
+ def send_message
94
+ # Expand the template, here we also check if template is well-formatted
95
+ @message_sections = expand_template
96
+
97
+ # As we can define instance variables with highest priority than template
98
+ # tags, we may want to override those tags for a certain protocol
99
+ override_tags
100
+
101
+ # Check if a well-formatted template contains all fields necessaries
102
+ # to send this kind of message.
103
+ check_message_sections
104
+
105
+ begin
106
+ # Create the appropiate gateway, which is defined in gw_config
107
+ @gateway = Porteo.const_get( "#{@gw_config[:gateway]}_gateway".capitalize.to_sym ).new( @gw_config )
108
+ rescue NameError
109
+ raise ArgumentError, "Protocol Error. Undefined gateway. Check if '#{@gw_config[:gateway]}_gateway.rb' is created and is a valid gateway"
110
+ end
111
+ # Send the message
112
+ @gateway.init_send( @message_sections )
113
+ end
114
+
115
+
116
+ # Private methods
117
+ private
118
+
119
+ # @abstract
120
+ # Check for all required sections to be defined.
121
+ # @return [nil]
122
+ # @raise [Exception] This method is not meant to be call but to be overwritten.
123
+ # @note This method has to be overwritten.
124
+ def check_message_sections
125
+ raise Exception, "Protocol Error. This method has to be overwritten. You are trying to check for required sections in a generic protocol."
126
+ end
127
+
128
+ # @abstract
129
+ # Allow programmers to change the order precedence.
130
+ # @note If you want to change the value of any template tag, you should
131
+ # do it here.
132
+ def override_tags
133
+ end
134
+
135
+ # Expand the message from template and variables.
136
+ # A template is a collection of sections which define the content
137
+ # of a message, but those sections are dynamic thanks to the use
138
+ # of ERB code. At the same time, the parameters are filled with
139
+ # it value.
140
+ # @return [nil]
141
+ # @raise [ArgumentError] If a required param is not present or it is nil.
142
+ def expand_template
143
+ # Check for existence of required parameters
144
+ @requires.each do |required_param|
145
+ raise ArgumentError, "Protocol Error. Required parameter '#{required_param.to_s}' is not present or it is nil." if @param[required_param] == nil
146
+ end
147
+
148
+ param = @param
149
+
150
+ erb_template = ERB.new( @template, 0, "%<>" )
151
+
152
+ # Binding get the execution context to allow the use of
153
+ # param in the template
154
+ template_filled = erb_template.result( binding )
155
+
156
+ # We use eval to get a Hash with message sections because
157
+ # erb return a string
158
+ # [:body] => "..."
159
+ # [:attachments] => "..."
160
+ # ...
161
+ eval( template_filled )
162
+ end
163
+ end
164
+
165
+ end
166
+
167
+
@@ -0,0 +1,68 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2011 NoSoloSoftware
4
+ #
5
+ # This file is part of Porteo.
6
+ #
7
+ # Porteo is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # Porteo is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Porteo. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'protocols/protocol'
21
+
22
+ # Porteo is an integrated message sending service.
23
+ # It allows you to send messages by various protocols (sms, email, twitter)
24
+ # using differents gateways (mensario, pony, twitter API). You can also
25
+ # integrate new protocols and gateways for your favorite messenger
26
+ # service.
27
+ module Porteo
28
+
29
+ # Implementation of SMS protocol to be used in Porteo system.
30
+ # It only define specific behavior for this protocol.
31
+ class Sms_protocol < Protocol
32
+
33
+ # Check for the required fields to exists.
34
+ # @return [nil]
35
+ # @raise [ArgumentError] if message cannot be sent.
36
+ def check_message_sections
37
+ raise ArgumentError, "Protocol Error. There are not text section" if @message_sections[:text] == nil
38
+ # The sms must match GSM alphabet, double counting these chars
39
+ # [ , ], <FF>, ^, \, {, }, ~, | and €
40
+ count = 0
41
+
42
+ @message_sections[:text].each_char do | c |
43
+ count = count + 1 if c =~ /[\[\]\^\\\{\}\~\|€]/
44
+
45
+ count = count + 1
46
+ end
47
+
48
+ raise ArgumentError, "Protocol Error. The message is too long" if count > 160
49
+
50
+ raise ArgumentError, "Protocol Error. The phone number is invalid" unless @message_sections[:phone] =~ /^\d{9}$/
51
+
52
+ raise ArgumentError, "Protocol Error. The country phone code is invalid" unless @message_sections[:code] =~ /^\d{2,4}$/
53
+
54
+ raise ArgumentError, "Protocol Error. The sender is invalid" unless @message_sections[:sender] =~ /^[A-Za-z0-9]{1,11}$/
55
+
56
+ end
57
+
58
+ # Implementates the parent method to ensure that sms receptor is
59
+ # the one set at receiver instance variable not the :phone template tag
60
+ # @return [nil]
61
+ def override_tags
62
+ # In sms protocol, the receiver instance variable has precedence over
63
+ # :phone tag in template
64
+ @message_sections[:phone] = @receiver unless @receiver == nil
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,42 @@
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 'protocols/protocol'
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
+
27
+ # Implementation of Twitter protocol to be used in Porteo system.
28
+ # It only define specific behavior for this protocol.
29
+ class Twitter_protocol < Protocol
30
+ # Check for the required fields to exists.
31
+ # @return [nil]
32
+ # @raise [ArgumentError] if message cannot be sent.
33
+ def check_message_sections
34
+ raise ArgumentError, "Protocol Error. There are no body section" if @message_sections[:body] == nil
35
+ # the twitt must be shorter than 140 chars.
36
+ raise ArgumentError, "Protocol Error. The message is too long" if @message_sections[:body].length > 140
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+
data/porteo.gemspec ADDED
@@ -0,0 +1,76 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{porteo}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Rafael Garcia}, %q{Luis Ciudad}]
12
+ s.date = %q{2011-06-08}
13
+ s.description = %q{Send all messages that you want in any protocol, its ready to send mail messages, twitts and sms with the apropiate gateway}
14
+ s.email = [%q{rgarcia@nosolosoftware.biz}, %q{lciudad@nosolosoftware.biz}]
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "COPYING",
20
+ "README.markdown",
21
+ "VERSION",
22
+ "lib/gateways/gateway.rb",
23
+ "lib/gateways/mensario_gateway.rb",
24
+ "lib/gateways/pony_gateway.rb",
25
+ "lib/gateways/twitter_gateway.rb",
26
+ "lib/message/message.rb",
27
+ "lib/porteo.rb",
28
+ "lib/protocols/mail_protocol.rb",
29
+ "lib/protocols/protocol.rb",
30
+ "lib/protocols/sms_protocol.rb",
31
+ "lib/protocols/twitter_protocol.rb",
32
+ "porteo.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/nosolosoftware/porteo}
35
+ s.licenses = [%q{GPL-3}]
36
+ s.require_paths = [%q{lib}]
37
+ s.rubygems_version = %q{1.8.5}
38
+ s.summary = %q{A Ruby gem for sending all kind of messages}
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<twitter>, ["~> 1.5.0"])
45
+ s.add_runtime_dependency(%q<pony>, ["~> 1.2"])
46
+ s.add_runtime_dependency(%q<mensario>, ["~> 0.2.0"])
47
+ s.add_development_dependency(%q<rake>, ["~> 0.9.2"])
48
+ s.add_development_dependency(%q<yard>, ["~> 0.7.1"])
49
+ s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
50
+ s.add_development_dependency(%q<cucumber>, ["~> 0.10.5"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
52
+ s.add_development_dependency(%q<bluecloth>, ["~> 2.1.0"])
53
+ else
54
+ s.add_dependency(%q<twitter>, ["~> 1.5.0"])
55
+ s.add_dependency(%q<pony>, ["~> 1.2"])
56
+ s.add_dependency(%q<mensario>, ["~> 0.2.0"])
57
+ s.add_dependency(%q<rake>, ["~> 0.9.2"])
58
+ s.add_dependency(%q<yard>, ["~> 0.7.1"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
60
+ s.add_dependency(%q<cucumber>, ["~> 0.10.5"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
62
+ s.add_dependency(%q<bluecloth>, ["~> 2.1.0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<twitter>, ["~> 1.5.0"])
66
+ s.add_dependency(%q<pony>, ["~> 1.2"])
67
+ s.add_dependency(%q<mensario>, ["~> 0.2.0"])
68
+ s.add_dependency(%q<rake>, ["~> 0.9.2"])
69
+ s.add_dependency(%q<yard>, ["~> 0.7.1"])
70
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
71
+ s.add_dependency(%q<cucumber>, ["~> 0.10.5"])
72
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
73
+ s.add_dependency(%q<bluecloth>, ["~> 2.1.0"])
74
+ end
75
+ end
76
+
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: porteo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Rafael Garcia
9
+ - Luis Ciudad
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-06-08 00:00:00 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: twitter
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.5.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: pony
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "1.2"
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: mensario
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.2.0
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 0.9.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: yard
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 0.7.1
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 2.6.0
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: cucumber
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.5
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: jeweler
95
+ requirement: &id008 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ~>
99
+ - !ruby/object:Gem::Version
100
+ version: 1.6.0
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: *id008
104
+ - !ruby/object:Gem::Dependency
105
+ name: bluecloth
106
+ requirement: &id009 !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ~>
110
+ - !ruby/object:Gem::Version
111
+ version: 2.1.0
112
+ type: :development
113
+ prerelease: false
114
+ version_requirements: *id009
115
+ description: Send all messages that you want in any protocol, its ready to send mail messages, twitts and sms with the apropiate gateway
116
+ email:
117
+ - rgarcia@nosolosoftware.biz
118
+ - lciudad@nosolosoftware.biz
119
+ executables: []
120
+
121
+ extensions: []
122
+
123
+ extra_rdoc_files:
124
+ - README.markdown
125
+ files:
126
+ - COPYING
127
+ - README.markdown
128
+ - VERSION
129
+ - lib/gateways/gateway.rb
130
+ - lib/gateways/mensario_gateway.rb
131
+ - lib/gateways/pony_gateway.rb
132
+ - lib/gateways/twitter_gateway.rb
133
+ - lib/message/message.rb
134
+ - lib/porteo.rb
135
+ - lib/protocols/mail_protocol.rb
136
+ - lib/protocols/protocol.rb
137
+ - lib/protocols/sms_protocol.rb
138
+ - lib/protocols/twitter_protocol.rb
139
+ - porteo.gemspec
140
+ homepage: http://github.com/nosolosoftware/porteo
141
+ licenses:
142
+ - GPL-3
143
+ post_install_message:
144
+ rdoc_options: []
145
+
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ hash: -587049560380440763
154
+ segments:
155
+ - 0
156
+ version: "0"
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ none: false
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: "0"
163
+ requirements: []
164
+
165
+ rubyforge_project:
166
+ rubygems_version: 1.8.5
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: A Ruby gem for sending all kind of messages
170
+ test_files: []
171
+