# Advice on bulding a web service

**URL:** <https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899>\
**Category:** Ruby\
**Created:** [August 24, 2007, 9:04am UTC](https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899 "2007-08-24T09:04:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [August 24, 2007, 9:04am UTC](https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899/1 "2007-08-24T09:04:11Z")

</div>

Hallo.  
I need to build a web server (it’ll be RESTful & everything), but am not  
sure about the best way to proceed.  
It will be a single instance app, that needs to strictly control  
concurrency, ie only one instance of an object can be manipulated at  
once.  
The built-in web service support of a full-fledged Rails app is not what  
I’m after.  
Basically, I want to be able to set the service going, then have other  
apps calling it (I’ll probably use JSON) for data. At first, setting it  
off in a command prompt will do (sadly, I work in Windows).  
What I’m after, really, is advice on the best libraries to use, and  
maybe pointers to tutorials that achieve the sort of thing that I’m  
after.  
Thanks in advance,  
Doug.

---

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [August 24, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899/2 "2007-08-24T15:20:54Z")

</div>

Doug L. wrote:

> off in a command prompt will do (sadly, I work in Windows).  
> What I’m after, really, is advice on the best libraries to use, and  
> maybe pointers to tutorials that achieve the sort of thing that I’m  
> after.  
> Thanks in advance,  
> Doug.

maybe you’d enjoy camping [http://code.whytheluckystiff.net/camping/](http://code.whytheluckystiff.net/camping/)

---

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [August 24, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899/3 "2007-08-24T15:44:45Z")

</div>

Jeremy W. wrote:

> Doug L. wrote:
> 
> > off in a command prompt will do (sadly, I work in Windows).  
> > What I’m after, really, is advice on the best libraries to use, and  
> > maybe pointers to tutorials that achieve the sort of thing that I’m  
> > after.  
> > Thanks in advance,  
> > Doug.
> 
> maybe you’d enjoy camping [http://code.whytheluckystiff.net/camping/](http://code.whytheluckystiff.net/camping/)

Cheers for that – I don’t think (after a quick semi-peruse of the page)  
that this is for writing web servers (that receive & send JSON, for  
example, & never render to a screen).  
Although please correct me if I’m wrong.  
No, the app I have in mind could well become quite complex internally –  
it’s just a web service, not a web site.  
Although, Camping does look rather cool!  
Cheers again,  
Doug.

---

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [August 24, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899/4 "2007-08-24T16:14:01Z")

</div>

On 8/24/07, Doug L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## apps calling it (I’ll probably use JSON) for data. At first, setting it off in a command prompt will do (sadly, I work in Windows). What I’m after, really, is advice on the best libraries to use, and maybe pointers to tutorials that achieve the sort of thing that I’m after. Thanks in advance, Doug.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Sounds like you might be after a messaging server rather an HTTP server  
(although there’s nothing conceptually wrong with using HTTP for a job  
like  
this). Look at stompserver.

---

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [August 24, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899/5 "2007-08-24T18:16:24Z")

</div>

On Aug 24, 6:44 pm, Doug L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Basically, I want to be able to set the service going, then have other  
> > that this is for writing web servers (that receive & send JSON, for  
> > example, & never render to a screen).  
> > Although please correct me if I’m wrong.  
> > No, the app I have in mind could well become quite complex internally –  
> > it’s just a web service, not a web site.  
> > Although, Camping does look rather cool!  
> > Cheers again,  
> > Doug.  
> > –  
> > Posted viahttp://www.ruby-forum.com/.

You have at least a couple of options for the web server (I’m assuming  
you’re mainly interested in writing your app, not implementing a web  
server):

1. WEBRick
2. Mongrel

Both of these support ‘servlets’ - not the same as Java servlets, as  
in, not the exact same API as the servlet API, but something roughly  
similar (though their API’s are much simpler and smaller in size).

In outline, do something like this:

- Create an instance of a server (see the WEBrick / Mongrel docs for  
the exact class name).
- Write your app-specific classes, each of which you want to expose as  
a REST resource, i.e. give them Ruby methods corresponding to the HTTP  
methods GET, POST, PUT, DELETE, etc.
- Write a ‘controller’ class that reads the request URI and decides  
which of your app-specific classes to delegate the request to. E.g. in  
its doGET method, delegate to the ‘get’ method of the appropriate  
class (determined by checking the incoming request URI). If the method  
is GET, the parameters in the URI should be passed as arguments to  
your class’s ‘get’ method, including an optional one that says what  
format the client wants the response in (e.g.: HTML/XML/JSON).
- ‘Mount’ this class on the server.
- Start the server.
- For each request that comes in, your controller gets control, does  
as said above (delegates the request), and then the the return value  
from the appropriate method of your class, as HTML/XML/JSON. is sent  
to the client as the response.  
Note: The above approach is for if you have multiple app-specific  
classes / instances that you want to expose as REST resources. If you  
have only one, things can be simpler - the ‘controller’ class itself  
can be the main or only app-specific class. Then, in its doGET method,  
just return the representation of the resource, appropriately  
formatted (as HTML/XML/JSON).

On the client end, you’ll have to read and parse the response, and do  
whatever you want to, with it. (If your client is a browser, you may  
not have to do anything. Just let the user interact with the server  
using the browser, e.g. clicking links in the response.) E.g. Display  
it, allow the user to interact with the data shown, including clicking  
links that trigger new REST requests, etc. For parsing the XML you can  
use REXML, for JSON, there’s a JSON library for Ruby (ruby-json? -  
GIYF).  
For sending the requests, use open-uri or Net:HTTP.

(The WEBrick and Mongrel API’s provide support for some of the above  
steps, i.e. creating the server and mounting your classes on it. The  
rest is app-specific and up to you.)

The book “RESTful Web Services” from O’Reilly, by Leonard Richardson  
and Sam Ruby, is a great resource for what you’re trying to do (IMO).  
I got it recently and have read some of it. If you can, get it, then  
it should be a snap to do what you need.

Also read the WEBRick or Mongrel docs for their server and servlet  
classes. GIYF again …

The book authors mention some limitations with open-uri for REST work  
(doesn’t support all HTTP methods, among other things), which they fix  
by writing another library - rest-open-uri - on top of it, which you  
can probably get from the source code link for the book at the book’s  
web page on the O’Reilly site.

Also, ‘Enterprise Integration with Ruby’ by Maik Schmidt, from the  
Pragmatic Bookshelf, has some good examples of writing WEBrick  
servlets.

HTH  
Vasudev

---

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [August 28, 2007, 9:08am UTC](https://www.ruby-forum.com/t/advice-on-bulding-a-web-service/112899/6 "2007-08-28T09:08:04Z")

</div>

Vasudev – thankyou for an incredibly informative and complete reply!  
I have both the books you mentioned, although I haven’t quite got around  
to reading the RESTful one, but I shall be diving in, now!  
And your suggested procedure for writing a web server has given me a  
much better idea of the sorts of things I need to be looking into.  
Thanks again for a fantastic reply!  
Doug.
