From b8ee33b8e38911012fc055f02da1878e45eb721a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 23 Nov 2010 12:17:24 -0800 Subject: split out common read/write code It will be useful when we support other backends. --- lib/metropolis.rb | 2 ++ lib/metropolis/common.rb | 31 +++++++++++++++++++++++++++++++ lib/metropolis/tc/hdb.rb | 28 +--------------------------- 3 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 lib/metropolis/common.rb 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)}" -- cgit v1.2.3-24-ge0c7