kelredd-sinatra-helpers 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +12 -1
- data/Rakefile +1 -2
- data/lib/sinatra_helpers/environment_tests.rb +2 -0
- data/lib/sinatra_helpers/erb/partials.rb +14 -5
- data/lib/sinatra_helpers/erb.rb +6 -1
- data/lib/sinatra_helpers/version.rb +1 -1
- data/lib/sinatra_helpers.rb +0 -2
- metadata +3 -22
- data/lib/sinatra_helpers/erb/error_pages.rb +0 -19
- data/lib/sinatra_helpers/erb/forms.rb +0 -125
- data/lib/sinatra_helpers/erb/helpers.rb +0 -39
- data/lib/sinatra_helpers/erb/links.rb +0 -73
- data/lib/sinatra_helpers/erb/tags.rb +0 -50
- data/lib/sinatra_helpers/mailer/base.rb +0 -83
- data/lib/sinatra_helpers/mailer/exceptions.rb +0 -12
- data/lib/sinatra_helpers/mailer/tls.rb +0 -73
- data/lib/sinatra_helpers/mailer.rb +0 -4
data/README.rdoc
CHANGED
@@ -6,9 +6,20 @@ This is a ruby gem with a bunch of helpers to make Sinatra more useful.
|
|
6
6
|
|
7
7
|
=== Environment Tests
|
8
8
|
|
9
|
+
Have environment tests like development? and production? available in your app code
|
10
|
+
|
9
11
|
=== Erb Helpers
|
10
12
|
|
11
|
-
|
13
|
+
Helpful view template helpers for partial rendering and html generation. See
|
14
|
+
http://github.com/kelredd/useful/tree/master/lib/useful/erb_helpers/ for more info.
|
15
|
+
|
16
|
+
=== Sprockets
|
17
|
+
|
18
|
+
Coming.
|
19
|
+
|
20
|
+
=== ActiveRecord
|
21
|
+
|
22
|
+
Coming.
|
12
23
|
|
13
24
|
== Installation
|
14
25
|
|
data/Rakefile
CHANGED
@@ -17,8 +17,7 @@ spec = Gem::Specification.new do |s|
|
|
17
17
|
s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib}/**/*")
|
18
18
|
# s.executables = ['sinatra-helpers']
|
19
19
|
|
20
|
-
s.add_dependency('kelredd-useful', '>= 0.
|
21
|
-
s.add_dependency('tmail', '>= 1.2.3.0')
|
20
|
+
s.add_dependency('kelredd-useful', '>= 0.2.0')
|
22
21
|
end
|
23
22
|
|
24
23
|
Rake::GemPackageTask.new(spec) do |pkg|
|
@@ -1,13 +1,17 @@
|
|
1
|
+
require 'erb'
|
1
2
|
require 'sinatra/base'
|
2
3
|
|
4
|
+
module SinatraHelpers; end
|
5
|
+
module SinatraHelpers::Erb; end
|
6
|
+
|
3
7
|
module SinatraHelpers::Erb::Partials
|
4
8
|
|
5
|
-
# helper to emulate rails 'render :partial' helper, using erb
|
9
|
+
# helper to emulate rails' 'render :partial' helper, using erb
|
6
10
|
# => taken from the sinatra book, http://sinatra-book.gittr.com/#implemention_of_rails_style_partials
|
7
|
-
# Render the
|
11
|
+
# Render the partial once:
|
8
12
|
# Usage: partial :foo
|
9
13
|
#
|
10
|
-
# foo will be rendered once for each element in the array, passing in a local variable named "foo"
|
14
|
+
# foo partial will be rendered once for each element in the array, passing in a local variable named "foo"
|
11
15
|
# Usage: partial :foo, :collection => @my_foos
|
12
16
|
def partial(template, options={})
|
13
17
|
options.merge!(:layout => false)
|
@@ -17,14 +21,18 @@ module SinatraHelpers::Erb::Partials
|
|
17
21
|
template = File.join(path).to_sym
|
18
22
|
raise 'partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
|
19
23
|
if collection = options.delete(:collection)
|
24
|
+
options.delete(:object) # ignore any object passed in when using :collection
|
20
25
|
counter = 0
|
21
26
|
collection.inject([]) do |buffer, member|
|
22
27
|
counter += 1
|
23
|
-
|
28
|
+
options[:locals] ||= {}
|
29
|
+
options[:locals].merge!({object => member, "#{object}_counter".to_sym => counter})
|
30
|
+
buffer << erb(template, options)
|
24
31
|
end.join("\n")
|
25
32
|
else
|
26
33
|
if member = options.delete(:object)
|
27
|
-
options
|
34
|
+
options[:locals] ||= {}
|
35
|
+
options[:locals].merge!({object => member})
|
28
36
|
end
|
29
37
|
erb(template, options)
|
30
38
|
end
|
@@ -33,3 +41,4 @@ module SinatraHelpers::Erb::Partials
|
|
33
41
|
end
|
34
42
|
|
35
43
|
Sinatra::Application.helpers SinatraHelpers::Erb::Partials
|
44
|
+
|
data/lib/sinatra_helpers/erb.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
module SinatraHelpers::Erb; end
|
2
1
|
Dir[File.join(File.dirname(__FILE__), "erb" ,"*.rb")].each do |file|
|
3
2
|
require "sinatra_helpers/erb/#{File.basename(file, ".rb")}"
|
4
3
|
end
|
4
|
+
|
5
|
+
require "useful/erb_helpers"
|
6
|
+
Sinatra::Application.helpers Useful::ErbHelpers::Tags
|
7
|
+
Sinatra::Application.helpers Useful::ErbHelpers::Links
|
8
|
+
Sinatra::Application.helpers Useful::ErbHelpers::Forms
|
9
|
+
Sinatra::Application.helpers Useful::ErbHelpers::Proper
|
data/lib/sinatra_helpers.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kelredd-sinatra-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelly Redding
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-07 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,17 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.
|
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
|
23
|
+
version: 0.2.0
|
34
24
|
version:
|
35
25
|
description:
|
36
26
|
email: kelly@kelredd.com
|
@@ -44,17 +34,8 @@ files:
|
|
44
34
|
- README.rdoc
|
45
35
|
- Rakefile
|
46
36
|
- 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
37
|
- lib/sinatra_helpers/erb/partials.rb
|
52
|
-
- lib/sinatra_helpers/erb/tags.rb
|
53
38
|
- 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
39
|
- lib/sinatra_helpers/version.rb
|
59
40
|
- lib/sinatra_helpers.rb
|
60
41
|
has_rdoc: true
|
@@ -1,19 +0,0 @@
|
|
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
|
@@ -1,125 +0,0 @@
|
|
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
|
@@ -1,39 +0,0 @@
|
|
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
|
@@ -1,73 +0,0 @@
|
|
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
|
@@ -1,50 +0,0 @@
|
|
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
|
-
|
@@ -1,83 +0,0 @@
|
|
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
|
@@ -1,73 +0,0 @@
|
|
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
|