about summary refs log tree commit
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
parentdf91c57c312bee97a16bced1035bd704e518ac38 (diff)
downloadmetropolis-b8ee33b8e38911012fc055f02da1878e45eb721a.tar.gz
split out common read/write code
It will be useful when we support other backends.
-rw-r--r--lib/metropolis.rb2
-rw-r--r--lib/metropolis/common.rb31
-rw-r--r--lib/metropolis/tc/hdb.rb28
3 files changed, 34 insertions, 27 deletions
diff --git a/lib/metropolis.rb b/lib/metropolis.rb
index 05dc071..96a2a6d 100644
--- a/lib/metropolis.rb
+++ b/lib/metropolis.rb
@@ -26,3 +26,5 @@ module Metropolis
     rv
   end
 end
+
+require 'metropolis/common'
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
diff --git a/lib/metropolis/tc/hdb.rb b/lib/metropolis/tc/hdb.rb
index ec387b4..4c58ca8 100644
--- a/lib/metropolis/tc/hdb.rb
+++ b/lib/metropolis/tc/hdb.rb
@@ -6,14 +6,7 @@ module Metropolis::TC::HDB
   autoload :RO, 'metropolis/tc/hdb/ro'
 
   TCHDB = TokyoCabinet::HDB # :nodoc
-  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
+  include Metropolis::Common
 
   def setup(opts)
     @headers = { 'Content-Type' => 'application/octet-stream' }
@@ -64,25 +57,6 @@ module Metropolis::TC::HDB
     end
   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
 
   def ex!(msg, hdb)
     raise "#{msg}: #{hdb.errmsg(hdb.ecode)}"