├── .document ├── .gitignore ├── .rspec ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.markdown ├── Rakefile ├── VERSION ├── dripper.gemspec ├── lib ├── dripper.rb └── dripper │ └── resque_scheduler.rb ├── spec ├── dripper_spec.rb └── spec_helper.rb └── test /.document: -------------------------------------------------------------------------------- 1 | lib/**/*.rb 2 | bin/* 3 | - 4 | features/**/*.feature 5 | LICENSE.txt 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # rcov generated 2 | coverage 3 | coverage.data 4 | 5 | # rdoc generated 6 | rdoc 7 | 8 | # yard generated 9 | doc 10 | .yardoc 11 | 12 | # bundler 13 | .bundle 14 | 15 | # jeweler generated 16 | pkg 17 | 18 | # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: 19 | # 20 | # * Create a file at ~/.gitignore 21 | # * Include files you want ignored 22 | # * Run: git config --global core.excludesfile ~/.gitignore 23 | # 24 | # After doing this, these files will be ignored in all your git projects, 25 | # saving you from having to 'pollute' every project you touch with them 26 | # 27 | # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line) 28 | # 29 | # For MacOS: 30 | # 31 | #.DS_Store 32 | 33 | # For TextMate 34 | #*.tmproj 35 | #tmtags 36 | 37 | # For emacs: 38 | #*~ 39 | #\#* 40 | #.\#* 41 | 42 | # For vim: 43 | #*.swp 44 | 45 | # For redcar: 46 | #.redcar 47 | 48 | # For rubinius: 49 | #*.rbc 50 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem 'activesupport', '>= 3.0' 4 | # Add dependencies required to use your gem here. 5 | # Example: 6 | # gem "activesupport", ">= 2.3.5" 7 | 8 | # Add dependencies to develop your gem here. 9 | # Include everything needed to run rake, tests, features, etc. 10 | group :development do 11 | gem "rspec", "~> 2.10" 12 | gem "rdoc", "~> 3.12" 13 | gem "jeweler", "~> 1.8.4" 14 | end 15 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | activesupport (3.2.6) 5 | i18n (~> 0.6) 6 | multi_json (~> 1.0) 7 | diff-lcs (1.1.3) 8 | git (1.2.5) 9 | i18n (0.6.0) 10 | jeweler (1.8.4) 11 | bundler (~> 1.0) 12 | git (>= 1.2.5) 13 | rake 14 | rdoc 15 | json (1.7.3) 16 | multi_json (1.3.6) 17 | rake (0.9.2.2) 18 | rdoc (3.12) 19 | json (~> 1.4) 20 | rspec (2.10.0) 21 | rspec-core (~> 2.10.0) 22 | rspec-expectations (~> 2.10.0) 23 | rspec-mocks (~> 2.10.0) 24 | rspec-core (2.10.1) 25 | rspec-expectations (2.10.0) 26 | diff-lcs (~> 1.1.3) 27 | rspec-mocks (2.10.1) 28 | 29 | PLATFORMS 30 | ruby 31 | 32 | DEPENDENCIES 33 | activesupport (>= 3.0) 34 | jeweler (~> 1.8.4) 35 | rdoc (~> 3.12) 36 | rspec (~> 2.10) 37 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Brennan Dunn 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | Dripper 2 | ======= 3 | 4 | ### Description 5 | 6 | Dripper is a lightweight library that makes it easy to setup a series of scheduled events to be fired off. It was originally written to help me setup [drip marketing campaigns](http://en.wikipedia.org/wiki/Drip_marketing) for [Planscope](https://planscope.io). 7 | 8 | ### Usage 9 | 10 | To install 11 | 12 | gem 'dripper' 13 | 14 | Define a class, and include a Dripper adapter (at the moment, only ResqueScheduler is supported) 15 | 16 | class UserDrip 17 | include Dripper::ResqueScheduler 18 | end 19 | 20 | Dripper needs a few things defined in order to work properly. First, a series of blocks to be executed. 21 | 22 | after 5.minutes do |user| 23 | UserMailer.welcome(user).deliver! 24 | end 25 | 26 | after 10.days do |user| 27 | UserMailer.trial_expiring_soon(user).deliver! 28 | end 29 | 30 | No presumptions are made about how you go about finding an instance to yield into an after block, so define a class level lookup method: 31 | 32 | def self.fetch_instance(options={}) 33 | User.find(options[:id]) 34 | end 35 | 36 | By default Dripper will fire a block after the offset you provide has elapsed, but sending emails in the middle of the night isn't always ideal. By specifying the offset in hours and minutes (relative to midnight), you can make sure your messages are sent at the ideal time. 37 | 38 | send_at [9, 0] # send at 9am 39 | 40 | You can also have Dripper not perform blocks on weekends. When enabled, any delayed job that's supposed to run on Saturday will run exactly 24 hours prior, on Friday. Likewise, jobs on Sunday will run on Monday. 41 | 42 | send_at [15, 0], weekends: false 43 | 44 | If you *don't* want certain blocks to be fired, the only way to do that now is to add the right conditions in your blocks. A good use case would be sending out "come back! we miss you!" emails - it's likely that you don't want those sent to paying customers. 45 | 46 | Here's what a complete implementation might look like: 47 | 48 | class UserDrip 49 | include Dripper::ResqueScheduler 50 | 51 | send_at [9, 0], weekends: false 52 | 53 | after 5.minutes do |user| 54 | # send "welcome!" email 55 | end 56 | 57 | after 1.day do |user| 58 | # send "getting started" email 59 | end 60 | 61 | after 27.days do |user| 62 | # send "trial expiring in 3 days" email 63 | end 64 | 65 | after 30.days do |user| 66 | # send "trial expired" email unless subscribed 67 | end 68 | 69 | after 60.days do |user| 70 | # send "come back!" email unless subscribed 71 | end 72 | 73 | end 74 | 75 | Usage is pretty simple. 76 | 77 | UserDrip.new(current_user).schedule! 78 | 79 | At the moment **you must provide an object that responds to #id and #created_at**. ID is used for the delayed job, and the timestamp is used to determine the contact schedule. 80 | 81 | Modifying the times that these blocks will be fired off is tricky. Because Dripper queues up a list of absolute timestamps within resque-scheduler, the only way to safely add, remove or change a schedule is to purge it from Redis and add it again. 82 | 83 | UserDrip.new(current_user).clear! 84 | UserDrip.new(current_user).schedule! 85 | 86 | If you're interested in seeing the list of times generated: `UserDrip.new(current_user).scheduled_times` 87 | 88 | 89 | ### Dependencies 90 | 91 | At the moment, ResqueScheduler is the only job scheduler supported. However, it would be pretty trivial to fork and add in a hook to your own scheduler (hint hint!) 92 | 93 | Additionally, ActiveSupport is required. 94 | 95 | ### Contributing to Dripper 96 | 97 | * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. 98 | * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. 99 | * Fork the project. 100 | * Start a feature/bugfix branch. 101 | * Commit and push until you are happy with your contribution. 102 | * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. 103 | * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. 104 | 105 | ### Copyright 106 | 107 | Copyright (c) 2012 Brennan Dunn. See LICENSE.txt for 108 | further details. 109 | 110 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | require 'rubygems' 4 | require 'bundler' 5 | begin 6 | Bundler.setup(:default, :development) 7 | rescue Bundler::BundlerError => e 8 | $stderr.puts e.message 9 | $stderr.puts "Run `bundle install` to install missing gems" 10 | exit e.status_code 11 | end 12 | require 'rake' 13 | 14 | require 'jeweler' 15 | Jeweler::Tasks.new do |gem| 16 | # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options 17 | gem.name = "dripper" 18 | gem.homepage = "http://github.com/brennandunn/dripper" 19 | gem.license = "MIT" 20 | gem.summary = %Q{A simple gem that makes creating drip email campaigns a breeze} 21 | gem.description = %Q{A simple gem that makes creating drip email campaigns a breeze} 22 | gem.email = "me@brennandunn.com" 23 | gem.authors = ["Brennan Dunn"] 24 | # dependencies defined in Gemfile 25 | end 26 | Jeweler::RubygemsDotOrgTasks.new 27 | 28 | require 'rspec/core' 29 | require 'rspec/core/rake_task' 30 | RSpec::Core::RakeTask.new(:spec) do |spec| 31 | spec.pattern = FileList['spec/**/*_spec.rb'] 32 | end 33 | 34 | RSpec::Core::RakeTask.new(:rcov) do |spec| 35 | spec.pattern = 'spec/**/*_spec.rb' 36 | spec.rcov = true 37 | end 38 | 39 | task :default => :spec 40 | 41 | require 'rdoc/task' 42 | Rake::RDocTask.new do |rdoc| 43 | version = File.exist?('VERSION') ? File.read('VERSION') : "" 44 | 45 | rdoc.rdoc_dir = 'rdoc' 46 | rdoc.title = "dripper #{version}" 47 | rdoc.rdoc_files.include('README*') 48 | rdoc.rdoc_files.include('lib/**/*.rb') 49 | end 50 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.1.2 -------------------------------------------------------------------------------- /dripper.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by jeweler 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | 6 | Gem::Specification.new do |s| 7 | s.name = "dripper" 8 | s.version = "0.1.1" 9 | 10 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 11 | s.authors = ["Brennan Dunn"] 12 | s.date = "2012-07-13" 13 | s.description = "A simple gem that makes creating drip email campaigns a breeze" 14 | s.email = "me@brennandunn.com" 15 | s.extra_rdoc_files = [ 16 | "LICENSE.txt", 17 | "README.markdown" 18 | ] 19 | s.files = [ 20 | ".document", 21 | ".rspec", 22 | "Gemfile", 23 | "Gemfile.lock", 24 | "LICENSE.txt", 25 | "README.markdown", 26 | "Rakefile", 27 | "VERSION", 28 | "dripper.gemspec", 29 | "lib/dripper.rb", 30 | "lib/dripper/resque_scheduler.rb", 31 | "spec/dripper_spec.rb", 32 | "spec/spec_helper.rb" 33 | ] 34 | s.homepage = "http://github.com/brennandunn/dripper" 35 | s.licenses = ["MIT"] 36 | s.require_paths = ["lib"] 37 | s.rubygems_version = "1.8.11" 38 | s.summary = "A simple gem that makes creating drip email campaigns a breeze" 39 | 40 | if s.respond_to? :specification_version then 41 | s.specification_version = 3 42 | 43 | if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then 44 | s.add_runtime_dependency(%q, [">= 3.0"]) 45 | s.add_development_dependency(%q, ["~> 2.10"]) 46 | s.add_development_dependency(%q, ["~> 3.12"]) 47 | s.add_development_dependency(%q, ["~> 1.8.4"]) 48 | else 49 | s.add_dependency(%q, [">= 3.0"]) 50 | s.add_dependency(%q, ["~> 2.10"]) 51 | s.add_dependency(%q, ["~> 3.12"]) 52 | s.add_dependency(%q, ["~> 1.8.4"]) 53 | end 54 | else 55 | s.add_dependency(%q, [">= 3.0"]) 56 | s.add_dependency(%q, ["~> 2.10"]) 57 | s.add_dependency(%q, ["~> 3.12"]) 58 | s.add_dependency(%q, ["~> 1.8.4"]) 59 | end 60 | end 61 | 62 | -------------------------------------------------------------------------------- /lib/dripper.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/all' 2 | require 'dripper/resque_scheduler' 3 | 4 | module Dripper 5 | 6 | def self.included(klass) 7 | klass.extend ClassMethods 8 | end 9 | 10 | def initialize(instance) 11 | raise ArgumentError, "The object must respond to #id" unless instance.respond_to?(:id) 12 | raise ArgumentError, "The object must respond to #created_at" unless instance.respond_to?(:created_at) 13 | @instance = instance 14 | end 15 | 16 | def starting_time 17 | @instance.created_at # Fix this 18 | end 19 | 20 | def offset_time(offset) 21 | calculated_time = starting_time + offset 22 | options = self.class.send_at_options 23 | unless options[:weekends] 24 | if calculated_time.wday == 6 # Saturday 25 | calculated_time = calculated_time - 1.day 26 | elsif calculated_time.wday == 0 # Sunday 27 | calculated_time = calculated_time + 1.day 28 | end 29 | end 30 | calculated_time 31 | end 32 | 33 | def scheduled_times 34 | offset = self.class.send_at_offset 35 | self.class.after_blocks.map do |b| 36 | t = offset_time(b[:offset]) 37 | if offset and b[:offset] >= 1.day 38 | t = t.beginning_of_day + offset[0].hours + offset[1].minutes 39 | end 40 | t 41 | end 42 | end 43 | 44 | def schedule! 45 | scheduled_times.each do |time| 46 | enqueue(time) 47 | end 48 | end 49 | 50 | def enqueue(time) 51 | # nothing here 52 | end 53 | 54 | module ClassMethods 55 | def send_at_offset ; @send_at_offset ; end 56 | def send_at_options ; @send_at_options || { :weekends => true } ; end 57 | 58 | def after_blocks 59 | @after_blocks ||= [] 60 | end 61 | 62 | def after(offset, &block) 63 | after_blocks << { offset: offset, block: block } 64 | end 65 | 66 | def send_at(offset_array, options={}) 67 | @send_at_offset = offset_array 68 | @send_at_options = { :weekends => true }.merge(options) 69 | end 70 | 71 | def perform(options={}) 72 | position = options.delete(:position) 73 | if found = after_blocks[position] 74 | found[:block].call(fetch_instance(options)) 75 | end 76 | end 77 | 78 | def fetch_instance(options={}) 79 | # nothing here 80 | end 81 | 82 | end 83 | 84 | end 85 | -------------------------------------------------------------------------------- /lib/dripper/resque_scheduler.rb: -------------------------------------------------------------------------------- 1 | module Dripper 2 | module ResqueScheduler 3 | def self.included(klass) 4 | klass.send :include, Dripper 5 | klass.send :include, InstanceMethods 6 | end 7 | 8 | module InstanceMethods 9 | 10 | def enqueue(time) 11 | Resque.enqueue_at(time, self.class, id: @instance.id) 12 | end 13 | 14 | def clear! 15 | Resque.remove_delayed(self.class, id: @instance.id) 16 | end 17 | 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /spec/dripper_spec.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path(File.dirname(__FILE__) + '/spec_helper') 2 | 3 | describe "Dripper" do 4 | subject { Class.new { include Dripper } } 5 | let!(:now) { Time.now } 6 | let(:instance) { stub(id: 1, created_at: now) } 7 | 8 | it 'requires an id and timestamp' do 9 | expect { subject.new(stub()) }.to raise_error(ArgumentError) 10 | expect { subject.new(instance) }.to_not raise_error 11 | end 12 | 13 | describe 'choosing a time to schedule events' do 14 | 15 | it 'uses the timestamp plus the after amount when send_at is not defined' do 16 | subject.after(1.day) {} 17 | 18 | drip = subject.new(instance) 19 | drip.scheduled_times.should == [now + 1.day] 20 | end 21 | 22 | describe 'generating a schedule' do 23 | it 'ignores send_at when the after amount is less than a day' do 24 | subject.send_at [9, 0] 25 | subject.after(5.minutes) {} 26 | 27 | drip = subject.new(instance) 28 | drip.scheduled_times.should == [now + 5.minutes] 29 | end 30 | 31 | it 'schedules for the number of days out plus the send_at offset' do 32 | subject.send_at [9, 0] 33 | subject.after(2.days) {} 34 | 35 | drip = subject.new(instance) 36 | drip.scheduled_times.should == [(now + 2.days).beginning_of_day + 9.hours] 37 | end 38 | 39 | describe 'when skipping weekends' do 40 | before do 41 | subject.send_at [9, 0], weekends: false 42 | end 43 | 44 | it 'sends on a Friday for Saturday events' do 45 | subject.after(5.days) {} 46 | 47 | new_instance = stub(id: 1, created_at: Time.now.beginning_of_week) 48 | drip = subject.new(new_instance) 49 | drip.scheduled_times.should == [(new_instance.created_at + 4.days).beginning_of_day + 9.hours] # Friday, 9am 50 | end 51 | it 'sends on a Monday for Sunday events' do 52 | subject.after(6.days) {} 53 | 54 | new_instance = stub(id: 1, created_at: Time.now.beginning_of_week) 55 | drip = subject.new(new_instance) 56 | drip.scheduled_times.should == [(new_instance.created_at + 7.days).beginning_of_day + 9.hours] # Monday, 9am 57 | 58 | end 59 | end 60 | end 61 | 62 | end 63 | 64 | describe 'performing an offset action' do 65 | let(:drip) { subject.new(instance) } 66 | 67 | before do 68 | subject.after 1.day do |instance| 69 | instance.foo 70 | end 71 | end 72 | 73 | it 'gracefully fails when an after block cannot be found' do 74 | expect { subject.perform position: 4, id: 1 }.to_not raise_error 75 | end 76 | 77 | it 'calls the block and supplies the instance' do 78 | instance.should_receive :foo 79 | subject.should_receive(:fetch_instance).and_return(instance) 80 | subject.perform position: 0, id: 1 81 | end 82 | end 83 | 84 | describe 'Using with resque-scheduler' do 85 | subject { Class.new { include Dripper::ResqueScheduler } } 86 | before { class Resque ; end } 87 | 88 | it 'attempts to enqueue each job' do 89 | subject.after(1.day) {} 90 | drip = subject.new(instance) 91 | offset = drip.scheduled_times.first 92 | Resque.should_receive(:enqueue_at).with(offset, subject, { id: instance.id }) 93 | 94 | drip.schedule! 95 | end 96 | 97 | it 'purges any scheduled jobs' do 98 | Resque.should_receive(:remove_delayed).with(subject, { id: instance.id }) 99 | 100 | drip = subject.new(instance) 101 | drip.clear! 102 | end 103 | 104 | end 105 | 106 | end 107 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 2 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 3 | require 'bundler/setup' 4 | require 'rspec' 5 | require 'dripper' 6 | 7 | Bundler.require 8 | 9 | # Requires supporting files with custom matchers and macros, etc, 10 | # in ./support/ and its subdirectories. 11 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} 12 | 13 | RSpec.configure do |config| 14 | end 15 | -------------------------------------------------------------------------------- /test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brennandunn/dripper/e1aec89e7c884613e7cfb44d58398c2d46af9858/test --------------------------------------------------------------------------------