jruby, ruby, rails and you on the cloud
DESCRIPTION
Cloudstock 2010 PresentationTRANSCRIPT
• Platform as a Service provider • Ruby on Rails specialists • http://www.engineyard.com
3
Engine Yard
WHAT IS RUBY ON RAILS?"AND"WHY RUBY ON RAILS?
5
http://www.flickr.com/photos/88019192@N00/3277925689/
• http://bit.ly/jvm-frameworks-matrix • http://bit.ly/webmatrixsurveyresults
6
JVM Web Frameworks Surveys
Rails
ü Is a web application framework written in Ruby
7
http://www.flickr.com/photos/46799485@N00/4386602878
• Conceived by Yukihiro Matsumoto (a.k.a. Matz) in 1993 • Object-oriented dynamically-typed interpreted scripting language • Influenced by Perl, Smalltalk, Eiffel and LISP • External libraries are most frequently distributed as “gems”
8
Ruby quick facts
• Conceived by David Heinemeier Hansson (a.k.a. DHH) in 2004 • Extracted from 37signals’ “Basecamp” application • Uses MVC design pattern • Rails 3, released in August, 2010, uses “Bundler” to manage gems.
9
Rails quick facts
18
jruby -S rails new cloudstock_demo cd cloudstock_demo vi Gemfile jruby -S bundle install jruby -S bundle exec glassfish
$ jruby -S rails new cloudstock_demo -m http://jruby.org create create README … create app create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/mailers create app/models create app/views/layouts/application.html.erb … create public create public/404.html … create script/rails create test create test/fixtures … create test/test_helper.rb create test/unit … apply http://jruby.org apply http://jruby.org/templates/default.rb gsub Gemfile
19
$ jruby script/rails generate scaffold user name:string calling init (1017e6ac0) invoke active_record create db/migrate/20101115150436_create_users.rb create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml route resources :users invoke scaffold_controller create app/controllers/users_controller.rb invoke erb create app/views/users create app/views/users/index.html.erb create app/views/users/edit.html.erb create app/views/users/show.html.erb create app/views/users/new.html.erb create app/views/users/_form.html.erb invoke test_unit create test/functional/users_controller_test.rb invoke helper create app/helpers/users_helper.rb invoke test_unit create test/unit/helpers/users_helper_test.rb invoke stylesheets create public/stylesheets/scaffold.css
21
$ cat db/migrate/20101115150436_create_users.rb class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.timestamps end end def self.down drop_table :users end end
22
$ find test test test/fixtures test/fixtures/users.yml test/functional test/functional/users_controller_test.rb test/integration test/performance test/performance/browsing_test.rb test/test_helper.rb test/unit test/unit/helpers test/unit/helpers/users_helper_test.rb test/unit/user_test.rb
26
$ cat test/unit/user_test.rb require 'test_helper' class UserTest < ActiveSupport::TestCase test "bare user is invalid" do u = User.new assert !u.valid? assert u.errors[:name].any? end test "user with name shorter than 5 character is invalid" do u = User.new(:name => "hi") assert !u.valid? assert u.errors[:name].any? end test "user with name with at least 5 character is valid" do u = User.new(:name => "hello") assert u.valid? end end
27
• Generating SVG with Batik • http://xmlgraphics.apache.org/batik/
31
Accessing Java library from Rails
Example 1: Generating SVG with Batik
32
require 'java' require 'batik' java_import org.apache.batik.svggen.SVGGraphics2D java_import org.apache.batik.dom.GenericDOMImplementation dom = GenericDOMImplementation.getDOMImplementation svg_ns = 'http://www.w3.org/2000/svg' doc = dom.createDocument svg_ns, 'svg', nil svg_gen = SVGGraphics2D.new( doc ) svg_gen.set_paint java.awt.Color.send(color) svg_gen.fill java.awt.Rectangle.new(10,10,100,100) svg_gen.set_paint java.awt.Color.send(text_color) svg_gen.draw_string @user.name, 25, 55 out = java.io.StringWriter.new svg_gen.stream(out, true) render :inline => out.to_string
Example 2: Generating PDF with iText
34
require 'iText-5.0.5' pdf = com.itextpdf.text.Document.new para = com.itextpdf.text.Paragraph.new "Hello #{@user.name}" file = "#{::Rails.root.to_s}/tmp/pdfs/pdf_demo.pdf” out = java.io.FileOutputStream.new file com.itextpdf.text.pdf.PdfWriter.get_instance pdf, out pdf.open pdf.add para pdf.close render :file => file
Don’t do this in production code!
• RSpec http://rspec.info • Cucumber http://cukes.info • Celerity http://celerity.rubyforge.org
37
Testing in more detail
• Glassfish http://glassfish.java.net/ (.WAR) • Tomcat http://tomcat.apache.org/ (.WAR) • JBoss http://jboss.org/ (.WAR) • Jetty http://jetty.codehaus.org/jetty/ (.WAR) • Glassfish gem http://rubyforge.org/projects/glassfishgem/ (embedded
glassfish) • Trinidad https://github.com/calavera/trinidad (embedded Tomcat) • Torquebox http://torquebox.org/ (embedded JBoss with extras) • Google App Engine http://code.google.com/appengine/ (via google-
appengine gem http://code.google.com/p/appengine-jruby/) • Mongrel (outdated)
38
Deployment strategies
• The Ruby language web site http://ruby-lang.org • Ruby On Rails web site http://rubyonrails.org • JRuby web site http://jruby.org • Engine Yard web site http://engineyard.com • Using JRuby (Pragmatic Bookshelf) http://www.pragprog.com/titles/jruby/using-jruby • Meet Rails 3 (Peep Code) https://peepcode.com/products/meet-rails-3-i
39
Further information
Contact me [email protected]
Twitter: @hiro_asari Github: BanzaiMan
42
Thank you!
http://www.flickr.com/photos/42033648@N00/372887164