foca-sinatra-diddies 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +74 -0
- data/lib/diddies.rb +2 -0
- data/lib/diddies/authorization.rb +61 -0
- data/lib/diddies/mailer.rb +110 -0
- metadata +82 -0
data/README.markdown
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
Sinatra Diddies
|
2
|
+
===============
|
3
|
+
|
4
|
+
All those handy tunes in one elegant package.
|
5
|
+
|
6
|
+
Sinatra::Mailer
|
7
|
+
===============
|
8
|
+
|
9
|
+
Adds an `email` method to your email handlers, that receives a hash of values
|
10
|
+
to create your email.
|
11
|
+
|
12
|
+
For example:
|
13
|
+
|
14
|
+
post "/signup" do
|
15
|
+
# sign up the user, and then:
|
16
|
+
email :to => @user.email,
|
17
|
+
:from => "awesomeness@example.com",
|
18
|
+
:subject => "Welcome to Awesomeness!",
|
19
|
+
:body => haml(:some_template)
|
20
|
+
end
|
21
|
+
|
22
|
+
Configuration
|
23
|
+
=============
|
24
|
+
|
25
|
+
This plugin is very dirty yet :) Since it's just a port to Sinatra of
|
26
|
+
[Merb::Mailer][merb-mailer]. So the configuration is not Sinatra-y, yet.
|
27
|
+
But we'll get to that.
|
28
|
+
|
29
|
+
Using SMTP
|
30
|
+
----------
|
31
|
+
|
32
|
+
Sinatra::Mailer.config = {
|
33
|
+
:host => 'smtp.yourserver.com',
|
34
|
+
:port => '25',
|
35
|
+
:user => 'user',
|
36
|
+
:pass => 'pass',
|
37
|
+
:auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
38
|
+
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
39
|
+
}
|
40
|
+
|
41
|
+
Using Gmail SMTP
|
42
|
+
----------------
|
43
|
+
|
44
|
+
You need [smtp-tls][], a gem that improves `net/smtp` to add support for secure
|
45
|
+
servers such as Gmail.
|
46
|
+
|
47
|
+
require "smtp-tls"
|
48
|
+
|
49
|
+
Sinatra::Mailer.config = {
|
50
|
+
:host => 'smtp.gmail.com',
|
51
|
+
:port => '587',
|
52
|
+
:user => 'user@gmail.com',
|
53
|
+
:pass => 'pass',
|
54
|
+
:auth => :plain
|
55
|
+
}
|
56
|
+
|
57
|
+
Make sure that when you call your `email` method you pass the `:text` option
|
58
|
+
and not `:body`.
|
59
|
+
|
60
|
+
Using sendmail
|
61
|
+
--------------
|
62
|
+
|
63
|
+
Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
64
|
+
Sinatra::Mailer.delivery_method = :sendmail
|
65
|
+
|
66
|
+
Credits
|
67
|
+
=======
|
68
|
+
|
69
|
+
This has been blatantly adapted from [Merb::Mailer][merb-mailer], so all credit
|
70
|
+
is theirs, I just ported it to [Sinatra][].
|
71
|
+
|
72
|
+
[merb-mailer]: http://github.com/wycats/merb-more/tree/master/merb-mailer
|
73
|
+
[smtp-tls]: http://github.com/ambethia/smtp-tls/tree/master
|
74
|
+
[Sinatra]: http://sinatrarb.com
|
data/lib/diddies.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Sinatra
|
2
|
+
# HTTP Authorization helpers for Sinatra.
|
3
|
+
#
|
4
|
+
# In your helpers module, include Sinatra::Authorization and then define
|
5
|
+
# a +authorize(user, password)+ method to handle user provided
|
6
|
+
# credentials.
|
7
|
+
#
|
8
|
+
# Inside your events, call +login_required+ to trigger the HTTP
|
9
|
+
# Authorization window to pop up in the browser.
|
10
|
+
#
|
11
|
+
# Code adapted from Ryan Tomayko <http://tomayko.com> and Christopher
|
12
|
+
# Schneid <http://gittr.com>, shared under an MIT License
|
13
|
+
module Authorization
|
14
|
+
# Redefine this method on your helpers block to actually contain
|
15
|
+
# your authorization logic.
|
16
|
+
def authorize(username, password)
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
# From you app, call set :authorization_realm, "my app" to set this
|
21
|
+
# or define a `authorization_realm` method in your helpers block.
|
22
|
+
def authorization_realm
|
23
|
+
Sinatra.options.authorization_realm
|
24
|
+
end
|
25
|
+
|
26
|
+
# Call in any event that requires authentication
|
27
|
+
def login_required
|
28
|
+
return if authorized?
|
29
|
+
unauthorized! unless auth.provided?
|
30
|
+
bad_request! unless auth.basic?
|
31
|
+
unauthorized! unless authorize(*auth.credentials)
|
32
|
+
request.env['REMOTE_USER'] = auth.username
|
33
|
+
end
|
34
|
+
|
35
|
+
# Convenience method to determine if a user is logged in
|
36
|
+
def authorized?
|
37
|
+
!!request.env['REMOTE_USER']
|
38
|
+
end
|
39
|
+
alias :logged_in? :authorized?
|
40
|
+
|
41
|
+
# Name provided by the current user to log in
|
42
|
+
def current_user
|
43
|
+
request.env['REMOTE_USER']
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def auth
|
49
|
+
@auth ||= Rack::Auth::Basic::Request.new(request.env)
|
50
|
+
end
|
51
|
+
|
52
|
+
def unauthorized!(realm=authorization_realm)
|
53
|
+
header 'WWW-Authenticate' => %(Basic realm="#{realm}")
|
54
|
+
throw :halt, [ 401, 'Authorization Required' ]
|
55
|
+
end
|
56
|
+
|
57
|
+
def bad_request!
|
58
|
+
throw :halt, [ 400, 'Bad Request' ]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# Shamelssly stolen from Merb::Mailer
|
2
|
+
# http://merbivore.com
|
3
|
+
|
4
|
+
require 'net/smtp'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'mailfactory'
|
7
|
+
require 'tlsmail'
|
8
|
+
|
9
|
+
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
|
10
|
+
|
11
|
+
class MailFactory
|
12
|
+
attr_reader :html, :text
|
13
|
+
end
|
14
|
+
|
15
|
+
module Sinatra
|
16
|
+
# You'll need a simple config like this in your configure block if you want
|
17
|
+
# to actually send mail:
|
18
|
+
#
|
19
|
+
# Sinatra::Mailer.config = {
|
20
|
+
# :host => 'smtp.yourserver.com',
|
21
|
+
# :port => '25',
|
22
|
+
# :user => 'user',
|
23
|
+
# :pass => 'pass',
|
24
|
+
# :auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
25
|
+
# :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
26
|
+
# }
|
27
|
+
#
|
28
|
+
# or
|
29
|
+
#
|
30
|
+
# Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
31
|
+
# Sinatra::Mailer.delivery_method = :sendmail
|
32
|
+
#
|
33
|
+
# From your event handlers then, you can just call the 'email' method to deliver an email:
|
34
|
+
#
|
35
|
+
# email :to => 'foo@bar.com',
|
36
|
+
# :from => 'bar@foo.com',
|
37
|
+
# :subject => 'Welcome to whatever!',
|
38
|
+
# :body => haml(:sometemplate)
|
39
|
+
#
|
40
|
+
module Mailer
|
41
|
+
class << self
|
42
|
+
attr_accessor :config, :delivery_method
|
43
|
+
end
|
44
|
+
|
45
|
+
def email(mail_options={})
|
46
|
+
Email.new(mail_options).deliver!
|
47
|
+
end
|
48
|
+
|
49
|
+
class Email
|
50
|
+
attr_accessor :mail, :config
|
51
|
+
|
52
|
+
# Sends the mail using sendmail.
|
53
|
+
def sendmail
|
54
|
+
sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+')
|
55
|
+
sendmail.puts @mail.to_s
|
56
|
+
sendmail.close
|
57
|
+
end
|
58
|
+
|
59
|
+
# Sends the mail using SMTP.
|
60
|
+
def net_smtp
|
61
|
+
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
|
62
|
+
config[:user], config[:pass], config[:auth]) { |smtp|
|
63
|
+
smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
# Delivers the mail with the specified delivery method, defaulting to
|
68
|
+
# net_smtp.
|
69
|
+
def deliver!
|
70
|
+
send(Mailer.delivery_method || :net_smtp)
|
71
|
+
end
|
72
|
+
|
73
|
+
# ==== Parameters
|
74
|
+
# file_or_files<File, Array[File]>:: File(s) to attach.
|
75
|
+
# filename<String>::
|
76
|
+
# type<~to_s>::
|
77
|
+
# The attachment MIME type. If left out, it will be determined from
|
78
|
+
# file_or_files.
|
79
|
+
# headers<String, Array>:: Additional attachment headers.
|
80
|
+
#
|
81
|
+
# ==== Raises
|
82
|
+
# ArgumentError::
|
83
|
+
# file_or_files was not a File or an Array of File instances.
|
84
|
+
def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
|
85
|
+
type = nil, headers = nil)
|
86
|
+
if file_or_files.is_a?(Array)
|
87
|
+
file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
|
88
|
+
else
|
89
|
+
raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
|
90
|
+
@mail.add_attachment_as(file_or_files, filename, type, headers)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# ==== Parameters
|
95
|
+
# o<Hash{~to_s => Object}>:: Configuration commands to send to MailFactory.
|
96
|
+
def initialize(o={})
|
97
|
+
self.config = Mailer.config || {:sendmail_path => '/usr/sbin/sendmail'}
|
98
|
+
o[:rawhtml] = o.delete(:html)
|
99
|
+
m = MailFactory.new()
|
100
|
+
o.each { |k,v| m.send "#{k}=", v }
|
101
|
+
@mail = m
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class EventContext
|
108
|
+
include Mailer
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: foca-sinatra-diddies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Nicol\xC3\xA1s Sanguinetti"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-10 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sinatra
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.3.2
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: mailfactory
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 1.4.0
|
32
|
+
version:
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: tlsmail
|
35
|
+
version_requirement:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
description: A series of plugins and useful helpers for the Sinatra web framework
|
43
|
+
email: contacto@nicolassanguinetti.info
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
extra_rdoc_files: []
|
49
|
+
|
50
|
+
files:
|
51
|
+
- README.markdown
|
52
|
+
- lib/diddies.rb
|
53
|
+
- lib/diddies/authorization.rb
|
54
|
+
- lib/diddies/mailer.rb
|
55
|
+
has_rdoc: false
|
56
|
+
homepage: http://github.com/foca/sinatra-diddies
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.2.0
|
78
|
+
signing_key:
|
79
|
+
specification_version: 2
|
80
|
+
summary: A series of plugins and useful helpers for the Sinatra web framework
|
81
|
+
test_files: []
|
82
|
+
|