Advice on bulding a web service

Discussion in 'Ruby' started by Doug Livesey, Aug 24, 2007.

  1. Doug Livesey

    Doug Livesey Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Doug Livesey, Aug 24, 2007
    #1
    1. Advertising

  2. Doug Livesey

    Jeremy Wells Guest

    Doug Livesey wrote:
    > 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.
    >

    maybe you'd enjoy camping http://code.whytheluckystiff.net/camping/
     
    Jeremy Wells, Aug 24, 2007
    #2
    1. Advertising

  3. Doug Livesey

    Doug Livesey Guest

    Jeremy Wells wrote:
    > Doug Livesey 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/


    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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Doug Livesey, Aug 24, 2007
    #3
  4. Doug Livesey

    vasudevram Guest

    On Aug 24, 6:44 pm, Doug Livesey <> wrote:
    > Jeremy Wells wrote:
    > > Doug Livesey wrote:
    > > 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. >
    > > maybe you'd enjoy campinghttp://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.
    > --
    > 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
    -----------------------------------------
    Vasudev Ram
    http://www.dancingbison.com
    http://jugad.livejournal.com
    http://sourceforge.net/projects/xtopdf
    -----------------------------------------
     
    vasudevram, Aug 24, 2007
    #4
  5. Doug Livesey

    Doug Livesey Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Doug Livesey, Aug 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Degtiariov

    Bulding python module using C

    Andrew Degtiariov, Sep 24, 2004, in forum: Python
    Replies:
    2
    Views:
    260
    Andrew Degtiariov
    Sep 24, 2004
  2. hobel
    Replies:
    0
    Views:
    290
    hobel
    Sep 22, 2006
  3. Roedy Green

    Encapsulating HashMap bulding

    Roedy Green, May 10, 2010, in forum: Java
    Replies:
    15
    Views:
    557
  4. Leo Violette
    Replies:
    0
    Views:
    1,108
    Leo Violette
    Apr 17, 2009
  5. Nick Brown
    Replies:
    3
    Views:
    102
    elise huard
    Nov 6, 2010
Loading...

Share This Page