Super simple serving of ruby pages

Hi. I apologize in advance if this is a dumb question, but though I've
searched online I haven't found an answer.

I'm starting to teach my 8yr daughter Ruby. We've covered HTML and CSS
already and she handcoded her own static website. I'd like to
incorporate her Ruby learning experience into her website, which is
much more interesting for her than using Ruby to write scripts or
desktop apps. She's just starting out, so Rails is much too complicated
for her. She's not ready for the whole MVC concept yet. What I'd like
to do is to run very simple Ruby scripts from the site and incorporate
Ruby code into rhtml files, but without Rails. I don't really want a
"framework", just the ability to run a ruby file that will serve up an
rhtml file. In other words, something very simple like PHP (I don't
want to teach her PHP). [I'll get into Rails later once she's more
advanced.]

ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

Thanks for the help.

Webrick has WEBrick::HTTPServlet::ERBHandler, which (I believe) serves
ERb pages. Mongrel probably has something similar.

···

On 9/20/06, zerohalo <zerohalo@gmail.com> wrote:

Hi. I apologize in advance if this is a dumb question, but though I've
searched online I haven't found an answer.

I'm starting to teach my 8yr daughter Ruby. We've covered HTML and CSS
already and she handcoded her own static website. I'd like to
incorporate her Ruby learning experience into her website, which is
much more interesting for her than using Ruby to write scripts or
desktop apps. She's just starting out, so Rails is much too complicated
for her. She's not ready for the whole MVC concept yet. What I'd like
to do is to run very simple Ruby scripts from the site and incorporate
Ruby code into rhtml files, but without Rails. I don't really want a
"framework", just the ability to run a ruby file that will serve up an
rhtml file. In other words, something very simple like PHP (I don't
want to teach her PHP). [I'll get into Rails later once she's more
advanced.]

ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

Thanks for the help.

--
- Simen

zerohalo wrote:

/ ...

ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

Just one question. Do you want the existing Web server to serve your Ruby
code, or do you require the server to be Ruby-based also?

If the former, you can tell Apache to serve Ruby scripts that contain the
usual CGI conventions. That seems simple enough. And it might not be what
you are asking for.

···

--
Paul Lutus
http://www.arachnoid.com

Webrick seems to have support for ERB - see httpservlet/erbhandler.rb,
that is used for .rhtml files by FileHandler. Just find in the
tutorials how to start webrick for a given document root
(Webrick::HttpServer.new({:DocumentRoot = '/var/www/'}) and
something?)

You can use meta_vars and query variables in your rhtml files.

···

On 9/20/06, zerohalo <zerohalo@gmail.com> wrote:

Hi. I apologize in advance if this is a dumb question, but though I've
searched online I haven't found an answer.

I'm starting to teach my 8yr daughter Ruby. We've covered HTML and CSS
already and she handcoded her own static website. I'd like to
incorporate her Ruby learning experience into her website, which is
much more interesting for her than using Ruby to write scripts or
desktop apps. She's just starting out, so Rails is much too complicated
for her. She's not ready for the whole MVC concept yet. What I'd like
to do is to run very simple Ruby scripts from the site and incorporate
Ruby code into rhtml files, but without Rails. I don't really want a
"framework", just the ability to run a ruby file that will serve up an
rhtml file. In other words, something very simple like PHP (I don't
want to teach her PHP). [I'll get into Rails later once she's more
advanced.]

ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

Thanks for the help.

Does this help?
http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html

···

On 9/20/06, zerohalo <zerohalo@gmail.com> wrote:

I don't really want a
"framework", just the ability to run a ruby file that will serve up an
rhtml file. In other words, something very simple like PHP (I don't
want to teach her PHP). [I'll get into Rails later once she's more
advanced.]

zerohalo wrote:

Hi.

Hi.

She's just starting out, so Rails is much too complicated
for her. She's not ready for the whole MVC concept yet.

FWIW, I have the brain of an 8yr old in when it comes
to web-aps, and I managed to write a /couple/ Camping aps
the other evening.

Regards,

···

--
Bil Kleb
http://fun3d.larc.nasa.gov

zerohalo wrote:

