kelredd-sinatra-helpers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +44 -0
- data/Rakefile +64 -0
- data/lib/sinatra_helpers.rb +4 -0
- data/lib/sinatra_helpers/environment_tests.rb +15 -0
- data/lib/sinatra_helpers/erb.rb +4 -0
- data/lib/sinatra_helpers/erb/error_pages.rb +19 -0
- data/lib/sinatra_helpers/erb/forms.rb +125 -0
- data/lib/sinatra_helpers/erb/helpers.rb +39 -0
- data/lib/sinatra_helpers/erb/links.rb +73 -0
- data/lib/sinatra_helpers/erb/partials.rb +35 -0
- data/lib/sinatra_helpers/erb/tags.rb +50 -0
- data/lib/sinatra_helpers/mailer.rb +4 -0
- data/lib/sinatra_helpers/mailer/base.rb +83 -0
- data/lib/sinatra_helpers/mailer/exceptions.rb +12 -0
- data/lib/sinatra_helpers/mailer/tls.rb +73 -0
- data/lib/sinatra_helpers/version.rb +13 -0
- metadata +90 -0
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= SinatraHelpers
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
This is a ruby gem with a bunch of helpers to make Sinatra more useful.
|
6
|
+
|
7
|
+
=== Environment Tests
|
8
|
+
|
9
|
+
=== Erb Helpers
|
10
|
+
|
11
|
+
=== Mailer
|
12
|
+
|
13
|
+
== Installation
|
14
|
+
|
15
|
+
sudo gem install kelredd-sinatra-helpers --source http://gemcutter.org
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
require 'sinatra_helpers'
|
20
|
+
|
21
|
+
== License
|
22
|
+
|
23
|
+
Copyright (c) 2009 Kelly Redding
|
24
|
+
|
25
|
+
Permission is hereby granted, free of charge, to any person
|
26
|
+
obtaining a copy of this software and associated documentation
|
27
|
+
files (the "Software"), to deal in the Software without
|
28
|
+
restriction, including without limitation the rights to use,
|
29
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
30
|
+
copies of the Software, and to permit persons to whom the
|
31
|
+
Software is furnished to do so, subject to the following
|
32
|
+
conditions:
|
33
|
+
|
34
|
+
The above copyright notice and this permission notice shall be
|
35
|
+
included in all copies or substantial portions of the Software.
|
36
|
+
|
37
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
38
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
39
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
40
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
41
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
42
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
43
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
44
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
require 'lib/sinatra_helpers/version'
|
6
|
+
|
7
|
+
spec = Gem::Specification.new do |s|
|
8
|
+
s.name = 'kelredd-sinatra-helpers'
|
9
|
+
s.version = SinatraHelpers::Version.to_s
|
10
|
+
s.has_rdoc = true
|
11
|
+
s.extra_rdoc_files = %w(README.rdoc)
|
12
|
+
s.rdoc_options = %w(--main README.rdoc)
|
13
|
+
s.summary = "a ruby gem with a bunch of helpers to make Sinatra more useful"
|
14
|
+
s.author = 'Kelly Redding'
|
15
|
+
s.email = 'kelly@kelredd.com'
|
16
|
+
s.homepage = 'http://github.com/kelredd/sinatra-helpers'
|
17
|
+
s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib}/**/*")
|
18
|
+
# s.executables = ['sinatra-helpers']
|
19
|
+
|
20
|
+
s.add_dependency('kelredd-useful', '>= 0.1.25')
|
21
|
+
s.add_dependency('tmail', '>= 1.2.3.0')
|
22
|
+
end
|
23
|
+
|
24
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
25
|
+
pkg.gem_spec = spec
|
26
|
+
end
|
27
|
+
|
28
|
+
Rake::TestTask.new do |t|
|
29
|
+
t.libs << 'test'
|
30
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
31
|
+
t.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
|
37
|
+
Rcov::RcovTask.new(:coverage) do |t|
|
38
|
+
t.libs = ['test']
|
39
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
40
|
+
t.verbose = true
|
41
|
+
t.rcov_opts = ['--text-report', "-x #{Gem.path}", '-x /Library/Ruby', '-x /usr/lib/ruby']
|
42
|
+
end
|
43
|
+
|
44
|
+
task :default => :coverage
|
45
|
+
|
46
|
+
rescue LoadError
|
47
|
+
warn "\n**** Install rcov (sudo gem install relevance-rcov) to get coverage stats ****\n"
|
48
|
+
task :default => :test
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
desc 'Generate the gemspec to serve this gem'
|
53
|
+
task :gemspec do
|
54
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
55
|
+
File.open(file, 'w') {|f| f << spec.to_ruby }
|
56
|
+
puts "Created gemspec: #{file}"
|
57
|
+
end
|
58
|
+
|
59
|
+
require 'cucumber'
|
60
|
+
require 'cucumber/rake/task'
|
61
|
+
|
62
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
63
|
+
t.cucumber_opts = "test/features --format pretty"
|
64
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module SinatraHelpers::EnvironmentTests
|
4
|
+
|
5
|
+
def production?
|
6
|
+
Sinatra::Application.environment.to_s == 'production'
|
7
|
+
end
|
8
|
+
def development?
|
9
|
+
Sinatra::Application.environment.to_s == 'development'
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
Sinatra::Application.helpers SinatraHelpers::EnvironmentTests
|
15
|
+
Sinatra::Application.register SinatraHelpers::EnvironmentTests
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module SinatraHelpers::Erb::ErrorPages
|
4
|
+
|
5
|
+
def self.registered(app)
|
6
|
+
app.configure :production do
|
7
|
+
app.not_found do
|
8
|
+
erb :'404.html'
|
9
|
+
end
|
10
|
+
app.error do
|
11
|
+
@err = request.env['sinatra_error']
|
12
|
+
erb :'500.html'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
Sinatra::Application.register SinatraHelpers::Erb::ErrorPages
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'sinatra_helpers/erb/helpers')
|
3
|
+
require 'sinatra_helpers/erb/tags')
|
4
|
+
|
5
|
+
module SinatraHelpers::Erb::Forms
|
6
|
+
|
7
|
+
include SinatraHelpers::Erb::Helpers
|
8
|
+
|
9
|
+
def form_tag(url, options={}, &block)
|
10
|
+
options[:method] = 'post' unless ['get','post','put','delete'].include?(options[:method])
|
11
|
+
options.update :action => url
|
12
|
+
if multipart = options.delete(:multipart)
|
13
|
+
options[:enctype] = sinatra_erb_helper_multipart_option
|
14
|
+
end
|
15
|
+
if block_given?
|
16
|
+
@_out_buf << tag(:form, options) { sinatra_erb_helper_capture(&block) }
|
17
|
+
else
|
18
|
+
tag(:form, options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def field_set_tag(legend=nil, options={}, &block)
|
23
|
+
legend_html = legend.nil? ? '' : tag(:legend) { legend.to_s }
|
24
|
+
if block_given?
|
25
|
+
@_out_buf << tag(:fieldset, options) { legend_html + sinatra_erb_helper_capture(&block) }
|
26
|
+
else
|
27
|
+
tag(:fieldset, options) { legend_html }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def label_tag(name, value=nil, options={})
|
32
|
+
value ||= name.to_s.capitalize
|
33
|
+
options.update :for => name.to_s
|
34
|
+
tag(:label, options) { value }
|
35
|
+
end
|
36
|
+
|
37
|
+
def hidden_field_tag(name, value=nil, options={})
|
38
|
+
input_tag('hidden', name, value, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def password_field_tag(name="password", value=nil, options={})
|
42
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
43
|
+
input_tag('password', name, value, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def file_field_tag(name, options={})
|
47
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
48
|
+
input_tag('file', name, nil, options)
|
49
|
+
end
|
50
|
+
|
51
|
+
def check_box_tag(name, label=nil, value='1', checked=false, options={})
|
52
|
+
tag_name = options.delete(:tag) || :div
|
53
|
+
if options.has_key?(:class)
|
54
|
+
options[:class] += ' checkbox'
|
55
|
+
else
|
56
|
+
options[:class] = 'checkbox'
|
57
|
+
end
|
58
|
+
options[:id] ||= sinatra_erb_helper_safe_id(rand(1000).to_s)
|
59
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
60
|
+
options[:checked] = sinatra_erb_helper_checked_option if checked
|
61
|
+
input_str = input_tag('checkbox', name, value, options)
|
62
|
+
if label.nil?
|
63
|
+
input_str
|
64
|
+
else
|
65
|
+
tag(tag_name, :class => 'checkbox') { input_str + label_tag(options[:id], label) }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def radio_button_tag(name, value, label=nil, checked=false, options={})
|
70
|
+
tag_name = options.delete(:tag) || :span
|
71
|
+
if options.has_key?(:class)
|
72
|
+
options[:class] += ' radiobutton'
|
73
|
+
else
|
74
|
+
options[:class] = 'radiobutton'
|
75
|
+
end
|
76
|
+
label ||= value.to_s.capitalize
|
77
|
+
options[:id] ||= sinatra_erb_helper_safe_id(rand(1000).to_s)
|
78
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
79
|
+
options[:checked] = sinatra_erb_helper_checked_option if checked
|
80
|
+
input_str = input_tag('radio', name, value, options)
|
81
|
+
if label.nil?
|
82
|
+
input_str
|
83
|
+
else
|
84
|
+
label_tag(options[:id], input_str + tag(tag_name) { label }, :class => options.delete(:class))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def select_tag(name, options={}, &block)
|
89
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
90
|
+
html_name = (options[:multiple] == true && !name.to_s[(name.to_s.length-2)..-1] == "[]") ? "#{name}[]" : name
|
91
|
+
options[:multiple] = sinatra_erb_helper_multiple_option if options[:multiple] == true
|
92
|
+
options[:tag] = 'select'
|
93
|
+
if block_given?
|
94
|
+
@_out_buf << input_tag(:select, html_name, nil, options) { sinatra_erb_helper_capture(&block) }
|
95
|
+
else
|
96
|
+
input_tag(:select, html_name, nil, options)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def text_area_tag(name, content=nil, options={})
|
101
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
102
|
+
options[:tag] = 'textarea'
|
103
|
+
input_tag(nil, name, nil, options) { content || '' }
|
104
|
+
end
|
105
|
+
|
106
|
+
def text_field_tag(name, value=nil, options={})
|
107
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
108
|
+
input_tag('text', name, value, options)
|
109
|
+
end
|
110
|
+
|
111
|
+
def submit_tag(value="Save", options={})
|
112
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
113
|
+
input_tag('submit', 'commit', value, options)
|
114
|
+
end
|
115
|
+
|
116
|
+
def image_submit_tag(source, options={})
|
117
|
+
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
118
|
+
options[:src] = source
|
119
|
+
options[:alt] ||= 'Save'
|
120
|
+
input_tag('image', nil, nil, options)
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
Sinatra::Application.helpers SinatraHelpers::Erb::Forms
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module SinatraHelpers::Erb::Helpers
|
2
|
+
|
3
|
+
def sinatra_erb_helper_safe_id(id)
|
4
|
+
id.gsub(/\W/,'')
|
5
|
+
end
|
6
|
+
|
7
|
+
def sinatra_erb_helper_capture(*args, &block)
|
8
|
+
sinatra_erb_helper_with_output_buffer { block.call(*args) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def sinatra_erb_helper_with_output_buffer(buf = '') #:nodoc:
|
12
|
+
@_out_buf, old_buffer = buf, @_out_buf
|
13
|
+
yield
|
14
|
+
@_out_buf
|
15
|
+
ensure
|
16
|
+
@_out_buf = old_buffer
|
17
|
+
end
|
18
|
+
|
19
|
+
def sinatra_erb_helper_hash_to_html_attrs(a_hash)
|
20
|
+
a_hash.collect{|key, val| "#{key}=\"#{val}\""}.join(' ')
|
21
|
+
end
|
22
|
+
|
23
|
+
def sinatra_erb_helper_disabled_option
|
24
|
+
'disabled'
|
25
|
+
end
|
26
|
+
|
27
|
+
def sinatra_erb_helper_checked_option
|
28
|
+
'checked'
|
29
|
+
end
|
30
|
+
|
31
|
+
def sinatra_erb_helper_multiple_option
|
32
|
+
'multiple'
|
33
|
+
end
|
34
|
+
|
35
|
+
def sinatra_erb_helper_multipart_option
|
36
|
+
'multipart/form-data'
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'sinatra_helpers/erb/tags')
|
3
|
+
|
4
|
+
module SinatraHelpers::Erb::Links
|
5
|
+
|
6
|
+
# helper to emulate action view's 'link_to'
|
7
|
+
# EX : link_to "google", "http://google.com"
|
8
|
+
# => <a href="http://google.com">google</a>
|
9
|
+
def link_to(content,href,options={})
|
10
|
+
options.update :href => href
|
11
|
+
tag(:a, options) { content }
|
12
|
+
end
|
13
|
+
|
14
|
+
def open_link_to(content, href, options={})
|
15
|
+
options[:onclick] = "javascript: window.open('#{href}'); return false;"
|
16
|
+
link_to(content, href, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def mail_link_to(email, options={})
|
20
|
+
link_to options[:label] || email, "mailto: #{email}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def link_to_function(content, function, opts={})
|
24
|
+
opts ||= {}
|
25
|
+
opts[:href] ||= 'javascript: void(0);'
|
26
|
+
opts[:onclick] = "javascript: #{function}; return false;"
|
27
|
+
link_to content, opts[:href], opts
|
28
|
+
end
|
29
|
+
|
30
|
+
# helper to emulate 'image_tag'
|
31
|
+
# EX : image_tag 'logo.jpg'
|
32
|
+
# => <img src="images/logo.jpg" />
|
33
|
+
def image_tag(src,options={})
|
34
|
+
options[:src] = ['/'].include?(src[0..0]) ? src : "/images/#{src}"
|
35
|
+
tag(:img, options)
|
36
|
+
end
|
37
|
+
|
38
|
+
# helper to emulate 'stylesheet_link_tag'
|
39
|
+
# EX : stylesheet_link_tag 'default'
|
40
|
+
# => <link rel="stylesheet" href="/stylesheets/default.css" type="text/css" media="all" title="no title" charset="utf-8">
|
41
|
+
def stylesheet_link_tag(srcs,options={})
|
42
|
+
options[:media] ||= "screen"
|
43
|
+
options[:type] ||= "text/css"
|
44
|
+
options[:rel] ||= "stylesheet"
|
45
|
+
srcs.to_a.collect do |src|
|
46
|
+
options[:href] = "/stylesheets/#{src}.css#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
47
|
+
tag(:link, options)
|
48
|
+
end.join("\n")
|
49
|
+
end
|
50
|
+
|
51
|
+
# helper to emulate 'javascript_include_tag'
|
52
|
+
# EX : javascript_include_tag 'app'
|
53
|
+
# => <script src="/js/app.js" type="text/javascript" />
|
54
|
+
# EX : javascript_include_tag ['app', 'jquery']
|
55
|
+
# => <script src="/js/app.js" type="text/javascript" />
|
56
|
+
# => <script src="/js/jquery.js" type="text/javascript" />
|
57
|
+
def javascript_include_tag(srcs,options={})
|
58
|
+
options[:type] ||= "text/javascript"
|
59
|
+
srcs.to_a.collect do |src|
|
60
|
+
options[:src] = "/javascripts/#{src}.js#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
61
|
+
tag(:script, options) { '' }
|
62
|
+
end.join("\n")
|
63
|
+
end
|
64
|
+
|
65
|
+
# helper to emulate 'javascript_tag'
|
66
|
+
def javascript_tag(options={})
|
67
|
+
options[:type] ||= "text/javascript"
|
68
|
+
tag(:script, options) { yield }
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
Sinatra::Application.helpers SinatraHelpers::Erb::Links
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module SinatraHelpers::Erb::Partials
|
4
|
+
|
5
|
+
# helper to emulate rails 'render :partial' helper, using erb
|
6
|
+
# => taken from the sinatra book, http://sinatra-book.gittr.com/#implemention_of_rails_style_partials
|
7
|
+
# Render the page once:
|
8
|
+
# Usage: partial :foo
|
9
|
+
#
|
10
|
+
# foo will be rendered once for each element in the array, passing in a local variable named "foo"
|
11
|
+
# Usage: partial :foo, :collection => @my_foos
|
12
|
+
def partial(template, options={})
|
13
|
+
options.merge!(:layout => false)
|
14
|
+
path = template.to_s.split(File::SEPARATOR)
|
15
|
+
object = path[-1].to_sym
|
16
|
+
path[-1] = "_#{path[-1]}"
|
17
|
+
template = File.join(path).to_sym
|
18
|
+
raise 'partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
|
19
|
+
if collection = options.delete(:collection)
|
20
|
+
counter = 0
|
21
|
+
collection.inject([]) do |buffer, member|
|
22
|
+
counter += 1
|
23
|
+
buffer << erb(template, options.merge(:locals => {object => member, "#{object}_counter".to_sym => counter}))
|
24
|
+
end.join("\n")
|
25
|
+
else
|
26
|
+
if member = options.delete(:object)
|
27
|
+
options.merge!(:locals => {object => member})
|
28
|
+
end
|
29
|
+
erb(template, options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
Sinatra::Application.helpers SinatraHelpers::Erb::Partials
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'sinatra_helpers/erb/helpers.rb')
|
3
|
+
|
4
|
+
module SinatraHelpers::Erb::Tags
|
5
|
+
|
6
|
+
include SinatraHelpers::Erb::Helpers
|
7
|
+
|
8
|
+
def input_tag(type, name, value, options={}, &block)
|
9
|
+
options[:tag] ||= :input
|
10
|
+
options[:type] = type unless type.nil?
|
11
|
+
unless name.nil?
|
12
|
+
options[:name] = name
|
13
|
+
options[:id] ||= sinatra_erb_helper_safe_id(name)
|
14
|
+
end
|
15
|
+
options[:value] = value unless value.nil?
|
16
|
+
tag(options.delete(:tag), options, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def clear_tag(options={})
|
20
|
+
options[:tag] ||= :div
|
21
|
+
options[:style] ||= ''
|
22
|
+
options[:style] = "clear: both;#{options[:style]}"
|
23
|
+
tag(options.delete(:tag), options) { '' }
|
24
|
+
end
|
25
|
+
|
26
|
+
# helpers to escape tag text content
|
27
|
+
include Rack::Utils
|
28
|
+
alias_method :h, :escape_html
|
29
|
+
|
30
|
+
# escape tag text content and format for text like display
|
31
|
+
def h_text(text, opts={})
|
32
|
+
h(text.to_s).
|
33
|
+
gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
|
34
|
+
split("\n").collect do |line|
|
35
|
+
line.nil? ? '': line.sub(/(\s+)?\S*/) {|lead| lead.gsub(/\s/,' ')}
|
36
|
+
end.join("\n"). # change any leading white spaces on a line to ' '
|
37
|
+
gsub(/\n/,'\1<br />') # newlines -> br added
|
38
|
+
end
|
39
|
+
|
40
|
+
# emulator for 'tag'
|
41
|
+
# EX : tag :h1, "shizam", :title => "shizam"
|
42
|
+
# => <h1 title="shizam">shizam</h1>
|
43
|
+
def tag(name, options={})
|
44
|
+
"<#{name.to_s} #{sinatra_erb_helper_hash_to_html_attrs(options)} #{block_given? ? ">#{yield}</#{name}" : "/"}>"
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
Sinatra::Application.helpers SinatraHelpers::Erb::Tags
|
50
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'net/smtp'
|
3
|
+
require 'tmail'
|
4
|
+
require 'sinatra/base'
|
5
|
+
require 'sinatra_helpers/mailer/exceptions'
|
6
|
+
require 'sinatra_helpers/mailer/tls'
|
7
|
+
|
8
|
+
module SinatraHelpers::Mailer
|
9
|
+
class Base
|
10
|
+
|
11
|
+
CONFIGS = [:server, :domain, :port, :reply_to, :username, :password, :authentication, :content_type, :charset]
|
12
|
+
CONFIGS.each do |config|
|
13
|
+
attr_reader config
|
14
|
+
end
|
15
|
+
attr_accessor :logger
|
16
|
+
|
17
|
+
def initialize(configs={})
|
18
|
+
CONFIGS.each do |config|
|
19
|
+
instance_variable_set("@#{config}", configs[config])
|
20
|
+
end
|
21
|
+
@authentication ||= :login
|
22
|
+
@reply_to ||= @username
|
23
|
+
@content_type ||= 'text/plain'
|
24
|
+
@charset ||= 'UTF-8'
|
25
|
+
@logger = block_given? ? yield : nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def send(settings={})
|
29
|
+
check_configs
|
30
|
+
[:to, :subject].each do |setting|
|
31
|
+
raise SinatraHelpers::Mailer::SendError, "cannot send, #{setting} not specified." unless settings[setting]
|
32
|
+
end
|
33
|
+
mail = generate_mail(settings)
|
34
|
+
mail.body = yield(mail) if block_given?
|
35
|
+
mail.body ||= ''
|
36
|
+
Sinatra::Application.environment.to_s == 'production' ? send_mail(mail) : log_mail(mail)
|
37
|
+
log(:info, "Sent '#{mail.subject}' to #{mail.to.join(',')}")
|
38
|
+
mail
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def check_configs
|
44
|
+
CONFIGS.each do |config|
|
45
|
+
raise SinatraHelpers::Mailer::ConfigError, "#{config} not configured." unless instance_variable_get("@#{config}")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def generate_mail(settings)
|
50
|
+
mail = TMail::Mail.new
|
51
|
+
mail.to = Array.new([settings[:to]])
|
52
|
+
mail.from = @reply_to
|
53
|
+
mail.reply_to = @reply_to
|
54
|
+
mail.subject = settings[:subject]
|
55
|
+
mail.date = Time.now
|
56
|
+
mail.set_content_type @content_type
|
57
|
+
mail.charset = @charset
|
58
|
+
mail
|
59
|
+
end
|
60
|
+
|
61
|
+
def send_mail(mail)
|
62
|
+
raise SinatraHelpers::Mailer::SendError, "cannot send, bad (or empty) mail object given." unless mail
|
63
|
+
Net::SMTP.start(@server, @port, @domain, @username, @password, @authentication) do |server|
|
64
|
+
mail.to.each {|recipient| server.send_message(mail.to_s, mail.from, recipient) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def log_mail(mail)
|
69
|
+
log(:debug, mail.to_s)
|
70
|
+
end
|
71
|
+
|
72
|
+
def log(level, msg)
|
73
|
+
if(msg)
|
74
|
+
if @logger && @logger.respond_to?(level)
|
75
|
+
@logger.send(level.to_s, msg)
|
76
|
+
else
|
77
|
+
puts "** [#{level.to_s.upcase}]: [Mailer] #{msg}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# Note: This is a complete rip of http://github.com/ambethia/smtp-tls/tree/master
|
2
|
+
# => I chose to copy the source in here instead of add yet another gem depencency
|
3
|
+
# => I take no credit for this work, check out link for more info.
|
4
|
+
|
5
|
+
require 'net/smtp'
|
6
|
+
|
7
|
+
Net::SMTP.class_eval do
|
8
|
+
private
|
9
|
+
def do_start(helodomain, user, secret, authtype)
|
10
|
+
raise IOError, 'SMTP session already started' if @started
|
11
|
+
|
12
|
+
if RUBY_VERSION > "1.8.6"
|
13
|
+
check_auth_args user, secret if user or secret
|
14
|
+
else
|
15
|
+
check_auth_args user, secret, authtype if user or secret
|
16
|
+
end
|
17
|
+
|
18
|
+
sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
|
19
|
+
@socket = Net::InternetMessageIO.new(sock)
|
20
|
+
@socket.read_timeout = 60 #@read_timeout
|
21
|
+
|
22
|
+
check_response(critical { recv_response() })
|
23
|
+
do_helo(helodomain)
|
24
|
+
|
25
|
+
if starttls
|
26
|
+
raise 'openssl library not installed' unless defined?(OpenSSL)
|
27
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
28
|
+
ssl.sync_close = true
|
29
|
+
ssl.connect
|
30
|
+
@socket = Net::InternetMessageIO.new(ssl)
|
31
|
+
@socket.read_timeout = 60 #@read_timeout
|
32
|
+
do_helo(helodomain)
|
33
|
+
end
|
34
|
+
|
35
|
+
authenticate user, secret, authtype if user
|
36
|
+
@started = true
|
37
|
+
ensure
|
38
|
+
unless @started
|
39
|
+
# authentication failed, cancel connection.
|
40
|
+
@socket.close if not @started and @socket and not @socket.closed?
|
41
|
+
@socket = nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def do_helo(helodomain)
|
46
|
+
begin
|
47
|
+
if @esmtp
|
48
|
+
ehlo helodomain
|
49
|
+
else
|
50
|
+
helo helodomain
|
51
|
+
end
|
52
|
+
rescue Net::ProtocolError
|
53
|
+
if @esmtp
|
54
|
+
@esmtp = false
|
55
|
+
@error_occured = false
|
56
|
+
retry
|
57
|
+
end
|
58
|
+
raise
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def starttls
|
63
|
+
getok('STARTTLS') rescue return false
|
64
|
+
return true
|
65
|
+
end
|
66
|
+
|
67
|
+
def quit
|
68
|
+
begin
|
69
|
+
getok('QUIT')
|
70
|
+
rescue EOFError
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kelredd-sinatra-helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kelly Redding
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-13 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: kelredd-useful
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.1.25
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: tmail
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.3.0
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: kelly@kelredd.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- README.rdoc
|
45
|
+
- Rakefile
|
46
|
+
- lib/sinatra_helpers/environment_tests.rb
|
47
|
+
- lib/sinatra_helpers/erb/error_pages.rb
|
48
|
+
- lib/sinatra_helpers/erb/forms.rb
|
49
|
+
- lib/sinatra_helpers/erb/helpers.rb
|
50
|
+
- lib/sinatra_helpers/erb/links.rb
|
51
|
+
- lib/sinatra_helpers/erb/partials.rb
|
52
|
+
- lib/sinatra_helpers/erb/tags.rb
|
53
|
+
- lib/sinatra_helpers/erb.rb
|
54
|
+
- lib/sinatra_helpers/mailer/base.rb
|
55
|
+
- lib/sinatra_helpers/mailer/exceptions.rb
|
56
|
+
- lib/sinatra_helpers/mailer/tls.rb
|
57
|
+
- lib/sinatra_helpers/mailer.rb
|
58
|
+
- lib/sinatra_helpers/version.rb
|
59
|
+
- lib/sinatra_helpers.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/kelredd/sinatra-helpers
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --main
|
67
|
+
- README.rdoc
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.3.5
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: a ruby gem with a bunch of helpers to make Sinatra more useful
|
89
|
+
test_files: []
|
90
|
+
|