Class: Howl::Padrino::Core

Inherits:
Howl
  • Object
show all
Defined in:
lib/howl-router/padrino/core.rb

Constant Summary

Constants inherited from Howl

HTTP_VERBS, RESPONSE_HEADERS, VERSION

Instance Method Summary collapse

Methods inherited from Howl

#compile, #compiled?, #initialize, #recognize, #recognize_path, #reset!, #router, #routes

Constructor Details

This class inherits a constructor from Howl

Instance Method Details

#add(verb, path, options = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/howl-router/padrino/core.rb', line 6

def add(verb, path, options = {}, &block)
  route = Route.new(path, &block)
  route.path_for_generation = options[:path_for_generation] if options[:path_for_generation]
  route.verb = verb.downcase.to_sym
  route.router = router
  router.routes << route
  route
end

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/howl-router/padrino/core.rb', line 15

def call(env)
  request  = Request.new(env)
  return bad_request unless HTTP_VERBS.include?(request.request_method.downcase.to_sym)
  compile unless compiled?
  begin
    matched_routes = recognize(request)
    [200, {}, matched_routes]
  rescue => evar
    case evar
    when NotFound then not_found
    when MethodNotAllowed then method_not_allowed('Allow' => request.acceptable_methods.sort.join(", "))
    else server_error
    end
  end
end

#path(name, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/howl-router/padrino/core.rb', line 31

def path(name, *args)
  params = args.delete_at(args.last.is_a?(Hash) ? -1 : 0) || {}
  saved_args = args.dup
  router.routes.each do |route|
    next unless route.name == name
    matcher = route.matcher
    if !args.empty? and matcher.mustermann?
      matcher_names = matcher.names
      params_for_expand = Hash[matcher_names.map{|matcher_name|
        [matcher_name.to_sym, (params[matcher_name.to_sym] || args.shift)]
      }]
      params_for_expand.merge!(Hash[params.select{|k, v| !matcher_names.include?(name.to_sym) }])
      args = saved_args.dup
    else
      params_for_expand = params.dup
    end
    return matcher.mustermann? ? matcher.expand(params_for_expand) : route.path_for_generation
  end
  raise InvalidRouteException
end