Hi. I apologize in advance if this is a dumb question, but though I've
searched online I haven't found an answer.

I'm starting to teach my 8yr daughter Ruby. We've covered HTML and CSS
already and she handcoded her own static website. I'd like to
incorporate her Ruby learning experience into her website, which is
much more interesting for her than using Ruby to write scripts or
desktop apps. She's just starting out, so Rails is much too complicated
for her. She's not ready for the whole MVC concept yet. What I'd like
to do is to run very simple Ruby scripts from the site and incorporate
Ruby code into rhtml files, but without Rails. I don't really want a
"framework", just the ability to run a ruby file that will serve up an
rhtml file. In other words, something very simple like PHP (I don't
want to teach her PHP). [I'll get into Rails later once she's more
advanced.]

ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

I've an example of this that's ~100 lines of code, if you're interested.

Maps URLs to classes and templates and renders them, using erb and Webrick.

···

--
James Britt

"A principle or axiom is of no value without the rules for applying it."
   - Len Bullard

This works fine:

eRuby: The Erb Alternative to Embedded Ruby in HTML
http://www.hiveminds.co.uk/node/3105

It is extremly easy to setup, I have tried this solution in 2 shared
hosting services without a problem.

···

--
Aníbal Rojas

zerohalo wrote:

Hi. I apologize in advance if this is a dumb question, but though I've
searched online I haven't found an answer.

I'm starting to teach my 8yr daughter Ruby. We've covered HTML and CSS
already and she handcoded her own static website. I'd like to
incorporate her Ruby learning experience into her website, which is
much more interesting for her than using Ruby to write scripts or
desktop apps. She's just starting out, so Rails is much too complicated
for her. She's not ready for the whole MVC concept yet. What I'd like
to do is to run very simple Ruby scripts from the site and incorporate
Ruby code into rhtml files, but without Rails. I don't really want a
"framework", just the ability to run a ruby file that will serve up an
rhtml file. In other words, something very simple like PHP (I don't
want to teach her PHP). [I'll get into Rails later once she's more
advanced.]

ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

Thanks for the help.

Paul Lutus wrote:

Just one question. Do you want the existing Web server to serve your Ruby
code, or do you require the server to be Ruby-based also?

No, the server doesn't need to be Ruby-based. I could use lighttpd or
apache to serve the code.

If the former, you can tell Apache to serve Ruby scripts that contain the
usual CGI conventions. That seems simple enough. And it might not be what
you are asking for.

That's the part I'm missing. I find Apache extremely difficult to
configure (beyond the basics). How would I tell Apache to serve Ruby
scripts?

Thanks.

Jan Svitok wrote:

Webrick seems to have support for ERB - see httpservlet/erbhandler.rb,
that is used for .rhtml files by FileHandler. Just find in the
tutorials how to start webrick for a given document root
(Webrick::HttpServer.new({:DocumentRoot = '/var/www/'}) and
something?)

Yes, I tried that. Unfortunately there is no documentation on the
webrick site on how to use ERBHandler. I have this in a ruby script
which does start up the Webrick server. However, while it serves HTML
files fine, it treats RHTML files as binaries and RB files as just
plain text. Here's my code:

<pre><code>
require 'webrick'
include WEBrick

def start_webrick(config = {})
  config.update(:Port => 8080)
  server = HTTPServer.new(config)
  yield server if block_given?
  ['INT', 'TERM'].each {|signal|
    trap(signal) {server.shutdown}
  }
  ruby_dir = File.expand_path('/data/sandbox/ruby')
  server.mount("/data/sandbox/ruby", HTTPServlet::ERBHandler, ruby_dir)
  server.start
end

start_webrick(:DocumentRoot => '/data/sandbox/ruby')
</code></pre>

Wilson Bilkovich wrote:

Does this help?
http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html

Indeed it did. Thanks. That works nicely.

Bil Kleb wrote:

FWIW, I have the brain of an 8yr old in when it comes
to web-aps, and I managed to write a /couple/ Camping aps
the other evening.

Fascinating. I've looked at Camping but haven't really
grasped it yet.

I'm not a web guy. I do it slowly and painfully.

