Class: Howl

Inherits:
Object
  • Object
show all
Defined in:
lib/howl-router.rb,
lib/howl-router/route.rb,
lib/howl-router/router.rb,
lib/howl-router/matcher.rb,
lib/howl-router/padrino.rb,
lib/howl-router/request.rb,
lib/howl-router/version.rb,
lib/howl-router/padrino/core.rb,
lib/howl-router/padrino/route.rb,
lib/howl-router/padrino/router.rb,
lib/howl-router/padrino/matcher.rb,
lib/howl-router/padrino/ext/class_methods.rb,
lib/howl-router/padrino/ext/instance_methods.rb

Direct Known Subclasses

Padrino::Core

Defined Under Namespace

Modules: Padrino Classes: InvalidRouteException, Matcher, Request, Route, Router

Constant Summary collapse

HTTP_VERBS =
[:get, :post, :delete, :put, :head]
RESPONSE_HEADERS =
{
  :bad_request        => 400,
  :not_found          => 404,
  :method_not_allowed => 405,
  :server_error       => 500
}
VERSION =
'0.3.0'

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Howl

Returns a new instance of Howl.



19
20
21
# File 'lib/howl-router.rb', line 19

def initialize(&block)
  instance_eval(&block) if block_given?
end

Instance Method Details

#add(verb, path, options = {}) { ... } ⇒ Howl::Route

Generate a route, and add to routes.

Examples:

howl = Howl.new
howl.add(:get, "/") #=> Howl::Route

Parameters:

  • verb (String, Symbol)

    The verb decide a acceptable request method.

  • path (String, Regexp)

    The path associate to route.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :path_for_generation (String)

    Accept path_for_generation.

Yields:

  • The block associate to route.

Returns:

  • (Howl::Route)

    Return a generated Howl::Route instance.



36
37
38
39
40
41
42
# File 'lib/howl-router.rb', line 36

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

#call(env) ⇒ Object

call method for Rack Application.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/howl-router.rb', line 45

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)
    route, params = matched_routes.first
    result = route.arity != 0 ? route.call(params) : route.call
    [200, {'Content-Type' => 'text/html;charset=utf-8;'}, [result]]
  rescue => evar
    case evar
    when NotFound then not_found
    when MethodNotAllowed then method_not_allowed
    else server_error
    end
  end
end

#compileArray

Compile routes.

Returns:

  • (Array)

    Return a compiled routes.



75
76
77
78
# File 'lib/howl-router.rb', line 75

def compile
  @compiled = true
  router.compile
end

#compiled?Boolean

Determines whether the compiled.

Returns:

  • (Boolean)


67
68
69
# File 'lib/howl-router.rb', line 67

def compiled?
  @compiled
end

#path(name, *args) ⇒ String

Find a route, and return a generated path of route.

Examples:


howl = Howl.new
index = howl.add(:get, "/:id"){}
index.name = :index
howl.path(:index, :id => 1) #=> "/1"
howl.path(:index, :id => 2, :foo => "bar") #=> "/1?foo=bar"

Parameters:

  • name (Symbol)

    The name is route name.

  • args (Array)

    The args are route params and queries.

Returns:

  • (String)

    return a generated path.

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/howl-router.rb', line 138

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
  end
  raise InvalidRouteException
end

#recognize(request) ⇒ Array

Recognize a request, and return a matched routes.

Parameters:

  • request (Rack::Request)

    The request is a Rack::Request or instance that inherited it.

Returns:

  • (Array)

    Return a routes that match the path_info.



86
87
88
# File 'lib/howl-router.rb', line 86

def recognize(request)
  router.recognize(request)
end

#recognize_path(path_info) ⇒ Array

Recognize a path_info, and return a matched first route’s name and params.

Parameters:

  • path_info (String)

Returns:

  • (Array)

    Return an Array that likes [name, params].



95
96
97
98
99
# File 'lib/howl-router.rb', line 95

def recognize_path(path_info)
  response      = router.recognize(Rack::MockRequest.env_for(path_info))
  route, params = response.first
  [route.name, params]
end

#reset!Object

Reset a router.



102
103
104
105
# File 'lib/howl-router.rb', line 102

def reset!
  @compiled = nil
  router.reset!
end

#routerHowl::Router

Return a Router instance.

Returns:



111
112
113
# File 'lib/howl-router.rb', line 111

def router
  @router ||= Router.new
end

#routesArray

Return a added routes.

Returns:

  • (Array)


119
120
121
# File 'lib/howl-router.rb', line 119

def routes
  router.routes
end