sinatra-ditties 0.0.2
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 +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 +85 -0
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
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-ditties
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
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 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sinatra
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.3.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mailfactory
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.4.0
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: tlsmail
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: A series of plugins and useful helpers for the Sinatra web framework
|
46
|
+
email: contacto@nicolassanguinetti.info
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files: []
|
52
|
+
|
53
|
+
files:
|
54
|
+
- README.rdoc
|
55
|
+
- lib/sinatra/ditties.rb
|
56
|
+
- lib/sinatra/ditties/authorization.rb
|
57
|
+
- lib/sinatra/ditties/mailer.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/foca/sinatra-ditties
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project: sinatra-ditties
|
80
|
+
rubygems_version: 1.3.1
|
81
|
+
signing_key:
|
82
|
+
specification_version: 2
|
83
|
+
summary: A series of plugins and useful helpers for the Sinatra web framework
|
84
|
+
test_files: []
|
85
|
+
|