Permalink
Browse files

emails / refactor a little

  • Loading branch information...
kastner committed Nov 4, 2011
1 parent b7c9198 commit 534050bc0749dd42463af9fc75a79e26631fffb8
Showing with 128 additions and 25 deletions.
  1. +2 −0 Gemfile
  2. +15 −0 Gemfile.lock
  3. +1 −0 Procfile
  4. +13 −0 README.md
  5. +59 −0 clock.rb
  6. +12 −25 droppy.rb
  7. +26 −0 droppy_box.rb
View
@@ -2,3 +2,5 @@ source :gemcutter
gem 'sinatra', '1.3.1'
gem 'thin', '1.2.8'
gem 'dropbox-sdk', '1.1'
+gem 'clockwork', '0.3.0'
+gem 'pony', '1.3'
View
@@ -1,11 +1,21 @@
GEM
remote: http://rubygems.org/
specs:
+ clockwork (0.3.0)
daemons (1.1.4)
dropbox-sdk (1.1)
json
eventmachine (0.12.10)
+ i18n (0.6.0)
json (1.6.1)
+ mail (2.3.0)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ polyglot (0.3.2)
+ pony (1.3)
+ mail (> 2.0)
rack (1.3.5)
rack-protection (1.1.4)
rack
@@ -18,11 +28,16 @@ GEM
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
PLATFORMS
ruby
DEPENDENCIES
+ clockwork (= 0.3.0)
dropbox-sdk (= 1.1)
+ pony (= 1.3)
sinatra (= 1.3.1)
thin (= 1.2.8)
View
@@ -1,2 +1,3 @@
web: bundle exec ruby droppy.rb -p $PORT
+clock: bundle exec clockwork clock.rb
View
@@ -76,6 +76,19 @@ AUTH_USERNAME=user
AUTH_PASSWORD=pass
```
+
+Sending Email
+-------------
+
+So you want to alert someone when a new file is there?
+
+Add these to the environment settings (either `.env`, or `heroku config:add`):
+
+* `EMAIL_NOTIFY_LIST` email address to send "to"
+* `GMAIL_USER` Gmail username (with @gmail.com, or whathaveyou)
+* `GMAIL_PASS` Gmail password
+* `DROPPY_URL` Web address for the email to link to
+
Debugging
---------
View
@@ -0,0 +1,59 @@
+require 'clockwork'
+require 'pony'
+
+require './droppy_box'
+
+include Clockwork
+
+@files = {}
+@droppy_box = DroppyBox.new
+
+Pony.options = {
+ :via => :smtp,
+ :from => ENV["GMAIL_USER"],
+ :via_options => {
+ :address => 'smtp.gmail.com',
+ :port => '587',
+ :enable_starttls_auto => true,
+ :user_name => ENV["GMAIL_USER"],
+ :password => ENV["GMAIL_PASS"],
+ :authentication => :plain,
+ :domain => "localhost.localdomain"
+ }
+}
+
+handler do |job|
+ old_files = @files
+ begin
+ @files = @droppy_box.client.metadata("/" + @droppy_box.path)
+
+ # don't email the first time you're populating
+ next if @files.key?("hash") && old_files.empty?
+ rescue DropboxAuthError
+ next
+ end
+
+ if old_files["hash"] != @files["hash"]
+ # something changed...
+ # look for new files
+ new_files = (@files["contents"] - old_files["contents"]).map do |f|
+ f["path"].gsub(/^#{@files["path"]}\//, '')
+ end
+
+ if new_files && ! new_files.empty?
+ subject = "some New file#{"s" if new_files.count > 1} ready to download: #{new_files.join(", ")}"
+ body = "Get them here:"
+ new_files.each do |file|
+ body += "\n\t#{ENV["DROPPY_URL"]}/get/#{file}"
+ end
+
+ puts %Q{Sending email with subject: "#{subject}" to #{ENV["EMAIL_NOTIFY_LIST"]}}
+
+ puts Pony.mail(:to => ENV["EMAIL_NOTIFY_LIST"], :subject => subject, :body => body)
+ end
+ end
+end
+
+if ENV["EMAIL_NOTIFY_LIST"]
+ every(10.seconds, 'check_for_new_files')
+end
View
@@ -1,5 +1,5 @@
require 'sinatra'
-require 'dropbox_sdk'
+require './droppy_box'
if ENV["AUTH_USERNAME"] && ENV["AUTH_PASSWORD"]
use Rack::Auth::Basic do |username, password|
@@ -15,42 +15,29 @@
def base_url
@base_url ||= "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}"
end
-
- def dropbox_path
- @dropbox_path ||= (ENV["DROPBOX_PATH"] || "droppy")
- end
end
-# Get the dropbox auth token
before do
@nav = {
'/' => 'Files',
'/about' => 'About'
}
-
- @client = nil
- @session = DropboxSession.new(ENV["DROPBOX_KEY"], ENV["DROPBOX_SECRET"])
- if ENV["DROPBOX_TOKEN"]
- (key, secret) = ENV["DROPBOX_TOKEN"].split(":")
- @session.set_access_token(key, secret)
- db_type = (ENV["DROPBOX_TYPE"]) ? ENV["DROPBOX_TYPE"].to_sym : :dropbox
- @client = DropboxClient.new(@session, db_type)
- end
+ @droppy_box = DroppyBox.new
- start_auth_flow unless @client or request.path.match(%r{/auth})
+ start_auth_flow unless @droppy_box.client? or request.path.match(%r{/auth})
end
def start_auth_flow
- @session.clear_access_token
- request_token = @session.get_request_token
+ @droppy_box.session.clear_access_token
+ request_token = @droppy_box.session.get_request_token
session[:request_token] = request_token
- redirect @session.get_authorize_url(url('/auth'))
+ redirect @droppy_box.session.get_authorize_url(url('/auth'))
end
get '/' do
begin
- @files = @client.metadata("/" + dropbox_path)
+ @files = @droppy_box.client.metadata("/" + @droppy_box.path)
erb :files
rescue DropboxAuthError
start_auth_flow
@@ -62,16 +49,16 @@ def start_auth_flow
end
get '/get/:file' do
- path = File.join(dropbox_path, params[:file])
- redirect @client.media(File.join(dropbox_path, params[:file]))["url"]
+ path = File.join(@droppy_box.path, params[:file])
+ redirect @droppy_box.client.media(File.join(@droppy_box.path, params[:file]))["url"]
end
get '/auth' do
if request_token = session[:request_token]
- @session.set_request_token(request_token.key, request_token.secret)
- token = @session.get_access_token
+ @droppy_box.session.set_request_token(request_token.key, request_token.secret)
+ token = @droppy_box.session.get_access_token
erb "Here is your DROPBOX_TOKEN=#{token.key}:#{token.secret}"
else
erb "Sorry, must have expired"
end
-end
+end
View
@@ -0,0 +1,26 @@
+require 'dropbox_sdk'
+
+class DroppyBox
+ def session
+ @session ||= DropboxSession.new(ENV["DROPBOX_KEY"], ENV["DROPBOX_SECRET"])
+ end
+
+ def client?
+ client
+ end
+
+ def client
+ return nil unless ENV["DROPBOX_TOKEN"]
+
+ @client ||= begin
+ (key, secret) = ENV["DROPBOX_TOKEN"].split(":")
+ session.set_access_token(key, secret)
+ db_type = (ENV["DROPBOX_TYPE"]) ? ENV["DROPBOX_TYPE"].to_sym : :dropbox
+ DropboxClient.new(session, db_type)
+ end
+ end
+
+ def path
+ @path ||= (ENV["DROPBOX_PATH"] || "droppy")
+ end
+end

0 comments on commit 534050b

Please sign in to comment.