It's hard to argue that WEBrick is good at documentation. So, there are other servers. But how many come built-in? None.
As a result, lot of my projects use WB. It's a pain in the ass. It's a very verbose and painful way to write code, especially when you're trying to test an idea that doesn't quite agree with WB's way of doing things.
That's when you need to assert yourself as a human, and write in English: server.path "/" do |name| if not posted? :name then write "You need to POST name!" else write "Hi there, %s" % name end end
Wouldn't that just be great? HumaneBrick does that. You can practically write in English, if you do things right. See the humane-examples.rb
file at the end if you really want a treat. As for the code, it manages to hold itself at 80 lines of code, without any extra compression. Enjoy.
Okay, maybe meticulous isn't the right word. But I digress.
Server#path(url, &block)
This is the big one. Give it a URL and a block and let her rip. If you've got stuff POSTed to your page, then have at'em, in alphabetical order.
That's the real one you need to worry about. But if you're interested, there are a bunch of helpers out there.
write(parse,text)
drops the text into the browser's lap. Setparse
to true if you want auto-paragraph/header wrappings, or just ignore it and have your words repeated verbatim.redirect_to(url)
sends the browser to the new location.url
hands you the path (ex, "/index.html")posted?(varname)
is kind of important, since it'll tell you whether or not a certain variable was POSTed- There are times you don't want to talk HTML (say, a JSON response). For that, to ahead and set
@format
to the desired value (seeHB.Content_Types
)
Fine, let's talk cookies while we're here too:
add_cookie(name,value)
makes a new oneread_cookie(name)
will give them back- If you feel like holding onto some cookies instead of passing them back to the browser, make sure to call
do_not_save_cookies
for good measure. Otherwise, they'll be saved & sent back.
There's another handy utility if you don't feel like writing it all in the current file: view(name)
loads HUMANEDIR/views/name.@format
, for either executing or write
ing
If you really want to, you can add new content types with HB.content_type(hash)
(for example, content_type :js => "text/javascript"
)
Nothing to see, really. Go back to writing fun code again.