Class: Pendragon::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/pendragon/router.rb

Overview

A class for the router

Examples:

Construct with a block which has no argument

router = Pendragon do
  get("/"){ "hello world" }
end

Construct with a block which has an argument

router = Pendragon.new do |config|
  config.enable_compiler = true
end

Direct Known Subclasses

Padrino::Router

Constant Summary collapse

@@mutex =

See Also:

  • Configuration#lock?
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Router

Constructs a new instance of Pendragon::Router Possible to pass the block

Examples:

with a block

app = Pendragon::Router.new do |config|
  config.enable_compiler  = true
  config.auto_rack_format = false
end

with base style

app = Pendragon::Router.new
app.get("/"){ "hello!" }
app.post("/"){ "hello post!" }


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pendragon/router.rb', line 40

def initialize(&block)
  reset!
  if block_given?
    if block.arity.zero?
      instance_eval(&block)
    else
      @configuration = Configuration.new
      block.call(configuration)
    end
  end
end

Instance Attribute Details

#currentObject

The accessors are useful to access from Pendragon::Route



22
23
24
# File 'lib/pendragon/router.rb', line 22

def current
  @current
end

#routesObject

The accessors are useful to access from Pendragon::Route



22
23
24
# File 'lib/pendragon/router.rb', line 22

def routes
  @routes
end

Instance Method Details

#add(verb, path, options = {}, &block) ⇒ Pendragon::Route

Adds a new route to router

Returns:



86
87
88
89
90
# File 'lib/pendragon/router.rb', line 86

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

#call(env) ⇒ Object

Finds the routes if request method is valid

Returns:

  • the Rack style response



54
55
56
57
58
59
60
61
# File 'lib/pendragon/router.rb', line 54

def call(env)
  request = Rack::Request.new(env)
  recognize(request).each do |route, params|
    catch(:pass){ return invoke(route, params) }
  end
rescue BadRequest, NotFound, MethodNotAllowed
  $!.call
end

#configurationPendragon::Configuration

Returns Pendragon configuration

Returns:

  • (Pendragon::Configuration)


148
149
150
# File 'lib/pendragon/router.rb', line 148

def configuration
  @configuration || Pendragon.configuration
end

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



80
# File 'lib/pendragon/router.rb', line 80

def delete(path, options = {}, &block); add :delete, path, options, &block end

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

Provides some methods intuitive than #add Basic usage is the same as #add

See Also:



78
# File 'lib/pendragon/router.rb', line 78

def get(path, options = {}, &block);    add :get,    path, options, &block end

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



82
# File 'lib/pendragon/router.rb', line 82

def head(path, options = {}, &block);   add :head,   path, options, &block end

#increment_order!Object

Increments for the integrity of priorities



113
114
115
# File 'lib/pendragon/router.rb', line 113

def increment_order!
  @current += 1
end

#invoke(route, params) ⇒ Array<Fixnum, Hash, Array>

Calls a route, and build return value of the router

Parameters:

  • route (Pendragon::Route)

    The route matched with the condition of request

  • params (Hash)

    The params will be passed with the route

Returns:

  • (Array<Fixnum, Hash, Array>)

    The return value of the route block



67
68
69
70
71
72
73
# File 'lib/pendragon/router.rb', line 67

def invoke(route, params)
  response = route.arity != 0 ? route.call(params) : route.call
  return response unless configuration.auto_rack_format?
  status = route.options[:status] || 200
  header = {'Content-Type' => 'text/html;charset=utf-8'}.merge(route.options[:header] || {})
  [status, header, Array(response)]
end

#path(name, *args) ⇒ String, Regexp

Returns an expanded path matched with the conditions as arguments

Examples:

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

Returns:

  • (String, Regexp)


140
141
142
143
144
# File 'lib/pendragon/router.rb', line 140

def path(name, *args)
  extract_with(name, *args) do |route, params, matcher|
    matcher.mustermann? ? matcher.expand(params) : route.path
  end
end

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



79
# File 'lib/pendragon/router.rb', line 79

def post(path, options = {}, &block);   add :post,   path, options, &block end

#prepare!Object

Prepares the router for route’s priority This method is executed only once in the initial load



101
102
103
104
105
# File 'lib/pendragon/router.rb', line 101

def prepare!
  @routes.sort_by!(&:order)
  @engine = (configuration.enable_compiler? ? Compiler : Recognizer).new(routes)
  @prepared = true
end

#prepared?Boolean

Returns the router is already prepared?.

Returns:

  • (Boolean)

    the router is already prepared?



108
109
110
# File 'lib/pendragon/router.rb', line 108

def prepared?
  !!@prepared
end

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



81
# File 'lib/pendragon/router.rb', line 81

def put(path, options = {}, &block);    add :put,    path, options, &block end

#recognize(request) ⇒ Array

Recognizes the route by request

Parameters:

  • request (Rack::Request)

Returns:

  • (Array)


120
121
122
123
# File 'lib/pendragon/router.rb', line 120

def recognize(request)
  prepare! unless prepared?
  synchronize { @engine.call(request) }
end

#recognize_path(path_info) ⇒ Array

Recognizes a given path

Parameters:

  • path_info (String)

Returns:

  • (Array)


128
129
130
131
# File 'lib/pendragon/router.rb', line 128

def recognize_path(path_info)
  route, params = recognize(Rack::MockRequest.env_for(path_info)).first
  [route.name, params.inject({}){|hash, (key, value)| hash[key] = value; hash }]
end

#reset!Object

Resets the router’s instance variables



93
94
95
96
97
# File 'lib/pendragon/router.rb', line 93

def reset!
  @routes = []
  @current = 0
  @prepared = nil
end