Nejdříve si ukážeme obligátní „hello“ program.
1 #!/usr/bin/env ruby -w 2 # $Id: hello.rb,v 1.1 2003/12/08 18:40:07 radek Exp $ 3 # $Source: /home/radek/cvs/ruby-book/example/net/web/borges02/hello.rb,v $ 4 # Just a simple hello application 5 # Copyright (c) 2003 Radek Hnilica 6 require 'borges' 7 require 'borges/Page' 8 9 include Borges 10 11 class Hello < Page 12 def render_on(r) 13 r.heading "Hello world!" 14 end 15 end 16 17 Borges::register_application("hello", Hello) 18 19 if $0 == __FILE__ then 20 require 'borges/Webrick' 21 Borges::WebrickConnection.start 22 end 23
Spustíme jej příkazem
$ ruby -w hello.rb
A prohlížeč nasměrujeme na http://localhost:7000/hello
Od verze 0.1.1 spolupracuje Broges s Apachem přes DRb. Vlastní stránka je pak v Apachi napsaná v mod_ruby
1 #!/usr/bin/env ruby 2 # $Id: hello.rbx,v 1.1 2003/12/08 18:40:07 radek Exp $ 3 # $Source: /home/radek/cvs/ruby-book/example/net/web/borges02/hello.rbx,v $ 4 require 'borges/ApacheDRbClient' 5 Borges::ApacheDRbClient.handle_with('druby://127.0.0.1:7001')
Aby stránka fungovala, musí být spuštěn server na který se odkazuje. Tento spustíme následujícím skriptem.
1 #!/usr/bin/env ruby -w 2 # $Id: hello_srv.rb,v 1.1 2003/12/08 18:40:07 radek Exp $ 3 # $Source: /home/radek/cvs/ruby-book/example/net/web/borges02/hello_srv.rb,v $ 4 require 'hello.rb' 5 require 'borges' 6 require 'borges/ApacheDRbServer' 7 8 Borges::register_application("borges/hello.rbx", Hello) 9 Borges::ApacheDRbServer.start('druby://127.0.0.1:7001') 10 Borges::ApacheDRbServer.install_INT_handler 11 puts "Borges DRb Server listening on #{DRb.uri}" 12 puts "Press Ctrl-c to quit" 13 DRb.thread.join 14
Zatím neodzkoušeno, nemám nainstalován DRb.
Havaruje se zprávou:
[Tue May 20 16:20:33 2003] [error] mod_ruby: error in ruby /home/radek/opt/ruby-1.8.0-2003.05.20/lib/ruby/site_ruby/1.8/borges/ApacheDRbClient.rb:23:in `handle_with': undefined method `contentType' for #<Borges::Response:0x403dc840> (NoMethodError) from /var/www/borges/hello.rbx:5 from /home/radek/opt/ruby-1.8.0-2003.05.20/lib/ruby/1.8/apache/ruby-run.rb:70:in `load' from /home/radek/opt/ruby-1.8.0-2003.05.20/lib/ruby/1.8/apache/ruby-run.rb:70:in `handler'
Na tak jednoduchém příkladu jako je hello.rb
toho moc neuvidíme. Skusíme něco přímo z příkladů od Broges
1 #!/usr/bin/env ruby 2 # $Id: counter.rb,v 1.1 2003/12/08 18:40:07 radek Exp $ 3 # $Source: /home/radek/cvs/ruby-book/example/net/web/borges02/counter.rb,v $ 4 require 'borges' 5 require 'borges/Page' 6 7 include Borges 8 9 class Counter < Page 10 def initialize 11 @counter = 0 12 end 13 14 def render_on(r) 15 r.heading @counter 16 r.anchor("++") { inc } 17 r.space 18 r.anchor("--") { dec } 19 end 20 21 def inc(); @counter += 1; end 22 def dec(); @counter -= 1; end 23 end 24 25 Borges::register_application("counter", Counter) 26 27 if $0 == __FILE__ then 28 require 'borges/Webrick' 29 Borges::WebrickConnection.start 30 end 31 32
Pokročíme k složitějšímu příkladu, přihlašovacímu formuláři
1 #!/usr/bin/env ruby 2 # $Id: login.rb,v 1.1 2003/12/08 18:40:07 radek Exp $ 3 # $Source: /home/radek/cvs/ruby-book/example/net/web/borges02/login.rb,v $ 4 # Copyright (c) 2003 Radek Hnilica 5 require 'borges' 6 require 'borges/Page' 7 8 include Borges 9 10 class Login < Page 11 def initialize 12 @username = nil 13 @password = nil 14 end 15 16 def render_on(r) 17 r.heading 'Login' 18 r.form do 19 r.print "Jm�no:" 20 r.text_input(''){|n| @username = n; puts "*> n= #{n}"} 21 r.print "<br>Heslo:" 22 r.text_input(''){|n| @password = n; puts "*> n= #{n}"} 23 r.print "<br>" 24 r.submit({'value'=>'P�ihl�sit'}) {login} 25 end 26 end 27 28 def login() 29 puts "*> Login as #{@username} with #{@password}" 30 end 31 end 32 33 Borges::register_application("login", Login) 34 35 if $0 == __FILE__ then 36 require 'borges/Webrick' 37 Borges::WebrickConnection.start({:Port=>7000}) 38 end 39
Po drobné úpravě v Borges, konkrétne po záplatě:
1 diff -rC3 borges-0.1.0/lib/borges/Renderer.rb borges-0.1.0.1/lib/borges/Renderer.rb 2 *** borges-0.1.0/lib/borges/Renderer.rb Sun Jan 26 02:28:30 2003 3 --- borges-0.1.0.radek/lib/borges/Renderer.rb Mon Feb 10 13:25:03 2003 4 *************** 5 *** 61,66 **** 6 --- 61,72 ---- 7 attrs["name"] = @callbacks.register_callback(update) 8 input("text", attrs) 9 end 10 + 11 + def password_input(value, attrs={}, &update) 12 + attrs["value"] = value 13 + attrs["name"] = @callbacks.register_callback(update) 14 + input("password", attrs) 15 + end 16 17 def checkbox(value, attrs={}, &update) 18 update_key = @callbacks.register_callback(lambda { |v|
který přidává metodu password_input
a přepsání s použitím tabulky vypadá přihlašovací stránka takto:
1 #!/usr/bin/env ruby 2 # $Id: login2.rb,v 1.1 2003/12/08 18:40:07 radek Exp $ 3 #�$Source: /home/radek/cvs/ruby-book/example/net/web/borges02/login2.rb,v $ 4 # Copyright (c) 2003 Radek Hnilica 5 require 'borges' 6 require 'borges/Page' 7 8 include Borges 9 10 class Login < Page 11 attr_reader :username 12 13 def initialize 14 @username = nil 15 @password = nil 16 end 17 18 def render_on(r) 19 r.tag('html') do 20 r.tag('head') do 21 r.print "<title>P�ihla�ovac� dialog varianta 2</title>\n" 22 r.print %q{<meta http-equiv="Content-Type"} 23 r.print %Q{content="text/html; charset=iso-8859-2">\n} 24 end 25 r.tag('body') do 26 r.form do 27 r.table({'frame'=>'border', 'align'=>'center', 'bgcolor'=>'#e0e8ff'}) do 28 r.tr do 29 r.td({'colspan'=>'2', 'align'=>'center', 'bgcolor'=>'#d0c0e0'}){ 30 r.print "<b>Pros�m, p�ihlaste se:</b>\n"} 31 end 32 r.tr do 33 r.td{r.print "Jm�no:"} 34 r.td{r.text_input(""){|n| @username = n;}} 35 end 36 r.tr do 37 r.td{r.print "Heslo:"} 38 r.td{r.password_input(""){|n| @password = n;}} 39 end 40 r.tr do 41 r.td({'colspan'=>'2', 'align'=>'center'}){ 42 r.submit({'value'=>'P�ihl�sit'}) {login}} 43 end 44 end 45 end 46 end 47 end 48 end 49 50 def login() 51 # Use user authorization code for login in 52 # This is simple fake code. 53 if @username == 'radek' and @password == 'pass' then 54 puts "#{@username}> P�ihl�enn� bylo �sp�n�." 55 else 56 puts "#{@username}> ERR: Bad password given!" 57 @username = @password = nil 58 end 59 end 60 end 61 62 Borges::register_application("login-two", Login) 63 64 if $0 == __FILE__ then 65 require 'borges/Webrick' 66 Borges::WebrickConnection.start({:Port=>7000}) 67 end 68 69 #Keep this comment at the end of the file 70 #Local variables: 71 #indent-tabs-mode: nil 72 #End: 73