Changeset 16094 in project


Ignore:
Timestamp:
09/26/09 21:08:00 (10 years ago)
Author:
Moritz Heidkamp
Message:

uri-match 0.2

  • matcher now also accepts uri-reference objects
  • added license header
Location:
release/4/uri-match
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • release/4/uri-match/tests/run.scm

    r16093 r16094  
    22
    33(import uri-match)
    4 (use test srfi-1 data-structures)
     4(use test srfi-1 data-structures uri-common)
    55
    66(test-group "routes creation"
     
    2525(test-group "basic matching"
    2626
    27   (test-assert (equal? "this is the body"
    28                        ((uri-match 'get "/" (make-routes '((get "this is the body")))))))
     27  (test "this is the body"
     28        ((uri-match 'get "/" (make-routes '((get "this is the body"))))))
     29
     30  (test "against the path of a uri-reference" "something!"
     31        ((uri-match 'get (uri-reference "http://foo/bar") (make-routes '(("/bar" (get "something!")))))))
    2932
    3033  (test-assert (not (uri-match 'get "/" (make-routes '((post "won't reach me"))))))
  • release/4/uri-match/uri-match.meta

    r15906 r16094  
    11((egg "uri-match.egg")
    22 (synopsis "A flexible URI matcher")
     3 (doc-from-wiki)
    34 (depends uri-common)
    45 (author "Moritz Heidkamp")
  • release/4/uri-match/uri-match.scm

    r16093 r16094  
     1;; A flexible URI matcher
     2;;
     3;; Copyright (C) 2009 Moritz Heidkamp
     4;;
     5;; This program is free software; you can redistribute it and/or
     6;; modify it under the terms of the GNU General Public License as
     7;; published by the Free Software Foundation; either version 3 of the
     8;; License, or (at your option) any later version.
     9;;
     10;; This program is distributed in the hope that it will be useful, but
     11;; WITHOUT ANY WARRANTY; without even the implied warranty of
     12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
     13;; General Public License for more details.
     14;;
     15;; You can find a copy of the GNU General Public License at
     16;; http://www.gnu.org/licenses/
     17
    118(module uri-match
    219        (uri-match make-routes make-uri-matcher)
     
    3653
    3754
    38 ;; Matches a given method and path in routes and returns the body of
    39 ;; the first matching route, #f otherwise. If the body is a procedure,
    40 ;; it is applied to the possibly found capture groups.
    41 (define (uri-match method path routes)
    42   (let find ([routes (alist-ref method routes)])
    43     (and routes (not (null? routes))
    44          (let ([matches (string-match (caar routes) path)])
    45            (if matches
    46                (let ([body (cadar routes)])
    47                  (if (procedure? body)
    48                      (lambda () (apply body (cdr matches)))
    49                      (lambda () body)))
    50                (find (cdr routes)))))))
     55;; Matches a given HTTP method and path (or uri-path, respectively) in
     56;; routes and returns the body of the first matching route, #f
     57;; otherwise. If the body is a procedure, it is applied to the
     58;; possibly found capture groups.
     59(define (uri-match method uri routes)
     60  (let ([path (if (uri-reference? uri) (string-join (cons "" (cdr (uri-path uri))) "/") uri)])
     61    (let find ([routes (alist-ref method routes)])
     62      (and routes (not (null? routes))
     63           (let ([matches (string-match (caar routes) path)])
     64             (if matches
     65                 (let ([body (cadar routes)])
     66                   (if (procedure? body)
     67                       (lambda () (apply body (cdr matches)))
     68                       (lambda () body)))
     69                 (find (cdr routes))))))))
    5170
    5271
  • release/4/uri-match/uri-match.setup

    r15906 r16094  
    55  'uri-match
    66  '("uri-match.so" "uri-match.import.so")
    7   `((version 0.1)
     7  `((version 0.2)
    88    (documentation "uri-match.html")))
Note: See TracChangeset for help on using the changeset viewer.