sr-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 +28 -0
- data/lib/sinatra/ditties.rb +12 -0
- data/lib/sinatra/ditties/authorization.rb +60 -0
- data/lib/sinatra/ditties/mailer.rb +145 -0
- metadata +9 -8
- data/README.markdown +0 -74
data/README.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= Sinatra Ditties
|
2
|
+
|
3
|
+
All those handy tunes in one elegant package.
|
4
|
+
|
5
|
+
== License
|
6
|
+
|
7
|
+
(The MIT License)
|
8
|
+
|
9
|
+
Copyright (c) 2008-2009 {Nicolás Sanguinetti}[http://nicolassanguinetti.info]
|
10
|
+
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
12
|
+
a copy of this software and associated documentation files (the
|
13
|
+
'Software'), to deal in the Software without restriction, including
|
14
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
15
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
16
|
+
permit persons to whom the Software is furnished to do so, subject to
|
17
|
+
the following conditions:
|
18
|
+
|
19
|
+
The above copyright notice and this permission notice shall be
|
20
|
+
included in all copies or substantial portions of the Software.
|
21
|
+
|
22
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
23
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
24
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
25
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
26
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
27
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
28
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -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,60 @@
|
|
1
|
+
module Sinatra
|
2
|
+
# HTTP Authorization helpers for Sinatra.
|
3
|
+
#
|
4
|
+
# In your helpers module, include Sinatra::Authorization and then define
|
5
|
+
# an #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/about] and
|
12
|
+
# {Christopher 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
|
+
def auth
|
48
|
+
@auth ||= Rack::Auth::Basic::Request.new(request.env)
|
49
|
+
end
|
50
|
+
|
51
|
+
def unauthorized!(realm=authorization_realm)
|
52
|
+
response["WWW-Authenticate"] = %(Basic realm="#{realm}")
|
53
|
+
throw :halt, [ 401, 'Authorization Required' ]
|
54
|
+
end
|
55
|
+
|
56
|
+
def bad_request!
|
57
|
+
throw :halt, [ 400, 'Bad Request' ]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# Shamelssly stolen from Merb::Mailer
|
2
|
+
# http://merbivore.com
|
3
|
+
|
4
|
+
require 'net/smtp'
|
5
|
+
require 'mailfactory'
|
6
|
+
require 'tlsmail'
|
7
|
+
|
8
|
+
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
|
9
|
+
|
10
|
+
class MailFactory
|
11
|
+
attr_reader :html, :text
|
12
|
+
end
|
13
|
+
|
14
|
+
module Sinatra
|
15
|
+
# = Sinatra::Mailer
|
16
|
+
#
|
17
|
+
# Adds an #email method to your email handlers, that receives a hash of
|
18
|
+
# values to create your email.
|
19
|
+
#
|
20
|
+
# For example:
|
21
|
+
#
|
22
|
+
# post "/signup" do
|
23
|
+
# # sign up the user, and then:
|
24
|
+
# email :to => @user.email,
|
25
|
+
# :from => "awesomeness@example.com",
|
26
|
+
# :subject => "Welcome to Awesomeness!",
|
27
|
+
# :body => haml(:some_template)
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# == Configuration
|
31
|
+
#
|
32
|
+
# This plugin is very dirty yet :) Since it's just a port to Sinatra of
|
33
|
+
# Merb::Mailer[merbivore.com/documentation/1.0/doc/rdoc/merb-mailer-1.0].
|
34
|
+
# So the configuration is not Sinatra-y, yet. But we'll get to that.
|
35
|
+
#
|
36
|
+
# == Using SMTP
|
37
|
+
#
|
38
|
+
# Sinatra::Mailer.config = {
|
39
|
+
# :host => 'smtp.yourserver.com',
|
40
|
+
# :port => '25',
|
41
|
+
# :user => 'user',
|
42
|
+
# :pass => 'pass',
|
43
|
+
# :auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
44
|
+
# :domain => "localhost.localdomain" # HELO domain provided by the client
|
45
|
+
# }
|
46
|
+
#
|
47
|
+
# == Using Gmail SMTP
|
48
|
+
#
|
49
|
+
# You need smtp-tls[http://github.com/ambethia/smtp-tls], a gem that improves
|
50
|
+
# Net::HTTP to add support for secure servers such as Gmail.
|
51
|
+
#
|
52
|
+
# require "smtp-tls"
|
53
|
+
#
|
54
|
+
# Sinatra::Mailer.config = {
|
55
|
+
# :host => 'smtp.gmail.com',
|
56
|
+
# :port => '587',
|
57
|
+
# :user => 'user@gmail.com',
|
58
|
+
# :pass => 'pass',
|
59
|
+
# :auth => :plain
|
60
|
+
# }
|
61
|
+
#
|
62
|
+
# Make sure that when you call your #email method you pass the
|
63
|
+
# +:text+ option and not +:body+.
|
64
|
+
#
|
65
|
+
# == Using sendmail
|
66
|
+
#
|
67
|
+
# Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
68
|
+
# Sinatra::Mailer.delivery_method = :sendmail
|
69
|
+
#
|
70
|
+
# == Credits
|
71
|
+
#
|
72
|
+
# This has been blatantly adapted from
|
73
|
+
# Merb::Mailer[merbivore.com/documentation/1.0/doc/rdoc/merb-mailer-1.0]
|
74
|
+
# so all credit is theirs, I just ported it to Sinatra.
|
75
|
+
module Mailer
|
76
|
+
class << self
|
77
|
+
attr_accessor :config, :delivery_method
|
78
|
+
end
|
79
|
+
|
80
|
+
def email(mail_options={})
|
81
|
+
Email.new(mail_options).deliver!
|
82
|
+
end
|
83
|
+
|
84
|
+
class Email
|
85
|
+
attr_accessor :mail, :config
|
86
|
+
|
87
|
+
# Sends the mail using sendmail.
|
88
|
+
def sendmail
|
89
|
+
sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+')
|
90
|
+
sendmail.puts @mail.to_s
|
91
|
+
sendmail.close
|
92
|
+
end
|
93
|
+
|
94
|
+
# Sends the mail using SMTP.
|
95
|
+
def net_smtp
|
96
|
+
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
|
97
|
+
config[:user], config[:pass], config[:auth]) { |smtp|
|
98
|
+
smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
# Delivers the mail with the specified delivery method, defaulting to
|
103
|
+
# net_smtp.
|
104
|
+
def deliver!
|
105
|
+
send(Mailer.delivery_method || :net_smtp)
|
106
|
+
end
|
107
|
+
|
108
|
+
# ==== Parameters
|
109
|
+
# file_or_files<File, Array[File]>:: File(s) to attach.
|
110
|
+
# filename<String>::
|
111
|
+
# type<~to_s>::
|
112
|
+
# The attachment MIME type. If left out, it will be determined from
|
113
|
+
# file_or_files.
|
114
|
+
# headers<String, Array>:: Additional attachment headers.
|
115
|
+
#
|
116
|
+
# ==== Raises
|
117
|
+
# ArgumentError::
|
118
|
+
# file_or_files was not a File or an Array of File instances.
|
119
|
+
def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
|
120
|
+
type = nil, headers = nil)
|
121
|
+
if file_or_files.is_a?(Array)
|
122
|
+
file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
|
123
|
+
else
|
124
|
+
raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
|
125
|
+
@mail.add_attachment_as(file_or_files, filename, type, headers)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# ==== Parameters
|
130
|
+
# o<Hash{~to_s => Object}>:: Configuration commands to send to MailFactory.
|
131
|
+
def initialize(o={})
|
132
|
+
self.config = Mailer.config || {:sendmail_path => '/usr/sbin/sendmail'}
|
133
|
+
o[:rawhtml] = o.delete(:html)
|
134
|
+
m = MailFactory.new()
|
135
|
+
o.each { |k,v| m.send "#{k}=", v }
|
136
|
+
@mail = m
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
class EventContext
|
143
|
+
include Mailer
|
144
|
+
end
|
145
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sr-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"
|
8
|
+
- Simon Rozet
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date:
|
13
|
+
date: 2009-02-13 00:00:00 -08:00
|
13
14
|
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
@@ -48,11 +49,11 @@ extensions: []
|
|
48
49
|
extra_rdoc_files: []
|
49
50
|
|
50
51
|
files:
|
51
|
-
- README.
|
52
|
-
- lib/ditties.rb
|
53
|
-
- lib/ditties/authorization.rb
|
54
|
-
- lib/ditties/mailer.rb
|
55
|
-
has_rdoc:
|
52
|
+
- README.rdoc
|
53
|
+
- lib/sinatra/ditties.rb
|
54
|
+
- lib/sinatra/ditties/authorization.rb
|
55
|
+
- lib/sinatra/ditties/mailer.rb
|
56
|
+
has_rdoc: true
|
56
57
|
homepage: http://github.com/foca/sinatra-ditties
|
57
58
|
post_install_message:
|
58
59
|
rdoc_options: []
|
@@ -73,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
74
|
version:
|
74
75
|
requirements: []
|
75
76
|
|
76
|
-
rubyforge_project:
|
77
|
+
rubyforge_project: sinatra-ditties
|
77
78
|
rubygems_version: 1.2.0
|
78
79
|
signing_key:
|
79
80
|
specification_version: 2
|
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
|