Permalink
Browse files

Added email worker.

  • Loading branch information...
1 parent bf73e17 commit a305349a810a5d7dc4bc3c14cac471f8281f67ef @treeder treeder committed Aug 22, 2012
Showing with 112 additions and 28 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 Gemfile
  3. +6 −11 Gemfile.lock
  4. +12 −2 config.rb
  5. +8 −2 config_sample.yml
  6. +7 −1 models/contact.rb
  7. +15 −0 test_email.rb
  8. +1 −10 views/index.erb
  9. +0 −1 views/layout.erb
  10. +56 −0 workers/email_worker.rb
  11. +5 −0 workers/email_worker.worker
View
1 .gitignore
@@ -1,2 +1,3 @@
.idea/*
config.yml
+iron.json
View
2 Gemfile
@@ -2,7 +2,7 @@ source 'http://rubygems.org'
gem 'sinatra', :require=>'sinatra/base'
-gem 'iron_worker'
+gem 'iron_worker_ng'
gem 'iron_mq'
gem 'iron_cache'
View
17 Gemfile.lock
@@ -1,20 +1,15 @@
GEM
remote: http://rubygems.org/
specs:
- iron_cache (1.1.1)
+ iron_cache (1.1.2)
iron_core (>= 0.2.0)
iron_core (0.3.3)
rest (>= 2.0.0)
iron_mq (2.1.1)
iron_core (>= 0.2.0)
- iron_worker (2.3.15)
- bundler
- bundler
- rest
- rest
- rest-client
- rest-client
- zip
+ iron_worker_ng (0.10.0)
+ bundler (> 1.0.0)
+ iron_core (>= 0.3.0)
zip
jsonable (0.0.3)
macaddr (1.6.1)
@@ -32,7 +27,7 @@ GEM
rest-client (>= 0.3.0)
rest-client (1.6.7)
mime-types (>= 1.16)
- sinatra (1.3.2)
+ sinatra (1.3.3)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
@@ -49,7 +44,7 @@ PLATFORMS
DEPENDENCIES
iron_cache
iron_mq
- iron_worker
+ iron_worker_ng
jsonable
rack-flash3
rest
View
14 config.rb
@@ -1,5 +1,6 @@
require 'yaml'
require 'uber_config'
+require 'iron_worker_ng'
@config = UberConfig.load
UberConfig.symbolize_keys!(@config)
@@ -9,7 +10,16 @@
#@config[:iron][:token] ||= ENV['IRON_TOKEN'] || ENV['IRON_WORKER_TOKEN']
#@config[:iron][:project_id] ||= ENV['IRON_PROJECT_ID'] || ENV['IRON_WORKER_PROJECT_ID']
-ENV['IRON_WORKER_TOKEN'] ||= @config[:iron][:token]
-ENV['IRON_WORKER_PROJECT_ID'] ||= @config[:iron][:project_id]
+ENV['IRON_TOKEN'] ||= @config[:iron][:token]
+ENV['IRON_PROJECT_ID'] ||= @config[:iron][:project_id]
+module SingletonConfig
+ def self.config=(c)
+ @config = c
+ end
+ def self.config
+ @config
+ end
+end
+SingletonConfig.config = @config
View
10 config_sample.yml
@@ -1,4 +1,10 @@
iron:
- project_id: my_project_id
-
+ project_id: MY PROJECT ID
+ # token: optional if not set in ENV or ~/.iron.json
+email:
+ host: smtp.sendgrid.net
+ domain: YOUR DOMAIN, eg: yourbiz.com
+ port: 587
+ username: USERNAME
+ password: PASS
View
8 models/contact.rb
@@ -2,6 +2,12 @@ class Contact
include Jsonable
include Idable
- attr_accessor :email, :name, :company, :salesforce_id, :status, :action, :result
+ attr_accessor :email,
+ :name,
+ :company,
+ :salesforce_id,
+ :status,
+ :action,
+ :result
end
View
15 test_email.rb
@@ -0,0 +1,15 @@
+require_relative 'config.rb'
+
+iw = IronWorkerNG::Client.new
+task = iw.tasks.create("email_worker",
+ SingletonConfig.config.merge(
+ to: "treeder@gmail.com",
+ from: "travis@iron.io",
+ subject: "Thanks for the Lead!",
+ body: "Thanks again for the lead"
+
+ ))
+status = iw.tasks.wait_for(task.id)
+p status
+puts status.msg
+puts iw.tasks.log(task.id)
View
11 views/index.erb
@@ -1,11 +1,3 @@
-<!DOCTYPE html>
-<html>
-<head>
- <title>Lead Form</title>
-</head>
-<body>
-
-
<div class="description">
<h1>Welcome</h1>
@@ -27,5 +19,4 @@
<button>Submit Info</button>
</form>
</div>
-</body>
-</html>
+
View
1 views/layout.erb
@@ -45,7 +45,6 @@
<ul class="nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="#about">About</a></li>
- <li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
View
56 workers/email_worker.rb
@@ -0,0 +1,56 @@
+require 'mail'
+
+# Configures smtp settings to send email.
+def init_mail(params)
+ username = params['username']
+ password = params['password']
+ domain = params['domain']
+ host = params['host']
+ port = params['port']
+
+ puts "Preparing mail configuration"
+ mail_conf = {:address => host,
+ :port => port,
+ :domain => domain, #custom domain
+ :user_name => username,
+ :password => password,
+ :authentication => 'plain',
+ :enable_starttls_auto => true} #gmail require this option
+ Mail.defaults do
+ delivery_method :smtp, mail_conf
+ end
+ puts "Mail service configured"
+end
+
+def send_mail(to, from, subject, content)
+ puts "Preparing email from: #{from}, to: #{to}, subject: #{subject}, body: #{content}"
+ msg = Mail.new do
+ to to
+ from from
+ subject subject
+ html_part do |m|
+ content_type 'text/html'
+ body content
+ end
+ end
+ puts "Mail ready, delivering"
+ details = msg.deliver
+ puts "Mail delivered!"
+ details
+end
+
+# Sample worker that sends an email.
+puts "Worker started"
+
+init_mail(params['email'])
+
+to = params['to']
+unless to.is_a?(Array)
+ to = [to]
+end
+
+to.each do |email|
+ message_details = send_mail(email, params['from'], params['subject'], params['body'])
+end
+
+puts "Worker finished"
View
5 workers/email_worker.worker
@@ -0,0 +1,5 @@
+runtime "ruby"
+exec "email_worker.rb"
+gem "mail"
+gem "rest"
+gem "iron_cache"

0 comments on commit a305349

Please sign in to comment.