This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
capotej
authored and
capotej
committed
Jul 20, 2009
0 parents
commit 9e30415
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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"] %> | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> | |||
|