Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsz committed Jul 4, 2013
0 parents commit a3293cb
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in mmailer.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2013 Daniel Szmulewicz

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
@@ -0,0 +1,29 @@
# Mmailer

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'mmailer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mmailer

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require "bundler/gem_tasks"
8 changes: 8 additions & 0 deletions lib/mmailer.rb
@@ -0,0 +1,8 @@
require "mmailer/version"

module Mmailer
require 'mail_helper'
require 'master_helper'
require 'worker'
require 'master'
end
7 changes: 7 additions & 0 deletions lib/mmailer/client.rb
@@ -0,0 +1,7 @@
require 'drb/drb'

uri = ARGV.shift
obj = DRbObject.new_with_uri(uri)
obj.up?
rc = obj.up
puts rc
64 changes: 64 additions & 0 deletions lib/mmailer/mail_helper.rb
@@ -0,0 +1,64 @@
require 'mail'
require 'erb'

class MailHelper
attr_reader :template, :title

def initialize(args)
set_provider args.fetch(:provider, :mailchimp)
@template=args[:template]
@title=args[:title]
end

def set_provider(provider)

mandrill_config = Proc.new do
delivery_method :smtp, {
:port => 587,
:address => ENV['MANDRILL_SMTP'],
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY']
}
end

gmail_config = Proc.new do
delivery_method :smtp, {
:port => 587,
:address => ENV['GMAIL_SMTP'],
:user_name => ENV['GMAIL_USERNAME'],
:password => ENV['GMAIL_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
end

providers = {google: gmail_config, mailchimp: mandrill_config}

Mail.defaults(&providers[provider])

end

def send_email(user)

mail = Mail.new do
to user.email
from 'Etsy Fu <info@shopi-fu.com>'
end
mail.subject = title

text_part = Mail::Part.new
text_part.body=ERB.new(File.read("#{Bundler.root}/lib/mailer/templates/#{template}.txt.erb")).result(binding)

html_part = Mail::Part.new
html_part.content_type='text/html; charset=UTF-8'
html_part.body=ERB.new(File.read("#{Bundler.root}/lib/mailer/templates/#{template}.html.erb")).result(binding)

mail.text_part = text_part
mail.html_part = html_part
#when Non US-ASCII detected and no charset defined. Defaulting to UTF-8, set your own if this is incorrect.
mail.charset = 'UTF-8'
#puts mail.to_s
mail.deliver!

end
end
9 changes: 9 additions & 0 deletions lib/mmailer/master.rb
@@ -0,0 +1,9 @@

uri = ARGV.shift
DRb.start_service(uri, MasterHelper.new)
puts DRb.uri
begin
DRb.thread.join
rescue Interrupt
abort "Shutting down..."
end
60 changes: 60 additions & 0 deletions lib/mmailer/master_helper.rb
@@ -0,0 +1,60 @@

class MasterHelper
attr_accessor :stream, :worker, :machine

def initialize
@stream = $stdout
self.machine = MicroMachine.new(:stopped)
machine.when(:start, :stopped => :started)
machine.when(:resume, :paused => :started)
machine.when(:stop, :started => :stopped, :paused => :stopped)
machine.when(:pause, :started => :paused)
end

def display_state
stream.puts state
end

def state
machine.state
end

def puts(str)
stream.puts(str)
end

def up?
true
end

def resume
if machine.trigger(:resume)
display_state
machine.state
end
end

def stop
if machine.trigger(:stop)
display_state
machine.state
end
end

def pause
if machine.trigger(:pause)
display_state
machine.state
end
end

def start(from=0)
if machine.trigger(:start)
puts "starting from #{from}"
@worker = Thread.new(from) do | from |
worker = Worker.new(from)
end
end
end

end
3 changes: 3 additions & 0 deletions lib/mmailer/version.rb
@@ -0,0 +1,3 @@
module Mmailer
VERSION = "0.0.1"
end
41 changes: 41 additions & 0 deletions lib/mmailer/worker.rb
@@ -0,0 +1,41 @@
class Worker
attr_reader :obj, :mailHelper, :collection, :from

def initialize(from)
@from = from
@obj = DRbObject.new_with_uri('druby://localhost:8080')
@mailHelper = MailHelper.new
load_collection
exec
end

def exec
while not collection.empty? do
case obj.state
when :paused
sleep 1
when :started
index ||= from; index += 1
user = collection.shift
obj.puts "#{index}: #{user.email}"
mailHelper.send_email(user) if not user.email.nil?
sleep rand(6)
if index % 48 == 0
obj.puts "48th element, going to sleep for an hour"
sleep 3600
end
when :stopped
break
end
end
obj.puts "Exiting worker"
Thread.exit
end

def load_collection
@collection = User.active
collection.shift(from)
obj.puts "Loaded #{collection.count} entries"
end

end
24 changes: 24 additions & 0 deletions mmailer.gemspec
@@ -0,0 +1,24 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mmailer/version'

Gem::Specification.new do |spec|
spec.name = "mmailer"
spec.version = Mmailer::VERSION
spec.authors = ["Daniel Szmulewicz"]
spec.email = ["danielsz@freeshell.org"]
spec.description = %q{TODO: Write a gem description}
spec.summary = %q{Mass mailing the Ruby way}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
end

0 comments on commit a3293cb

Please sign in to comment.