add generic Rack read/write test
[metropolis.git] / lib / metropolis / common.rb
blob3bfbf776df0dfeaae73eb070462171d10f978dd6
1 # -*- encoding: binary -*-
2 module Metropolis::Common
3   include Rack::Utils # unescape
5   def r(code, body = nil)
6     body ||= "#{HTTP_STATUS_CODES[code]}\n"
7     [ code,
8       { 'Content-Length' => body.size.to_s, 'Content-Type' => 'text/plain' },
9       [ body ] ]
10   end
12   def call(env)
13     if %r{\A/(.*)\z} =~ env["PATH_INFO"]
14       key = unescape($1)
15       case env["REQUEST_METHOD"]
16       when "GET"
17         get(key)
18       when "HEAD"
19         head(key)
20       when "DELETE"
21         delete(key)
22       when "PUT"
23         put(key, env)
24       else
25         [ 405, {}, [] ]
26       end
27     else # OPTIONS
28       [ 405, {}, [] ]
29     end
30   end
31 end