Class: Ape::Handler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/ape/handler.rb

Overview

Implements the APE application handler for processing and responding to requests. See process for more detail.

Instance Method Summary collapse

Instance Method Details

#process(request, response) ⇒ Object

Called by Mongrel with Mongrel::HttpRequest and Mongrel::HttpResponse objects. Creates an Ape instance for the request and responds with its report.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ape/handler.rb', line 14

def process(request, response)
  cgi = Mongrel::CGIWrapper.new(request, response)

  uri  = cgi['uri'].strip
  user = cgi['username'].strip
  pass = cgi['password'].strip

  if uri.empty?
    response.start(200, true) do |header, body|
      header['Content-Type'] = 'text/plain'
      body << 'URI argument is required'
    end
    return
  end

  format = request.params['HTTP_ACCEPT'] == 'text/plain' ? 'text' : 'html'
  ape = Ape.new({ :crumbs => true, :output => format, :server => true })
  (user && pass) ? ape.check(uri, user, pass) : ape.check(uri)

  response.start(200, true) do |header, body|
    header['Content-Type'] = 'text/html'
    ape.report(body)
  end
end