Hello,
first of all, congratulations for TBNL :-)
Here's a small macro (with a big ugly name) for TBNL, that allows to bind portions of a request URI to arguments passed to dispatch functions. (see attachment)
I'm using it to create RESTful [1] URIs and web APIs for a web application I'm playing with. For example, let's say I want to publish a news archive. The "usual" way for accessing the news for August 27th, 2005 would be an URI like:
http://www.news.tld/archive?year=2005&month=08&day=27
The proposed add-on allows to easily (well, actually *more* easily) manage URIs like this:
http://www.news.tld/archive/2005/08/27/
The task is performed by a macro called create-groups-bind-regex-dispatcher, which takes three arguments:
1. a CL-PPCRE regex (a string, an s-expression or a scanner) with one or more register groups. It will be matched against the request URI;
2. a list of variable names that will be bound to the register groups above iff the regex matches;
3. a dispatch function that accepts keyword arguments named like the variables above.
A code sample for the news archive: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (defun archive-page (&key year month day) "Archive page" (format nil "Year: ~A; Month: ~A; Day: ~A" year month day))
(setq *dispatch-table* (list (create-groups-bind-regex-dispatcher "^\/archive\/(\d{4})\/(\d{1,2})\/(\d{1,2})\/?$" (year month day) archive-page) #'default-dispatcher)) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Under the hood, the list of variables provided to create-groups-bind-regex-dispatcher is used with cl-ppcre:register-groups-bind --- so you can apply some voodoo to the matched variables: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (defun archive-page (&key year month day) "Archive page with integer keyword parameters" (format nil "(+ year month day) => ~D" (+ year month day)))
(setq *dispatch-table* (list (create-groups-bind-regex-dispatcher "^\/archive\/(\d{4})\/(\d{1,2})\/(\d{1,2})\/?$" ((#'parse-integer year month day)) archive-page) #'default-dispatcher)) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
That's all. I hope it will be useful for you as well...
Comments are welcome (please don't be too rude, it's one of my first attempts with CL :-)
Regards,
alceste
References: [1] http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
Hi!
Sorry for the delay.
On Sat, 27 Aug 2005 17:11:41 +0200, Alceste Scalas alceste.scalas@gmx.net wrote:
first of all, congratulations for TBNL :-)
Thanks... :)
Here's a small macro (with a big ugly name) for TBNL, that allows to bind portions of a request URI to arguments passed to dispatch functions. (see attachment)
The macro is fine and I have in fact used something similar myself in applications I wrote. I'm a bit hesitant to add it to TBNL, though, as I'm not sure if this functionality belongs there. My gut feeling is that macros like yours are one level of abstraction above TBNL and should be in a separate library.
What do others think about this?
Cheers, Edi.
[Sorry Edi, you're getting this at least twice. ]
On Aug 29, 2005, at 6:49 PM, Edi Weitz wrote:
Hi!
Sorry for the delay.
On Sat, 27 Aug 2005 17:11:41 +0200, Alceste Scalas alceste.scalas@gmx.net wrote:
first of all, congratulations for TBNL :-)
Thanks... :)
Here's a small macro (with a big ugly name) for TBNL, that allows to bind portions of a request URI to arguments passed to dispatch functions. (see attachment)
The macro is fine and I have in fact used something similar myself in applications I wrote. I'm a bit hesitant to add it to TBNL, though, as I'm not sure if this functionality belongs there. My gut feeling is that macros like yours are one level of abstraction above TBNL and should be in a separate library.
What do others think about this?
Interesting question. This macro, likely, and the directory file stuff I posted a few weeks ago are both arguably a little higher level than TBNL. On the other hand, these (certainly the directory serving stuff) are not really useful outside of TBNL. Maybe a contributions directory? If this stuff isn't kept with TBNL I don't know how anyone is going to find it.
Cheers, Bob
Cheers, Edi. _______________________________________________ tbnl-devel site list tbnl-devel@common-lisp.net http://common-lisp.net/mailman/listinfo/tbnl-devel
---- Bob Hutchison -- blogs at http://www.recursive.ca/hutch/ Recursive Design Inc. -- http://www.recursive.ca/ Raconteur -- http://www.raconteur.info/