Ruby CGI example
Searching for examples of ruby/cgi usage didn't turn up much.
I had to rough it and unfortunately, it took longer than it should have to come up with what I wanted to accomplish. (well, more or less what I wanted, well, actually, it's not going to work for what I need but anyway, here's an working ruby/cgi example).
thought I'd share:
require 'cgi'
require 'json'
cgi = CGI.new('html4Tr') # add HTML generation methods
if cgi.params.empty?
# no request recieved, return html
cgi.out('charset' => 'UTF-8') do
cgi.html() do
cgi.head() do
"\n" +
cgi.title{"cgi testing"} +
CGI.pretty(
"\n" + %q(<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="Mon, 22 Jul 2002 11:12:01 GMT">
<meta http-equiv="pragma" content="no-cache">
<script type="text/javascript" src="../js/jquery-1.3.2.js">)
)
end +
cgi.body() do
CGI.pretty(
"\n" + %q(<input type='hidden' id="ids" value='' />
<!-- start: show results -->
<div id="show_hide_container" style="display:none"><a id="hide_n_show">[-]</a></div>
<div id="action_results"></div>
<!-- end: show results -->
<div id="display_results">
<div class="show_demo" id="unique" style="height:100%"></div>
</div>)
) \
+
cgi.form() do
cgi.textarea("get_text") +
cgi.br +
cgi.submit
end +
cgi.pre() do
CGI::escapeHTML(
"params: " + cgi.params.inspect + "\n" +
"cookies: " + cgi.cookies.inspect + "\n" +
ENV.collect() do |key, value|
key + " --> " + value + "\n"
end.join("")
)
end
end
end
end
else
# request recieved, return as JSON
cgi = CGI.new(:encoding => 'UTF-8')
print cgi.header('type' => 'application/x-javascript', 'charset' => 'UTF-8')
print cgi.params.to_json
end
