Method: Whisper::Handler#handle

Defined in:
lib/whisper/handler.rb

#handle(path, params, request_method, route) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/whisper/handler.rb', line 36

def handle path, params, request_method, route
  #debug "comparing #{request_method.inspect} against #{@request_method.inspect}"
  return unless (request_method == @request_method) ||   # special case:
    (request_method == :head && @request_method == :get) # proceed!

  #debug "comparing #{path.inspect} against #{@re} => #{@re.match(path) ? true : false}"
  match = @re.match(path) or return
  #debug "have captures: #{match.captures.inspect}"

  ## assemble the variables to be passed to the caller function
  vars = match.captures
  vars[vars.length - 1] ||= DEFAULT_FORMAT if @uses_format
  vars[vars.length - 2] = (vars[vars.length - 2].to_i || 0) if @uses_page
  vars << params

  ## make a key from the path and the query parameters, if any. the key is basically
  ## a normalized query string.
  key = path + "?" + params.sort_by { |k, v| [k, v] }.map { |k, v| k + "=" + v }.join("&")

  case request_method
  when :post # don't cache
    make route, vars
  else
    @map.prune!
    @map[key] ||= begin
      make(route, vars) || :invalid
    rescue SystemCallError => e
      :invalid
    end
  end
end