formprocessor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/formprocessor.rb +53 -0
- data/lib/mailer.rb +27 -0
- data/lib/required.rb +25 -0
- metadata +52 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'net/smtp'
|
2
|
+
require_relative 'required'
|
3
|
+
require_relative 'mailer'
|
4
|
+
# Process web forms based on the "values" Hash (typically cgi.params).
|
5
|
+
# = Example Usage
|
6
|
+
# #!/usr/bin/ruby
|
7
|
+
# require 'cgi'
|
8
|
+
# require 'formprocessor'
|
9
|
+
# cgi = CGI.new
|
10
|
+
# puts cgi.header
|
11
|
+
# puts FormProcessor::Main(cgi.params).result
|
12
|
+
# = Keywords
|
13
|
+
# * _required_
|
14
|
+
# A CSV string. Check for the existence of these form fields.
|
15
|
+
# * _mailto_
|
16
|
+
# If successful submission, mail results to this address. A valid SMTP server running on localhost is required.
|
17
|
+
# * _redirect_
|
18
|
+
# Redirect to this URL upon successful submission.
|
19
|
+
# = Return Values & Templating
|
20
|
+
# It is possible to template the results of the form submission by omitting the redirection key/value pair in the values Hash.
|
21
|
+
# If there are no errors, result will return nil. If there are errors, result will return a String containing those errors.
|
22
|
+
module FormProcessor
|
23
|
+
# The gem's logic handler.
|
24
|
+
class Main
|
25
|
+
# Result of the form submission after processing. Contains error String or nil if no errors.
|
26
|
+
attr_reader :result
|
27
|
+
# Expects a Hash such as _cgi.params_
|
28
|
+
def initialize(values)
|
29
|
+
@values = values
|
30
|
+
@result = String.new
|
31
|
+
@email_output = String.new
|
32
|
+
setup
|
33
|
+
run
|
34
|
+
end
|
35
|
+
private
|
36
|
+
def setup
|
37
|
+
@values.each do |key,val|
|
38
|
+
if val.is_a? Array then temp = val.join(', '); val, @values[key] = temp, temp end
|
39
|
+
temp = val.gsub(%r{</?[^>]+?>},'').strip; val, @values[key] = temp, temp
|
40
|
+
@email_output += key.capitalize + ': ' + val + '<br />'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
def run
|
44
|
+
if @values.has_key? 'required' then @result = Required.new(@values).result end
|
45
|
+
if @values.has_key? 'mailto' and @result.length == 0 then Mailer.new(@values,@email_output) end
|
46
|
+
if @values.has_key? 'redirect' and @result.length == 0
|
47
|
+
puts '<meta http-equiv="refresh" content="0;url=' + @values['redirect'] + '">'
|
48
|
+
exit
|
49
|
+
end
|
50
|
+
if @result.length == 0 then @result = nil end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/mailer.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module FormProcessor
|
2
|
+
# Sends an email with form results to the address specified in "mailto".
|
3
|
+
# A valid SMTP server running on localhost is required.
|
4
|
+
class Mailer
|
5
|
+
# Expects a Hash value and String for the email body passed from Main.
|
6
|
+
def initialize(values,output)
|
7
|
+
@values = values
|
8
|
+
@output = output
|
9
|
+
run
|
10
|
+
end
|
11
|
+
private
|
12
|
+
def run
|
13
|
+
msg = <<END_OF_MESSAGE
|
14
|
+
From: Automated Form Mailer <#{@values['mailto']}>
|
15
|
+
To: <#{@values['mailto']}>
|
16
|
+
Content-type: text/html
|
17
|
+
Subject: #{@values['subject']}
|
18
|
+
|
19
|
+
#{@output}
|
20
|
+
END_OF_MESSAGE
|
21
|
+
Net::SMTP.start('localhost') do |smtp|
|
22
|
+
smtp.send_message(msg,@values['mailto'],@values['mailto'])
|
23
|
+
smtp.finish
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/required.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module FormProcessor
|
2
|
+
# Checks to make sure all required fields are present and accounted for.
|
3
|
+
class Required
|
4
|
+
# A String with the list of missing fields.
|
5
|
+
attr_reader :result
|
6
|
+
# Expects a Hash passed from Main.
|
7
|
+
def initialize(values)
|
8
|
+
@values = values
|
9
|
+
@result = String.new
|
10
|
+
run
|
11
|
+
end
|
12
|
+
private
|
13
|
+
def run
|
14
|
+
if @values.has_key? 'required'
|
15
|
+
@values['required'].to_s.split(',').each do |val|
|
16
|
+
val.strip!
|
17
|
+
if !@values.has_key? val or @values[val].empty?
|
18
|
+
if @result.length == 0 then @result = 'You forgot to fill in the following required fields:<br />' end
|
19
|
+
@result += val.capitalize + '<br />'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formprocessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kenton Small
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'Author: Kenton Small
|
15
|
+
|
16
|
+
Purpose: Process web forms. See docs for usage.
|
17
|
+
|
18
|
+
Provided under the MIT license (see the LICENSE file).'
|
19
|
+
email: kentonthegreat@gmail.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- lib/formprocessor.rb
|
25
|
+
- lib/required.rb
|
26
|
+
- lib/mailer.rb
|
27
|
+
homepage: http://www.kentonsmall.com
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '1.9'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements:
|
46
|
+
- An SMTP server on localhost
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.11
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Process web forms and have the results emailed to you.
|
52
|
+
test_files: []
|