Skip to content
Browse files

added email example, refactored a bit

  • Loading branch information...
1 parent 6c901f0 commit baf5f10c049f3cc13eb3de097e3f02d898dfba41 @rkononov rkononov committed Sep 7, 2012
View
260 README.rdoc
@@ -1,261 +1,11 @@
-== Welcome to Rails
-
-Rails is a web-application framework that includes everything needed to create
-database-backed web applications according to the Model-View-Control pattern.
-
-This pattern splits the view (also called the presentation) into "dumb"
-templates that are primarily responsible for inserting pre-built data in between
-HTML tags. The model contains the "smart" domain objects (such as Account,
-Product, Person, Post) that holds all the business logic and knows how to
-persist themselves to a database. The controller handles the incoming requests
-(such as Save New Account, Update Product, Show Post) by manipulating the model
-and directing data to the view.
-
-In Rails, the model is handled by what's called an object-relational mapping
-layer entitled Active Record. This layer allows you to present the data from
-database rows as objects and embellish these data objects with business logic
-methods. You can read more about Active Record in
-link:files/vendor/rails/activerecord/README.html.
-
-The controller and view are handled by the Action Pack, which handles both
-layers by its two parts: Action View and Action Controller. These two layers
-are bundled in a single package due to their heavy interdependence. This is
-unlike the relationship between the Active Record and Action Pack that is much
-more separate. Each of these packages can be used independently outside of
-Rails. You can read more about Action Pack in
-link:files/vendor/rails/actionpack/README.html.
+== Iron Worker 101 Rails example
+This example show how to work with iron_worker under Rails environment
== Getting Started
-1. At the command prompt, create a new Rails application:
- <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
-
-2. Change directory to <tt>myapp</tt> and start the web server:
- <tt>cd myapp; rails server</tt> (run with --help for options)
-
-3. Go to http://localhost:3000/ and you'll see:
- "Welcome aboard: You're riding Ruby on Rails!"
-
-4. Follow the guidelines to start developing your application. You can find
-the following resources handy:
-
-* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
-* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
-
-
-== Debugging Rails
-
-Sometimes your application goes wrong. Fortunately there are a lot of tools that
-will help you debug it and get it back on the rails.
-
-First area to check is the application log files. Have "tail -f" commands
-running on the server.log and development.log. Rails will automatically display
-debugging and runtime information to these files. Debugging info will also be
-shown in the browser on requests from 127.0.0.1.
-
-You can also log your own messages directly into the log file from your code
-using the Ruby logger class from inside your controllers. Example:
-
- class WeblogController < ActionController::Base
- def destroy
- @weblog = Weblog.find(params[:id])
- @weblog.destroy
- logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
- end
- end
-
-The result will be a message in your log file along the lines of:
-
- Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
-
-More information on how to use the logger is at http://www.ruby-doc.org/core/
-
-Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
-several books available online as well:
-
-* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
-* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
-
-These two books will bring you up to speed on the Ruby language and also on
-programming in general.
-
-
-== Debugger
-
-Debugger support is available through the debugger command when you start your
-Mongrel or WEBrick server with --debugger. This means that you can break out of
-execution at any point in the code, investigate and change the model, and then,
-resume execution! You need to install ruby-debug to run the server in debugging
-mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
-
- class WeblogController < ActionController::Base
- def index
- @posts = Post.all
- debugger
- end
- end
-
-So the controller will accept the action, run the first line, then present you
-with a IRB prompt in the server window. Here you can do things like:
-
- >> @posts.inspect
- => "[#<Post:0x14a6be8
- @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
- #<Post:0x14a6620
- @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
- >> @posts.first.title = "hello from a debugger"
- => "hello from a debugger"
-
-...and even better, you can examine how your runtime objects actually work:
-
- >> f = @posts.first
- => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
- >> f.
- Display all 152 possibilities? (y or n)
-
-Finally, when you're ready to resume execution, you can enter "cont".
-
-
-== Console
-
-The console is a Ruby shell, which allows you to interact with your
-application's domain model. Here you'll have all parts of the application
-configured, just like it is when the application is running. You can inspect
-domain models, change values, and save to the database. Starting the script
-without arguments will launch it in the development environment.
-
-To start the console, run <tt>rails console</tt> from the application
-directory.
-
-Options:
-
-* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
- made to the database.
-* Passing an environment name as an argument will load the corresponding
- environment. Example: <tt>rails console production</tt>.
-
-To reload your controllers and models after launching the console run
-<tt>reload!</tt>
-
-More information about irb can be found at:
-link:http://www.rubycentral.org/pickaxe/irb.html
-
-
-== dbconsole
-
-You can go to the command line of your database directly through <tt>rails
-dbconsole</tt>. You would be connected to the database with the credentials
-defined in database.yml. Starting the script without arguments will connect you
-to the development database. Passing an argument will connect you to a different
-database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
-PostgreSQL and SQLite 3.
-
-== Description of Contents
-
-The default directory structure of a generated Ruby on Rails application:
-
- |-- app
- | |-- assets
- | |-- images
- | |-- javascripts
- | `-- stylesheets
- | |-- controllers
- | |-- helpers
- | |-- mailers
- | |-- models
- | `-- views
- | `-- layouts
- |-- config
- | |-- environments
- | |-- initializers
- | `-- locales
- |-- db
- |-- doc
- |-- lib
- | `-- tasks
- |-- log
- |-- public
- |-- script
- |-- test
- | |-- fixtures
- | |-- functional
- | |-- integration
- | |-- performance
- | `-- unit
- |-- tmp
- | |-- cache
- | |-- pids
- | |-- sessions
- | `-- sockets
- `-- vendor
- |-- assets
- `-- stylesheets
- `-- plugins
-
-app
- Holds all the code that's specific to this particular application.
-
-app/assets
- Contains subdirectories for images, stylesheets, and JavaScript files.
-
-app/controllers
- Holds controllers that should be named like weblogs_controller.rb for
- automated URL mapping. All controllers should descend from
- ApplicationController which itself descends from ActionController::Base.
-
-app/models
- Holds models that should be named like post.rb. Models descend from
- ActiveRecord::Base by default.
-
-app/views
- Holds the template files for the view that should be named like
- weblogs/index.html.erb for the WeblogsController#index action. All views use
- eRuby syntax by default.
-
-app/views/layouts
- Holds the template files for layouts to be used with views. This models the
- common header/footer method of wrapping views. In your views, define a layout
- using the <tt>layout :default</tt> and create a file named default.html.erb.
- Inside default.html.erb, call <% yield %> to render the view using this
- layout.
-
-app/helpers
- Holds view helpers that should be named like weblogs_helper.rb. These are
- generated for you automatically when using generators for controllers.
- Helpers can be used to wrap functionality for your views into methods.
-
-config
- Configuration files for the Rails environment, the routing map, the database,
- and other dependencies.
-
-db
- Contains the database schema in schema.rb. db/migrate contains all the
- sequence of Migrations for your schema.
-
-doc
- This directory is where your application documentation will be stored when
- generated using <tt>rake doc:app</tt>
-
-lib
- Application specific libraries. Basically, any kind of custom code that
- doesn't belong under controllers, models, or helpers. This directory is in
- the load path.
-
-public
- The directory available for the web server. Also contains the dispatchers and the
- default HTML files. This should be set as the DOCUMENT_ROOT of your web
- server.
-
-script
- Helper scripts for automation and generation.
+1. Set proper configuration in iron.json and config_development.yml
-test
- Unit and functional tests along with fixtures. When using the rails generate
- command, template test files will be generated for you and placed in this
- directory.
+2. run 'rails s'
-vendor
- External libraries that the application depends on. Also includes the plugins
- subdirectory. If the app has frozen rails, those gems also go here, under
- vendor/rails/. This directory is in the load path.
+3. Go to http://localhost:3000/ and test
View
3 app/assets/javascripts/custom_examples.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3 app/assets/stylesheets/custom_examples.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the custom_examples controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
2 app/controllers/application_controller.rb
@@ -2,6 +2,6 @@ class ApplicationController < ActionController::Base
protect_from_forgery
def client
- @client||= IronWorkerNG::Client.new
+ IW_CLIENT
end
end
View
4 app/controllers/codes_controller.rb
@@ -4,8 +4,8 @@ def index
@files = Dir.glob('workers/*.worker')
end
- def upload
- code_from_workerfile = IronWorkerNG::Code::Base.new(:workerfile => params[:filename])
+ def create
+ code_from_workerfile = IronWorkerNG::Code::Base.new(:workerfile => params['filename'])
client.codes.create(code_from_workerfile)
redirect_to codes_path
end
View
11 app/controllers/custom_examples_controller.rb
@@ -0,0 +1,11 @@
+class CustomExamplesController < ApplicationController
+ def index
+ end
+
+ def send_email
+ client.tasks.create('SimpleMailerWorker', {:email => params[:email],
+ :mailer => ActionMailer::Base.smtp_settings,
+ :message => params[:message]})
+ redirect_to tasks_path
+ end
+end
View
3 app/controllers/home_controller.rb
@@ -1,4 +1,7 @@
class HomeController < ApplicationController
def index
end
+
+ def custom_examples
+ end
end
View
5 app/controllers/schedules_controller.rb
@@ -8,8 +8,9 @@ def cancel_schedule
redirect_to schedules_path
end
- def schedule_task
- res = client.schedules.create(params[:worker_name], JSON.parse(params[:schedule_params]), {:start_at => Time.now + params[:seconds].to_i })
+ def create
+ schedule_params = params['schedule_params'] ? JSON.parse(params['schedule_params']) : {}
+ res = client.schedules.create(params['worker_name'], schedule_params, {:start_at => Time.now + params['seconds'].to_i})
puts "Result:#{res.inspect}"
redirect_to schedules_path
end
View
4 app/controllers/tasks_controller.rb
@@ -3,8 +3,8 @@ def index
@tasks = client.tasks.list
end
- def queue
- res = client.tasks.create(params[:worker_name], params[:task_params])
+ def create
+ res = client.tasks.create(params['worker_name'], params['task_params'])
puts "Result:#{res.inpect}"
redirect_to tasks_path
end
View
2 app/helpers/custom_examples_helper.rb
@@ -0,0 +1,2 @@
+module CustomExamplesHelper
+end
View
7 app/mailers/simple_mailer.rb
@@ -0,0 +1,7 @@
+class SimpleMailer < ActionMailer::Base
+ default from: "support@iron.io"
+ def welcome_email(email,message)
+ @message = message
+ mail(:to => email, :subject => "IronWorker SimpleMailer")
+ end
+end
View
37 app/views/codes/index.html.erb
@@ -1,4 +1,22 @@
<h1>Manage your codes</h1>
+
+<h3>Worker files to upload</h3>
+<table border="1">
+ <thead>
+ <tr>
+ <td>File Name</td>
+ <td>Action</td>
+ </tr>
+ </thead>
+
+ <% @files.each do |f| %>
+ <tr>
+ <td><%= File.basename(f) %></td>
+ <td><%= button_to "Upload file",codes_path(:filename => f) %></td>
+ <% end %>
+</table>
+
+<h3>List of existed codes:</h3>
<% if !@codes.empty? %>
<table border="1">
<thead>
@@ -23,8 +41,8 @@
<%= link_to "Revisions", revisions_code_path(c.id) %>
<%= link_to "Download", download_code_path(c.id) %>
</td>
- <td><%= button_to "Queue!", queue_tasks_path(:worker_name => c.name) %></td>
- <td><%= button_to "Schedule!", schedule_task_schedules_path(:worker_name => c.name, :seconds => 10) %></td>
+ <td><%= button_to "Queue!", tasks_path(:worker_name => c.name) %></td>
+ <td><%= button_to "Schedule!", schedules_path(:worker_name => c.name, :seconds => 10) %></td>
</tr>
<% end %>
</table>
@@ -33,19 +51,4 @@
<% end %>
-<h3>Worker files to upload</h3>
-<table border="1">
- <thead>
- <tr>
- <td>File Name</td>
- <td>Action</td>
- </tr>
- </thead>
-
- <% @files.each do |f| %>
- <tr>
- <td><%= File.basename(f) %></td>
- <td><%= button_to "Upload file", upload_codes_path(:filename => f) %></td>
- <% end %>
-</table>
<%= link_to "Back", root_path %>
View
11 app/views/custom_examples/index.html.erb
@@ -0,0 +1,11 @@
+<h1>List of simple demos</h1>
+
+<h3>Mailer Worker</h3>
+This is simple worker that just send email via worker (don't forget to set proper mailer configuration in config_development.yml)
+<%= form_tag(send_email_custom_examples_path) do %>
+ <%= label_tag(:email) %>
+ <%= text_field_tag(:email, 'email@email.com') %>
+ <%= label_tag(:message) %>
+ <%= text_field_tag(:message, 'sample message') %>
+ <%= submit_tag("Send Email!") %>
+<% end %>
View
10 app/views/home/index.html.erb
@@ -1,12 +1,14 @@
-<h1>IronWorker 101 example</h1>
+<h1>IronWorker Rails 101 example</h1>
<h3>Here are few sections that guide you through main IronWorker features:</h3>
<ul>
- <li>First of all you need to create and upload your code package:<%= link_to "Codes", codes_path %>
+ <li>First of all you need to create and upload your code package: <%= link_to "Manage code packages", codes_path %>
<br/></li>
- <li>Then you could queue task:<%= link_to "Tasks", tasks_path %>
+ <li>Then you could queue task: <%= link_to "Manage tasks", tasks_path %>
<br/></li>
- <li>And manage scheduled tasks:<%= link_to "Schedules", schedules_path %>
+ <li>And manage scheduled tasks: <%= link_to "Manage schedules", schedules_path %>
+ <br/></li>
+ <li>List of examples: <%= link_to "Examples", custom_examples_path %>
<br/></li>
</ul>
View
21 app/views/schedules/index.html.erb
@@ -1,5 +1,17 @@
<h1>Scheduled tasks</h1>
+<h3>Schedule Task</h3>
+<%= form_tag(schedules_path) do %>
+ <%= label_tag(:worker_name) %>
+ <%= text_field_tag(:worker_name, "HelloWorker") %>
+ <%= label_tag(:schedule_params) %>
+ <%= text_field_tag(:schedule_params, '{"text":"sample"}') %>
+ <%= label_tag(:seconds) %>
+ <%= select_tag "seconds", options_for_select([5, 10, 15], 1) %>
+ <%= submit_tag("Schedule") %>
+<% end %>
+
+
<h3>List of scheduled tasks:</h3>
<% if !@schedules.empty? %>
<table border="1">
@@ -30,13 +42,4 @@
<h5>There are now any active scheduled tasks, feel free to schedule new one</h5>
<% end %>
-<%= form_tag(schedule_task_schedules_path) do %>
- <%= label_tag(:worker_name) %>
- <%= text_field_tag(:worker_name, "HelloWorker") %>
- <%= label_tag(:schedule_params) %>
- <%= text_field_tag(:schedule_params, '{"text":"sample"}') %>
- <%= label_tag(:seconds) %>
- <%= select_tag "seconds", options_for_select([5, 10, 15], 1) %>
- <%= submit_tag("Schedule") %>
-<% end %>
<%= link_to "Back", root_path %>
View
14 app/views/simple_mailer/welcome_email.erb
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
+</head>
+<body>
+<h1>Thank you for using IronWorker Rails101 example</h1>
+
+<p>
+ Text passed to worker:<b><%= @message %></b>
+</p>
+
+</body>
+</html>
View
18 app/views/tasks/index.html.erb
@@ -1,4 +1,13 @@
<h1>Tasks</h1>
+<h3>Queue task</h3>
+<%= form_tag(tasks_path) do %>
+ <%= label_tag(:worker_name) %>
+ <%= text_field_tag(:worker_name, "HelloWorker") %>
+ <%= label_tag(:task_params) %>
+ <%= text_field_tag(:task_params, '{"text":"sample"}') %>
+ <%= submit_tag("Queue") %>
+<% end %>
+
<h3>List of tasks:</h3>
<% if !@tasks.empty? %>
<table border="1">
@@ -31,11 +40,4 @@
<h5>There are now any tasks, feel free to queue new one</h5>
<% end %>
<p>
- <%= form_tag(queue_tasks_path) do %>
- <%= label_tag(:worker_name) %>
- <%= text_field_tag(:worker_name,"HelloWorker") %>
- <%= label_tag(:task_params) %>
- <%= text_field_tag(:task_params,'{"text":"sample"}') %>
- <%= submit_tag("Queue") %>
-<% end %>
-<%=link_to "Back", root_path%>
+<%= link_to "Back", root_path %>
View
10 config/application.rb
@@ -58,5 +58,15 @@ class Application < Rails::Application
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
+ config2 = YAML.load_file(File.join(Rails.root, "config_#{Rails.env}.yml"))
+ config.action_mailer.smtp_settings =
+ {
+ :address => config2['address'],
+ :port => config2['port'],
+ :domain => config2['domain'],
+ :authentication => :plain,
+ :user_name => config2['user_name'],
+ :password => config2['password']
+ }
end
end
View
4 config/environments/development.rb
@@ -34,4 +34,8 @@
# Expands the lines which load the assets
config.assets.debug = true
+
+ config.action_mailer.delivery_method = :smtp
+
+
end
View
7 config/initializers/iron_worker.rb
@@ -0,0 +1,7 @@
+puts "Initializing IronWorker"
+IW_CLIENT = IronWorkerNG::Client.new
+puts "Creating worker from worker file"
+code_from_workerfile = IronWorkerNG::Code::Base.new(:workerfile => 'workers/simple_mailer_worker.worker')
+puts "Uploading worker"
+IW_CLIENT.codes.create(code_from_workerfile)
+puts "Worker uploaded"
View
15 config/routes.rb
@@ -6,14 +6,8 @@
get 'download'
get 'revisions'
end
- collection do
- post 'upload'
- end
end
resources :tasks do
- collection do
- post 'queue'
- end
member do
post 'cancel'
post 'cancel_all'
@@ -22,14 +16,17 @@
end
end
resources :schedules do
- collection do
- post 'schedule_task'
- end
member do
post 'cancel'
end
end
+ resources :custom_examples do
+ collection do
+ post 'send_email'
+ end
+ end
+
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
View
6 config_development.yml
@@ -0,0 +1,6 @@
+address: smtp.sendgrid.net
+port: 25
+domain: s
+user_name: sy
+password: a
+
View
4 iron.json
@@ -0,0 +1,4 @@
+{
+ "token": "ZJI9lHPkAdp",
+ "project_id": "5047727"
+}
View
7 test/functional/custom_examples_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CustomExamplesControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
7 test/functional/simple_mailer_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class SimpleMailerTest < ActionMailer::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/custom_examples_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CustomExamplesHelperTest < ActionView::TestCase
+end
View
19 workers/simple_mailer_worker.rb
@@ -0,0 +1,19 @@
+require "action_mailer"
+require 'simple_mailer.rb'
+
+#configuring up mailer
+def init_mailer
+ #Need to convert to proper hashes
+ mailer_config = params['mailer'].inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
+ ActionMailer::Base.prepend_view_path('.')
+ ActionMailer::Base.raise_delivery_errors = true
+ ActionMailer::Base.smtp_settings = mailer_config
+ ActionMailer::Base.delivery_method = :smtp
+end
+
+#-------------------Main worker code---------------------------
+init_mailer
+puts "I got '#{params.inspect}' parameters"
+#send email!
+SimpleMailer.welcome_email(params['email'], params['message']).deliver!
+
View
8 workers/simple_mailer_worker.worker
@@ -0,0 +1,8 @@
+# define the runtime language, this can be ruby, java, node, php, go, etc.
+runtime "ruby"
+name "SimpleMailerWorker"
+file "../app/views/simple_mailer/welcome_email.erb" , "simple_mailer"
+file "../app/mailers/simple_mailer.rb"
+
+# exec is the file that will be executed:
+exec "simple_mailer_worker.rb"

0 comments on commit baf5f10

Please sign in to comment.
Something went wrong with that request. Please try again.