foca-sinatra-ditties 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -0
- data/lib/sinatra/ditties.rb +12 -0
- data/lib/sinatra/ditties/authorization.rb +61 -0
- data/lib/sinatra/ditties/mailer.rb +146 -0
- metadata +7 -7
- data/README.markdown +0 -74
data/README.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "sinatra/base"
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module Ditties
|
5
|
+
def self.version
|
6
|
+
"0.0.2".freeze
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
autoload :Authorization, File.dirname(__FILE__) + "/ditties/authorization"
|
11
|
+
autoload :Mailer, File.dirname(__FILE__) + "/ditties/mailer"
|
12
|
+
end
|
@@ -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::Default.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
|
+
response["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,146 @@
|
|
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
|
+
# = Sinatra::Mailer
|
17
|
+
#
|
18
|
+
# Adds an #email method to your email handlers, that receives a hash of
|
19
|
+
# values to create your email.
|
20
|
+
#
|
21
|
+
# For example:
|
22
|
+
#
|
23
|
+
# post "/signup" do
|
24
|
+
# # sign up the user, and then:
|
25
|
+
# email :to => @user.email,
|
26
|
+
# :from => "awesomeness@example.com",
|
27
|
+
# :subject => "Welcome to Awesomeness!",
|
28
|
+
# :body => haml(:some_template)
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# == Configuration
|
32
|
+
#
|
33
|
+
# This plugin is very dirty yet :) Since it's just a port to Sinatra of
|
34
|
+
# Merb::Mailer[merbivore.com/documentation/1.0/doc/rdoc/merb-mailer-1.0].
|
35
|
+
# So the configuration is not Sinatra-y, yet. But we'll get to that.
|
36
|
+
#
|
37
|
+
# == Using SMTP
|
38
|
+
#
|
39
|
+
# Sinatra::Mailer.config = {
|
40
|
+
# :host => 'smtp.yourserver.com',
|
41
|
+
# :port => '25',
|
42
|
+
# :user => 'user',
|
43
|
+
# :pass => 'pass',
|
44
|
+
# :auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
45
|
+
# :domain => "localhost.localdomain" # HELO domain provided by the client
|
46
|
+
# }
|
47
|
+
#
|
48
|
+
# == Using Gmail SMTP
|
49
|
+
#
|
50
|
+
# You need smtp-tls[http://github.com/ambethia/smtp-tls], a gem that improves
|
51
|
+
# Net::HTTP to add support for secure servers such as Gmail.
|
52
|
+
#
|
53
|
+
# require "smtp-tls"
|
54
|
+
#
|
55
|
+
# Sinatra::Mailer.config = {
|
56
|
+
# :host => 'smtp.gmail.com',
|
57
|
+
# :port => '587',
|
58
|
+
# :user => 'user@gmail.com',
|
59
|
+
# :pass => 'pass',
|
60
|
+
# :auth => :plain
|
61
|
+
# }
|
62
|
+
#
|
63
|
+
# Make sure that when you call your #email method you pass the
|
64
|
+
# +:text+ option and not +:body+.
|
65
|
+
#
|
66
|
+
# == Using sendmail
|
67
|
+
#
|
68
|
+
# Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
69
|
+
# Sinatra::Mailer.delivery_method = :sendmail
|
70
|
+
#
|
71
|
+
# == Credits
|
72
|
+
#
|
73
|
+
# This has been blatantly adapted from
|
74
|
+
# Merb::Mailer[merbivore.com/documentation/1.0/doc/rdoc/merb-mailer-1.0]
|
75
|
+
# so all credit is theirs, I just ported it to Sinatra.
|
76
|
+
module Mailer
|
77
|
+
class << self
|
78
|
+
attr_accessor :config, :delivery_method
|
79
|
+
end
|
80
|
+
|
81
|
+
def email(mail_options={})
|
82
|
+
Email.new(mail_options).deliver!
|
83
|
+
end
|
84
|
+
|
85
|
+
class Email
|
86
|
+
attr_accessor :mail, :config
|
87
|
+
|
88
|
+
# Sends the mail using sendmail.
|
89
|
+
def sendmail
|
90
|
+
sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+')
|
91
|
+
sendmail.puts @mail.to_s
|
92
|
+
sendmail.close
|
93
|
+
end
|
94
|
+
|
95
|
+
# Sends the mail using SMTP.
|
96
|
+
def net_smtp
|
97
|
+
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
|
98
|
+
config[:user], config[:pass], config[:auth]) { |smtp|
|
99
|
+
smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
# Delivers the mail with the specified delivery method, defaulting to
|
104
|
+
# net_smtp.
|
105
|
+
def deliver!
|
106
|
+
send(Mailer.delivery_method || :net_smtp)
|
107
|
+
end
|
108
|
+
|
109
|
+
# ==== Parameters
|
110
|
+
# file_or_files<File, Array[File]>:: File(s) to attach.
|
111
|
+
# filename<String>::
|
112
|
+
# type<~to_s>::
|
113
|
+
# The attachment MIME type. If left out, it will be determined from
|
114
|
+
# file_or_files.
|
115
|
+
# headers<String, Array>:: Additional attachment headers.
|
116
|
+
#
|
117
|
+
# ==== Raises
|
118
|
+
# ArgumentError::
|
119
|
+
# file_or_files was not a File or an Array of File instances.
|
120
|
+
def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
|
121
|
+
type = nil, headers = nil)
|
122
|
+
if file_or_files.is_a?(Array)
|
123
|
+
file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
|
124
|
+
else
|
125
|
+
raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
|
126
|
+
@mail.add_attachment_as(file_or_files, filename, type, headers)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# ==== Parameters
|
131
|
+
# o<Hash{~to_s => Object}>:: Configuration commands to send to MailFactory.
|
132
|
+
def initialize(o={})
|
133
|
+
self.config = Mailer.config || {:sendmail_path => '/usr/sbin/sendmail'}
|
134
|
+
o[:rawhtml] = o.delete(:html)
|
135
|
+
m = MailFactory.new()
|
136
|
+
o.each { |k,v| m.send "#{k}=", v }
|
137
|
+
@mail = m
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class EventContext
|
144
|
+
include Mailer
|
145
|
+
end
|
146
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foca-sinatra-ditties
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Nicol\xC3\xA1s Sanguinetti"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-02-13 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.
|
22
|
+
version: 0.9.0.4
|
23
23
|
version:
|
24
24
|
- !ruby/object:Gem::Dependency
|
25
25
|
name: mailfactory
|
@@ -48,10 +48,10 @@ extensions: []
|
|
48
48
|
extra_rdoc_files: []
|
49
49
|
|
50
50
|
files:
|
51
|
-
- README.
|
52
|
-
- lib/ditties.rb
|
53
|
-
- lib/ditties/authorization.rb
|
54
|
-
- lib/ditties/mailer.rb
|
51
|
+
- README.rdoc
|
52
|
+
- lib/sinatra/ditties.rb
|
53
|
+
- lib/sinatra/ditties/authorization.rb
|
54
|
+
- lib/sinatra/ditties/mailer.rb
|
55
55
|
has_rdoc: false
|
56
56
|
homepage: http://github.com/foca/sinatra-ditties
|
57
57
|
post_install_message:
|
data/README.markdown
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
Sinatra Ditties
|
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
|