about summary refs log tree commit
path: root/lib/metropolis/common.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-23 12:17:24 -0800
committerEric Wong <normalperson@yhbt.net>2010-11-23 12:17:24 -0800
commitb8ee33b8e38911012fc055f02da1878e45eb721a (patch)
tree48c98c0a7390fd6693a7a4440f47a46e765f4fbe /lib/metropolis/common.rb
parentdf91c57c312bee97a16bced1035bd704e518ac38 (diff)
downloadmetropolis-b8ee33b8e38911012fc055f02da1878e45eb721a.tar.gz
split out common read/write code
It will be useful when we support other backends.
Diffstat (limited to 'lib/metropolis/common.rb')
-rw-r--r--lib/metropolis/common.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/metropolis/common.rb b/lib/metropolis/common.rb
new file mode 100644
index 0000000..b86102d
--- /dev/null
+++ b/lib/metropolis/common.rb
@@ -0,0 +1,31 @@
+# -*- encoding: binary -*-
+module Metropolis::Common
+  include Rack::Utils # unescape
+
+  def r(code)
+    body = "#{HTTP_STATUS_CODES[code]}\n"
+    [ code,
+      { 'Content-Length' => body.size.to_s, 'Content-Type' => 'text/plain' },
+      [ body ] ]
+  end
+
+  def call(env)
+    if %r{\A/(.*)\z} =~ env["PATH_INFO"]
+      key = unescape($1)
+      case env["REQUEST_METHOD"]
+      when "GET"
+        get(key)
+      when "HEAD"
+        head(key)
+      when "DELETE"
+        delete(key)
+      when "PUT"
+        put(key, env)
+      else
+        [ 405, {}, [] ]
+      end
+    else # OPTIONS
+      [ 405, {}, [] ]
+    end
+  end
+end