I also have the brain of an eight year old. I keep it
in a jar on my desk.

Hal

I'd be interested in seeing that James.

Thanks
-Ezra

···

On Sep 20, 2006, at 8:05 PM, James Britt wrote:

zerohalo wrote:

Hi. I apologize in advance if this is a dumb question, but though I've
searched online I haven't found an answer.
I'm starting to teach my 8yr daughter Ruby. We've covered HTML and CSS
already and she handcoded her own static website. I'd like to
incorporate her Ruby learning experience into her website, which is
much more interesting for her than using Ruby to write scripts or
desktop apps. She's just starting out, so Rails is much too complicated
for her. She's not ready for the whole MVC concept yet. What I'd like
to do is to run very simple Ruby scripts from the site and incorporate
Ruby code into rhtml files, but without Rails. I don't really want a
"framework", just the ability to run a ruby file that will serve up an
rhtml file. In other words, something very simple like PHP (I don't
want to teach her PHP). [I'll get into Rails later once she's more
advanced.]
ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

I've an example of this that's ~100 lines of code, if you're interested.

Maps URLs to classes and templates and renders them, using erb and Webrick.

--
James Britt

I'd like to see it, just because I might find it educational.

I've also been wondering if framework support for this sort of thing, but very simple to setup, would be useful to people?

Something akin to this:

app.rb

···

On Thu, 21 Sep 2006, James Britt wrote:

ERb looks like the ticket, but there's a missing link (for me). I can
write a ruby script that generates an rhtml file, but then how do I
serve that rhtml file without a framework like
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,
but I'm missing the connection. I can't find a simple tutorial anywhere
online, and I'm no expert on web applications.

I've an example of this that's ~100 lines of code, if you're interested.

Maps URLs to classes and templates and renders them, using erb and Webrick.

-----
require 'iowa'

Iowa.run
-----

app.cnf
-----
socket:
   hostname: foo.bar.com
   port: 3000
application:
   dispatcher:
     class: StandardDispatcher
     mapfile: mapfile.cnf
-----

mapfile.cnf:
-----
:map:
   /index.html: Index
   /form.html: Myform
   /response.html: Myresponse
-----

If one had an index.rhtml, myform.rhtml, and myresponse.rhtml, they would be mapped to classes Index, Myform, and Myresponse, and requests for the urls above would go to the class in question and be rendered as an erb template.

You'd run it with:

ruby app.rb -r webrick
or
ruby app.rb -r mongrel

No other code or config necessary to serve those rhtml files. Would anyone use this ability (as I personally would not). Worth my time to make the above work?

Kirk haines

Paul Lutus wrote:
> Just one question. Do you want the existing Web server to serve your Ruby
> code, or do you require the server to be Ruby-based also?
>

No, the server doesn't need to be Ruby-based. I could use lighttpd or
apache to serve the code.

> If the former, you can tell Apache to serve Ruby scripts that contain the
> usual CGI conventions. That seems simple enough. And it might not be what
> you are asking for.

That's the part I'm missing. I find Apache extremely difficult to
configure (beyond the basics). How would I tell Apache to serve Ruby
scripts?

In a recent thread (eruby?) someone mentioned adding

AddType application/x-httpd-eruby .rhtml
Action application/x-httpd-eruby /cgi-bin/eruby

to httpd.conf

In my win installation there is c:/ruby/bin/erb.bat so I suppose there
is something similar in unix. Now either add #!/usr/bin/erb to the top
of any .cgi file or, preferably, do something similar to the above,
somehow telling apache to run erb.

