sr-sinatra-diddies 0.0.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.markdown +74 -0
- data/lib/diddies.rb +2 -0
- data/lib/diddies/authorization.rb +61 -0
- data/lib/diddies/mailer.rb +107 -0
- metadata +73 -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,107 @@
|
|
1
|
+
# Shamelssly stolen from Merb::Mailer
|
2
|
+
# http://merbivore.com
|
3
|
+
|
4
|
+
require 'net/smtp'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'mailfactory'
|
7
|
+
|
8
|
+
class MailFactory
|
9
|
+
attr_reader :html, :text
|
10
|
+
end
|
11
|
+
|
12
|
+
module Sinatra
|
13
|
+
# You'll need a simple config like this in your configure block if you want
|
14
|
+
# to actually send mail:
|
15
|
+
#
|
16
|
+
# Sinatra::Mailer.config = {
|
17
|
+
# :host => 'smtp.yourserver.com',
|
18
|
+
# :port => '25',
|
19
|
+
# :user => 'user',
|
20
|
+
# :pass => 'pass',
|
21
|
+
# :auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
22
|
+
# :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
23
|
+
# }
|
24
|
+
#
|
25
|
+
# or
|
26
|
+
#
|
27
|
+
# Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
28
|
+
# Sinatra::Mailer.delivery_method = :sendmail
|
29
|
+
#
|
30
|
+
# From your event handlers then, you can just call the 'email' method to deliver an email:
|
31
|
+
#
|
32
|
+
# email :to => 'foo@bar.com',
|
33
|
+
# :from => 'bar@foo.com',
|
34
|
+
# :subject => 'Welcome to whatever!',
|
35
|
+
# :body => haml(:sometemplate)
|
36
|
+
#
|
37
|
+
module Mailer
|
38
|
+
class << self
|
39
|
+
attr_accessor :config, :delivery_method
|
40
|
+
end
|
41
|
+
|
42
|
+
def email(mail_options={})
|
43
|
+
Email.new(mail_options).deliver!
|
44
|
+
end
|
45
|
+
|
46
|
+
class Email
|
47
|
+
attr_accessor :mail, :config
|
48
|
+
|
49
|
+
# Sends the mail using sendmail.
|
50
|
+
def sendmail
|
51
|
+
sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+')
|
52
|
+
sendmail.puts @mail.to_s
|
53
|
+
sendmail.close
|
54
|
+
end
|
55
|
+
|
56
|
+
# Sends the mail using SMTP.
|
57
|
+
def net_smtp
|
58
|
+
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
|
59
|
+
config[:user], config[:pass], config[:auth]) { |smtp|
|
60
|
+
smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
# Delivers the mail with the specified delivery method, defaulting to
|
65
|
+
# net_smtp.
|
66
|
+
def deliver!
|
67
|
+
send(Mailer.delivery_method || :net_smtp)
|
68
|
+
end
|
69
|
+
|
70
|
+
# ==== Parameters
|
71
|
+
# file_or_files<File, Array[File]>:: File(s) to attach.
|
72
|
+
# filename<String>::
|
73
|
+
# type<~to_s>::
|
74
|
+
# The attachment MIME type. If left out, it will be determined from
|
75
|
+
# file_or_files.
|
76
|
+
# headers<String, Array>:: Additional attachment headers.
|
77
|
+
#
|
78
|
+
# ==== Raises
|
79
|
+
# ArgumentError::
|
80
|
+
# file_or_files was not a File or an Array of File instances.
|
81
|
+
def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
|
82
|
+
type = nil, headers = nil)
|
83
|
+
if file_or_files.is_a?(Array)
|
84
|
+
file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
|
85
|
+
else
|
86
|
+
raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
|
87
|
+
@mail.add_attachment_as(file_or_files, filename, type, headers)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# ==== Parameters
|
92
|
+
# o<Hash{~to_s => Object}>:: Configuration commands to send to MailFactory.
|
93
|
+
def initialize(o={})
|
94
|
+
self.config = Mailer.config || {:sendmail_path => '/usr/sbin/sendmail'}
|
95
|
+
o[:rawhtml] = o.delete(:html)
|
96
|
+
m = MailFactory.new()
|
97
|
+
o.each { |k,v| m.send "#{k}=", v }
|
98
|
+
@mail = m
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class EventContext
|
105
|
+
include Mailer
|
106
|
+
end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sr-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-09 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
|
+
description: A series of plugins and useful helpers for the Sinatra web framework
|
34
|
+
email: contacto@nicolassanguinetti.info
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- README.markdown
|
43
|
+
- lib/diddies.rb
|
44
|
+
- lib/diddies/authorization.rb
|
45
|
+
- lib/diddies/mailer.rb
|
46
|
+
has_rdoc: false
|
47
|
+
homepage: http://github.com/foca/sinatra-diddies
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.2.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: A series of plugins and useful helpers for the Sinatra web framework
|
72
|
+
test_files: []
|
73
|
+
|