Rackup file; makes it easier to test. May convert other examples.
authorPete Elmore <pete@debu.gs>
Tue, 15 Mar 2011 23:52:41 +0000 (16:52 -0700)
committerPete Elmore <pete@debu.gs>
Tue, 15 Mar 2011 23:52:41 +0000 (16:52 -0700)
doc/examples/matching.ru [new file with mode: 0644]

diff --git a/doc/examples/matching.ru b/doc/examples/matching.ru
new file mode 100644 (file)
index 0000000..44f7e28
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/env ruby
+# An illustration of the pattern-matching capabilities of Watts.  Some URLs to
+# try if you start this one up:
+#      http://localhost:8080/strlen/foo (Which should tell you '3'.)
+#      http://localhost:8080/fib/15 (Which should give you 987.)
+#      http://localhost:8080/fib/foo (Which is a 404.  'foo' isn't a number!)
+#      http://localhost:8080/fib/f (Which should give you 0x3db.)
+#      http://localhost:8080/fib/0x15 (Which should give you 0x452f.)
+
+require 'watts'
+
+class MatchingDemo < Watts::App
+       class Strlen < Watts::Resource
+               # Takes an argument, and just returns the length of the argument.
+               get { |str| str.length.to_s + "\n" }
+       end
+
+       class Fibonacci < Watts::Resource
+               # This resource takes an argument for GET.  It is filled in by Watts
+               # according to the argument pattern passed into resource below.
+               get { |n| fib(n.to_i).to_s + "\n" }
+
+               # A naive, recursive, slow, text-book implementation of Fibonacci.
+               def fib(n)
+                       if n < 2
+                               1
+                       else
+                               fib(n - 1) + fib(n - 2)
+                       end
+               end
+       end
+
+       # As above, but with a base-16 number.
+       class HexFibonacci < Fibonacci
+               get { |n| "0x" + fib(n.to_i(16)).to_s(16) + "\n" }
+       end
+
+       resource('/') {
+               # A symbol can be used to indicate an 'argument' component of a path,
+               # which is in turn passed to the resource's method as paths.  It will
+               # match anything, making it almost equivalent to just using an empty
+               # regex (see below), except that it can serve as documentation.
+               resource(['strlen', :str], Strlen)
+
+               resource('fib') {
+                       # You can match arguments based on a regex.  The path component for
+                       # the regex is passed to the resource's method as part of the
+                       # argument list.
+                       resource([/^[0-9]+$/], Fibonacci)
+
+                       # As above, but here we use hexadecimal.  If the pattern for
+                       # Fibonacci doesn't match, then we'll end up hitting this one.
+                       resource([/^(0x)?[0-9a-f]+$/i], HexFibonacci)
+               }
+       }
+end
+
+app = MatchingDemo.new
+builder = Rack::Builder.new { run app }
+run builder