Skip to content

Commit

Permalink
Merge branch 'master' of github.com:iron-io/iron_worker_ruby_ng
Browse files Browse the repository at this point in the history
  • Loading branch information
iced committed Dec 17, 2012
2 parents 09bad19 + d911315 commit 4d28a94
Show file tree
Hide file tree
Showing 25 changed files with 288 additions and 185 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -5,3 +5,6 @@ pkg
iron.json
examples/github_to_hipchat_webhook_worker/webhook_config.yml
examples/worker101/twitter_config.json
examples/actionmailer_standalone/actionmailer_config.json
examples/chargify_to_campfire_webhook_worker/campfire_config.json
examples/email_worker/email_config.json
25 changes: 23 additions & 2 deletions Gemfile.lock
@@ -1,17 +1,27 @@
PATH
remote: .
specs:
iron_worker_ng (0.13.0)
iron_worker_ng (0.14.0)
bundler (>= 1.2.0)
iron_core (>= 0.4.2)
rubyzip (>= 0.9.9)

GEM
remote: https://rubygems.org/
specs:
actionmailer (2.3.14)
actionpack (= 2.3.14)
actionpack (2.3.14)
activesupport (= 2.3.14)
rack (~> 1.1.0)
activesupport (2.3.14)
ansi (1.4.3)
builder (3.1.4)
faraday (0.8.4)
multipart-post (~> 1.1)
hashie (1.2.0)
iron_cache (1.3.0)
iron_core (>= 0.4.2)
iron_core (0.4.3)
rest (>= 2.0.2)
mime-types (1.19)
Expand All @@ -21,25 +31,36 @@ GEM
builder
minitest (>= 2.12, < 5.0)
powerbar
multi_json (1.3.6)
multipart-post (1.1.5)
net-http-persistent (2.8)
powerbar (1.0.11)
ansi (~> 1.4.0)
hashie (>= 1.1.0)
rack (1.1.3)
rake (10.0.1)
rest (2.1.0)
rest (2.1.1)
net-http-persistent
rest-client (>= 0.3.0)
rest-client (1.6.7)
mime-types (>= 1.16)
rubyzip (0.9.9)
simple_oauth (0.1.9)
test-unit (2.5.2)
twitter (4.3.0)
faraday (~> 0.8)
multi_json (~> 1.3)
simple_oauth (~> 0.1.6)

PLATFORMS
ruby

DEPENDENCIES
actionmailer
iron_cache
iron_worker_ng!
minitest
minitest-reporters (>= 0.5.0)
rake
test-unit
twitter
11 changes: 11 additions & 0 deletions examples/actionmailer_standalone/README.md
@@ -0,0 +1,11 @@
# action_mailer example

This example shows how to send emails in worker.

