Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

init

  • Loading branch information...
commit 9e304151e8da1d5a2cb8035e0cf7630eb56edfb5 0 parents
capotej authored
Showing with 77 additions and 0 deletions.
  1. +47 −0 config.ru
  2. +12 −0 form.rb
  3. +18 −0 index.rb
47 config.ru
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'erb'
+require 'rack'
+
+class Joke
+
+ #print_r and vardump for completeness
+ def vardump(o)
+ o.inspect
+ end
+
+ def print_r(o)
+ o.inspect
+ end
+
+ #support for array('s',1,4);
+ def array(*arr)
+ arr
+ end
+
+ #support for <? ?>
+ def _preprocess(str)
+ str = str.gsub('<?', '<%')
+ str = str.gsub('?>', '%>')
+ end
+
+ #the rack interface
+ def call(env)
+ req = Rack::Request.new(env)
+ path = env['REQUEST_PATH'].sub('/','')
+ if path == ''
+ path = 'index.rb'
+ end
+ if File.exists?(path)
+ # $_GET, $_POST and $_REQUEST support
+ eval('$_REQUEST,$_' << env['REQUEST_METHOD'] << '= req.params,req.params')
+ response = ERB.new(_preprocess(File.read(path))).result(binding)
+ # ensure data doesn't stick around between requests
+ $_GET, $_POST, $_REQUEST = nil
+ [200, {"Content-Type" => "text/html", "Content-Length" => response.length.to_s}, response]
+ else
+ [404, {"Content-Type" => "text/plain", "Content-Length" => '3'}, '404']
+ end
+ end
+end
+
+run Joke.new
12 form.rb
@@ -0,0 +1,12 @@
+<h1>Parameter Testing:</h1>
+$_GET:<br/>
+<?= print_r($_GET) ?><br/>
+$_POST:<br/>
+<?= print_r($_POST) ?><br/>
+$_REQUEST:<br/>
+<%= print_r($_REQUEST)?><br/>
+<br/>
+
+<h1>Form result:</h1>
+Your name is <%= $_POST["first_name"]%> <%= $_POST["last_name"] %>
+
18 index.rb
@@ -0,0 +1,18 @@
+<h1>Parameter Testing:</h1>
+$_GET:<br/>
+<?= print_r($_GET) ?><br/>
+$_POST:<br/>
+<?= print_r($_POST) ?><br/>
+$_REQUEST:<br/>
+<%= print_r($_REQUEST)?><br/>
+<br>
+
+array(1,2,3) = <%= print_r(array(1,2,3)) %>
+
+<h1>Forms:</h1>
+<form action="form.rb" method="post">
+First Name: <input type="text" name="first_name">
+Last Name: <input type="text" name="last_name">
+<input type="submit" value="submit">
+</form>
+
Please sign in to comment.
Something went wrong with that request. Please try again.