(I'm not an apache guru, so these are just hints to get you started...)

···

On 9/20/06, zerohalo <zerohalo@gmail.com> wrote:

require 'webrick'
include WEBrick

def start_webrick(config = {})
  config.update(:Port => 8080)
+ config.update(:MimeTypes => {'rhtml' => 'text/html'})
  server = HTTPServer.new(config)
  yield server if block_given?
  ['INT', 'TERM'].each {|signal|
    trap(signal) {server.shutdown}
  }
- ruby_dir = File.expand_path('/data/sandbox/ruby')
- server.mount("/data/sandbox/ruby", HTTPServlet::ERBHandler, ruby_dir)
  server.start
end

start_webrick(:DocumentRoot => '/data/sandbox/ruby')

···

On 9/20/06, zerohalo <zerohalo@gmail.com> wrote:

Yes, I tried that. Unfortunately there is no documentation on the
webrick site on how to use ERBHandler. I have this in a ruby script
which does start up the Webrick server. However, while it serves HTML
files fine, it treats RHTML files as binaries and RB files as just
plain text. Here's my code:

On Debian and Ubuntu, it just works. After you apt-get install
apache2, you just copy some foo.rb file (maybe like the one shown
below) to /usr/lib/cgi-bin/ and then point your browser at
http://localhost/cgi-bin/foo.rb and boom. :slight_smile:

==== snip /usr/lib/cgi-bin/foo.rb ====
#!/opt/ruby/bin/ruby -w

puts "Content-type: text/html"
puts

puts "<html><body>
<p>The time is now #{Time.now}, so you'd
better get crackin'!
</p>
</body>
</html>"
==== /snip ====

---John

···

On 9/20/06, zerohalo <zerohalo@gmail.com> wrote:

[snip]

> If the former, you can tell Apache to serve Ruby scripts that contain the
> usual CGI conventions. That seems simple enough. And it might not be what
> you are asking for.

That's the part I'm missing. I find Apache extremely difficult to
configure (beyond the basics). How would I tell Apache to serve Ruby
scripts?

zerohalo wrote:

Paul Lutus wrote:

Just one question. Do you want the existing Web server to serve your Ruby
code, or do you require the server to be Ruby-based also?

No, the server doesn't need to be Ruby-based. I could use lighttpd or
apache to serve the code.

If the former, you can tell Apache to serve Ruby scripts that contain the
usual CGI conventions. That seems simple enough. And it might not be what
you are asking for.

That's the part I'm missing. I find Apache extremely difficult to
configure (beyond the basics). How would I tell Apache to serve Ruby
scripts?

Others have offered good advice. The simplest way is to give the server what
it expects to see:

···

---------------------------------------

#!/usr/bin/ruby -w

print "Content-type: text/html\r\n\r\n"

print "<html><body><center><h1>Hello world!</h1></center></body></html>"

---------------------------------------

Put this file in a directory where executable content is expected to be,
like /serverpath/cgi-bin, and chances are Apache will treat it as you would
expect, without any special configuration.

--
Paul Lutus
http://www.arachnoid.com

Ezra Zygmuntowicz wrote:

I've an example of this that's ~100 lines of code, if you're interested.

Maps URLs to classes and templates and renders them, using erb and Webrick.

--
James Britt

I'd be interested in seeing that James.

Here ya go!

http://jamesbritt.com/code/wb_erb.tgz

Fairly simple, few features, demos a few concepts.

···

On Sep 20, 2006, at 8:05 PM, James Britt wrote:

--
James Britt

"Simplicity of the language is not what matters, but
simplicity of use."
  - Richard A. O'Keefe in squeak-dev mailing list

Thanks, Jan. That was easy. Now .rhtml files render correctly.

Is there a way to tell it to execute .rb files rather than treat them
as simple text files?

Jan Svitok wrote:

···

On 9/20/06, zerohalo <zerohalo@gmail.com> wrote:
> Yes, I tried that. Unfortunately there is no documentation on the
> webrick site on how to use ERBHandler. I have this in a ruby script
> which does start up the Webrick server. However, while it serves HTML
> files fine, it treats RHTML files as binaries and RB files as just
> plain text. Here's my code:

require 'webrick'
include WEBrick

def start_webrick(config = {})
  config.update(:Port => 8080)
+ config.update(:MimeTypes => {'rhtml' => 'text/html'})
  server = HTTPServer.new(config)
  yield server if block_given?
  ['INT', 'TERM'].each {|signal|
    trap(signal) {server.shutdown}
  }
- ruby_dir = File.expand_path('/data/sandbox/ruby')
- server.mount("/data/sandbox/ruby", HTTPServlet::ERBHandler, ruby_dir)
  server.start
end

start_webrick(:DocumentRoot => '/data/sandbox/ruby')