Class: Pendragon::Router

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

Direct Known Subclasses

Linear, Realism

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize { ... } ⇒ Pendragon::Router

Construcsts an instance of router class.

Examples:

construction for router class

require 'pendragon'

Pendragon.new do
  get '/', to: -> { [200, {}, ['hello']] }
end

Yields:

  • block a block is evaluated in instance context.



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

def initialize(&block)
  @compiled = false
  instance_eval(&block) if block_given?
end

Class Method Details

.on(event) {|env, method, routes| ... } ⇒ Object

Adds event listener in router class.

Examples:

require 'pendragon'

class Pendragon::SuperFast < Pendragon::Router
  register :super_fast

  on :call do |env|
    rotation(env) { |route| route.exec(env) }
  end

  on :compile do |method, routes|
    routes.each do |route|
      route.pattern = route.pattern.to_regexp
    end
  end
end

Parameters:

  • event (Symbol)

    a event name which is :call or :compile

Yield Parameters:

  • env (optional, Hash)

    a request environment variables on :call event.

  • method (String)
  • routes (Array<Pendragon::Route>)

Yield Returns:

  • (optional, Array<Integer, Hash, #each>, Rack::Response)

    response



55
56
57
# File 'lib/pendragon/router.rb', line 55

def self.on(event, &listener)
  define_method('on_%s_listener' % event, &listener)
end

.register(name) ⇒ Object

Registers new router type onto global maps.

Examples:

registring new router type.

require 'pendragon'

class Pendragon::SuperFast < Pendragon::Router
  register :super_fast
end

Pendragon[:super_fast] #=> Pendragon::SuperFast

Parameters:

  • name (Symbol)

    a router type identifier

See Also:

  • Pendragon.register


25
26
27
# File 'lib/pendragon/router.rb', line 25

def self.register(name)
  Pendragon.register(name, self)
end

Instance Method Details

#call(env) ⇒ Array<Integer, Hash, #each>, Rack::Response

Calls by given env, returns a response conformed Rack style.

Examples:

require 'pendragon'

router = Pendragon.new do
  get '/', to: -> { [200, {}, ['hello']]  }
end

env = Rack::MockRequest.env_for('/')
router.call(env) #=> [200, {}, ['hello']]

Returns:

  • (Array<Integer, Hash, #each>, Rack::Response)

    response conformed Rack style



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

def call(env)
  catch(:halt) { with_optimization { invoke(env) } }
end

#delete(path, to: nil, **options, &block) ⇒ Object

Appends a route of DELETE method

See Also:

  • Pendragon::Router.[Pendragon[Pendragon::Router[Pendragon::Router#route]


217
218
219
# File 'lib/pendragon/router.rb', line 217

def delete(path, to: nil, **options, &block)
  route Constants::Http::DELETE, path, to: to, **options, &block
end

#flat_mapArray<Pendragon::Route>

Maps all routes.

Returns:

  • (Array<Pendragon::Route>)

    flat_map



253
254
255
# File 'lib/pendragon/router.rb', line 253

def flat_map
  @flat_map ||= []
end

#get(path, to: nil, **options, &block) ⇒ Object

Appends a route of GET method

See Also:

  • Pendragon::Router.[Pendragon[Pendragon::Router[Pendragon::Router#route]


199
200
201
# File 'lib/pendragon/router.rb', line 199

def get(path, to: nil, **options, &block)
  route Constants::Http::GET, path, to: to, **options, &block
end

#head(path, to: nil, **options, &block) ⇒ Object

Appends a route of HEAD method

See Also:

  • Pendragon::Router.[Pendragon[Pendragon::Router[Pendragon::Router#route]


223
224
225
# File 'lib/pendragon/router.rb', line 223

def head(path, to: nil, **options, &block)
  route Constants::Http::HEAD, path, to: to, **options, &block
end

#mapHash{String => Array}

Maps all routes for each request methods.

Returns:

  • (Hash{String => Array})

    map



247
248
249
# File 'lib/pendragon/router.rb', line 247

def map
  @map ||= Hash.new { |hash, key| hash[key] = [] }
end

#namespace(name) { ... } ⇒ Object

Prefixes a namespace to route path inside given block.

Examples:

require 'pendragon'

Pendragon.new do
  namespace :foo do
    # This definition is dispatched to '/foo/bar'.
    get '/bar', to: -> { [200, {}, ['hello']] }
  end
end

Yields:

  • block a block is evaluated in instance context.



88
89
90
91
92
93
94
# File 'lib/pendragon/router.rb', line 88

def namespace(name, &block)
  fail ArgumentError unless block_given?
  (self.prefix ||= []) << name.to_s
  instance_eval(&block)
ensure
  prefix.pop
end

#options(path, to: nil, **options, &block) ⇒ Object

Appends a route of OPTIONS method

See Also:

  • Pendragon::Router.[Pendragon[Pendragon::Router[Pendragon::Router#route]


229
230
231
# File 'lib/pendragon/router.rb', line 229

def options(path, to: nil, **options, &block)
  route Constants::Http::OPTIONS, path, to: to, **options, &block
end

#post(path, to: nil, **options, &block) ⇒ Object

Appends a route of POST method

See Also:

  • Pendragon::Router.[Pendragon[Pendragon::Router[Pendragon::Router#route]


205
206
207
# File 'lib/pendragon/router.rb', line 205

def post(path, to: nil, **options, &block)
  route Constants::Http::POST, path, to: to, **options, &block
end

#put(path, to: nil, **options, &block) ⇒ Object

Appends a route of PUT method

See Also:

  • Pendragon::Router.[Pendragon[Pendragon::Router[Pendragon::Router#route]


211
212
213
# File 'lib/pendragon/router.rb', line 211

def put(path, to: nil, **options, &block)
  route Constants::Http::PUT, path, to: to, **options, &block
end

#route(method, path, to: nil, **options, &block) ⇒ Object

Appends a new route to router.

Parameters:

  • method (String)

    A request method, it should be upcased.

  • path (String)

    The application is dispatched to given path.

  • [Class, (Hash)

    a customizable set of options



238
239
240
241
242
243
# File 'lib/pendragon/router.rb', line 238

def route(method, path, to: nil, **options, &block)
  app = block_given? ? block : to
  fail ArgumentError, 'Rack application could not be found' unless app
  path = ?/ + prefix.join(?/) + path if prefix && !prefix.empty?
  append Route.new(method: method, pattern: path, application: app, **options)
end