1. Be sure you've setup your Iron.io credentials, see main [README.md](https://github.com/iron-io/iron_worker_examples).
2. Copy `actionmailer_config_sample.json` to `actionmailer_config.json` and edit it to set your gmail credentials.
3. Run `iron_worker upload actionmailer_standalone` to upload worker.
4. Run `iron_worker queue actionmailer_standalone --payload-file actionmailer_config.json`
5. Look at [HUD](https://hud.iron.io) to view your tasks running, check logs, etc.

Read the code in this directory to learn more about what happened.
@@ -0,0 +1,8 @@
{
"gmail": {
"username": "you@gmail.com",
"password": "yourpwd"
},
"from": "you@gmail.com",
"to": ["you@gmail.com"]
}
@@ -0,0 +1,4 @@
exec 'actionmailer_worker.rb'
file 'mailer.rb'
dir 'mailer'
gem 'actionmailer'
8 changes: 0 additions & 8 deletions examples/actionmailer_standalone/enqueue.rb

This file was deleted.

14 changes: 0 additions & 14 deletions examples/actionmailer_standalone/upload.rb

This file was deleted.

23 changes: 13 additions & 10 deletions examples/chargify_to_campfire_webhook_worker/README.md
@@ -1,13 +1,16 @@
# Github Webhook Worker
# Chargify Webhook Worker

This shows how to kick off a worker from a webhook. This example uses Github's Service Hooks.
This shows how to kick off a worker from a webhook. This example uses Chargify's webhooks.

## Getting started
1. Be sure you've setup your Iron.io credentials, see main [README.md](https://github.com/iron-io/iron_worker_examples).
1. Copy or rename the campfire_config_example.json to campfire_config.json and edit it to set your campfire credentials.
1. `iron_worker upload chargify_to_campfire`
1. See webhook url `iron_worker webhook chargify_to_campfire`
1. Set it in chargify (Settings -> Webhooks)
1. Select types of events you want to be notified of.
1. Click 'Save Webhook settings'
1. Click 'Send a test Webhook'
1. Check the worker status and logs in IronWorker at http://hud.iron.io to ensure it ran successfully.
1. Check campfire for messages from webhook.

- Copy or rename the webhook_config_example.yml to webhook_config.yml and edit it to set your hipchat credentials.
- Upload the worker by running `upload.rb` in this directory and follow instruction it prints
- Click Update Settings
- Click Test Hook
- Check the worker status and logs in IronWorker at http://hud.iron.io to ensure it ran successfully.

That's it, now everytime someone pushes to your github repo, it'll execute the GithubWebhookWorker on IronWorker.
That's it, now you'll be notified about selected chargify events in campfire.
@@ -0,0 +1,5 @@
{
"account": "sample",
"token": "12462462460f3456a3571e0456a7c5845f774674",
"room": "Room 1"
}
@@ -0,0 +1,4 @@
exec 'chargify_to_campfire_webhook_worker.rb'
gem 'broach'
file 'campfire_config.json'
full_remote_build true
@@ -1,27 +1,21 @@
require 'cgi'
require 'yaml'
require 'broach'

# the payload we get from github needs to be decoded first
cgi_parsed = CGI::parse(payload)
puts "cgi_parsed: #{cgi_parsed.inspect}"

# Then we can parse the json
#parsed = JSON.parse(cgi_parsed)
#puts "parsed: #{parsed.inspect}"
parsed = cgi_parsed # seems to already be in a hash?
event = cgi_parsed["event"][0]

@event = parsed["event"][0]

# Also parse the config we uploaded with this worker for our Hipchat stuff
webhook_config = YAML.load_file('webhook_config.yml')
puts "webhook_config: #{webhook_config.inspect}"

campfire_config = webhook_config['campfire']
# parse campfire config
cfg = JSON.parse(File.read('campfire_config.json'))
puts "campfire config: #{cfg.inspect}"

Broach.settings = {
'account' => campfire_config['account'],
'token' => campfire_config['token'],
'use_ssl' => true
'account' => cfg['account'],
'token' => cfg['token'],
'use_ssl' => true
}
Broach.speak(campfire_config['room'], @event)
Broach.speak(cfg['room'], event)

puts 'Done'
19 changes: 0 additions & 19 deletions examples/chargify_to_campfire_webhook_worker/upload.rb

This file was deleted.

This file was deleted.

15 changes: 7 additions & 8 deletions examples/email_worker/README.md
@@ -1,13 +1,12 @@
# email_worker
# email_worker example

This is simple example how to send email using IronWorker
Command line tool reference could be found here - http://dev.iron.io/worker/reference/cli/

## Getting started

- Edit _config.yml (iw and email sections)
- Upload the worker by running 'iron_worker upload email_worker' in this directory
- run 'single_email_sender.rb'
1. Be sure you've setup your Iron.io credentials, see main [README.md](https://github.com/iron-io/iron_worker_examples).
2. Copy `email_config_sample.json` to `email_config.json` and edit it to set your credentials.
3. Run `iron_worker upload email_worker` to upload worker.
4. Run `iron_worker queue email_worker --payload-file email_config.json`
5. Look at [HUD](https://hud.iron.io) to view your tasks running, check logs, etc.

That's it, now you should receive email.

Read the code in this directory to learn more about what happened.
26 changes: 26 additions & 0 deletions examples/email_worker/email_config_sample.json
@@ -0,0 +1,26 @@
{
"smtp": {
"user_name": "you@gmail.com",
"password": "secret",
"enable_starttls_auto": true,
"authentication": "plain",
"address": "smtp.gmail.com",
"port": 587
// "address": "smtp.sendgrid.net",
// "port": 25
},

// uncomment and edit to put email details on iron_cache
// "iron": {
// "token": "Et1E98729890283837Fu34L3234",
// "project_id": "523412343249345435342349"
// },

// uncomment and edit to send email detail via http api
// "endpoint": "www.myservice.com/api/email_sent",

"from": "you@gmail.com",
"to": "you@gmail.com",
"subject": "hello from IronWorker",
"body": "hello me"
}
102 changes: 28 additions & 74 deletions examples/email_worker/email_worker.rb
@@ -1,87 +1,41 @@
# Configures smtp settings to send email.
def init_mail(username, password, domain, provider)
puts "initializing provider"
case provider
when 'gmail'
puts "Selected gmail as a provider"
port = 587
address = "smtp.gmail.com"
when 'sendgrid'
puts "Selected sendgrid as a provider"
port = 25
address = "smtp.sendgrid.net"
end
puts "Preparing mail configuration"
mail_conf = {:address => address,
: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"
require 'mail'
require 'rest'
require 'iron_cache'

# 'params' name is used in Mail widely
pars = JSON.parse(payload, symbolize_names: true)

# Configure smtp settings to send email.
Mail.defaults do
delivery_method :smtp, pars[:smtp]
end

def send_mail(to, from, subject, content)
puts "Preparing email from:#{from},to:#{to},subject#{subject}"
[pars[:to]].flatten.each do |to|
msg = Mail.new do
to to
from from
subject subject
html_part do |m|
content_type 'text/html'
body content
end
from pars[:from]
subject pars[:subject]
body pars[:body]
end
puts "Mail ready, delivering"
details = msg.deliver
puts "Mail delivered!"
details
end

def update_message_status(email, message_details)
puts "Updating user status via API endpoint"
puts "Email:#{email},details:#{message_details.inspect}"
puts "email:#{to}, details:#{details.to_s}"

#here is the example how you could send email details from this worker to your api
require 'rest'
puts "initializing client"
web_client = Rest::Client.new # res gem already merged
puts "sending api request"
#just change google.com on your api endpoint
web_client.get('http://google.com', {:email => email, :message_details => message_details})

# or put email details in iron_cache
if params['iw_token'] && params['iw_project_id']
puts "Initializing cache"
require 'iron_cache'
cache = IronCache::Client.new(:token => params['iw_token'], :project_id => params['iw_project_id'])
puts "Sending message details to cache"
cache.items.put(email, {:timestamp => Time.now, :message_details => message_details.inspect}.to_json)
if pars[:endpoint]
# here is the example how you could send email details from this worker to your api
puts "initializing client"
web_client = Rest::Client.new
puts "sending api request"
web_client.get(pars[:endpoint], email: to, details: details.to_s)
end

end

# Sample worker that sends an email.
puts "Worker started"

require 'mail'

init_mail(params['username'], params['password'], params['domain'], params['provider'])


if params['to'].is_a? Array # check what is passed one email or array of emails
puts "Array of #{params['to'].count} emails passed"
params['to'].each do |email|
message_details = send_mail(email, params['from'], params['subject'], params['content'])
update_message_status(email, message_details)
# or put email details in iron_cache
if pars[:iron]
puts "initializing cache"
cache = IronCache::Client.new(pars[:iron])
puts "sending message details to cache"
cache.items.put(to, details.to_s)
end
else
message_details = send_mail(params['to'], params['from'], params['subject'], params['content'])
update_message_status(email, message_details)
end

puts "Worker finished"
puts "worker finished"

0 comments on commit 4d28a94

Please sign in to comment.