https://github.com/technicalpickles/ragamuffin
I'm a bad, bad man
https://github.com/technicalpickles/ragamuffin
Last synced: over 1 year ago
JSON representation
I'm a bad, bad man
- Host: GitHub
- URL: https://github.com/technicalpickles/ragamuffin
- Owner: technicalpickles
- Created: 2009-11-25T21:54:50.000Z (over 16 years ago)
- Default Branch: master
- Last Pushed: 2013-04-16T19:56:26.000Z (about 13 years ago)
- Last Synced: 2025-03-15T00:30:00.942Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 105 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
Java Servlets are the JEE way of doing anything web based. They are pretty easy to write. Here's short example to read a query parameter, and display it.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ExampleServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
String DATA = request.getParameter("DATA");
if(DATA != null){
out.println(DATA);
} else {
out.println("No text entered.");
}
out.close();
}
}
And don't forget to set it up in web.xml:
example
ExampleServlet
example
/example
All pretty straight forward. What if we could harness this technique in the Ruby world? Rack is a minimal framework to make talking to a Ruby webserver nicer, and is pretty flexible and great to build frameworks on top of. Imagine if you would, an API like Java Servlets... but implemented on rack....
I present to you... racklets! Let's start by making a new ruby file, and putting it in a well known load path: WEB-INF/lib. So, we'll make WEB-INF/lib/example_racklet.rb :
require 'ragamuffin'
class ExampleRacklet < Ragamuffin::Racklet
def do_get(request, response)
data = request.params['DATA']
if data
response.write data
else
response.write 'No data'
end
end
end
Next we need a web.xml to tell racklets what to load, and what urls to map it. This is kept in WEB-INF/web.xml:
example
ParamRacklet
example
/example
Since we're on rack, you'll want a config.ru to run our ragamuffin web application.
require 'ragamuffin'
run Ragamuffin::WebApplication.new
This handles loading up the web.xml and updating $LOAD_PATH to include the WEB-INF/lib directory. So now we're in business to something like shotgun to run our server:
$ shotgun
== Shotgun starting Rack::Handler::Mongrel on localhost:9393
Alternatively, you can use the included `ragamuffin` script to run it without a config.ru file.
$ bin/ragamuffin
== Ragamuffin starting Rack::Handler::Mongrel on localhost:9393
Now go ahead and browse to http://localhost:9393/example and you'll see:
No Data
Give it the DATA parameter, and you'll see it displayed, ie http://localhost:9393/example?DATA=Hello%20World
Hello World