Class: Lennarb

Inherits:
Object
  • Object
show all
Defined in:
lib/lennarb/router.rb,
lib/lennarb.rb,
lib/lennarb/request.rb,
lib/lennarb/version.rb,
lib/lennarb/response.rb,
lib/lennarb/route_node.rb

Overview

Released under the MIT License. Copyright, 2023-2024, by Aristóteles Coutinho.

Defined Under Namespace

Modules: Router Classes: LennarbError, Request, Response, RouteNode

Constant Summary collapse

VERSION =
'0.4.4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|...| ... } ⇒ Lennarb

Initialize the application

Yields:

  • (...)

    The application



37
38
39
40
# File 'lib/lennarb.rb', line 37

def initialize
	@root = RouteNode.new
	yield self if block_given?
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



29
30
31
# File 'lib/lennarb.rb', line 29

def root
  @root
end

Instance Method Details

#call(env) ⇒ Object

Call the application



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lennarb.rb', line 57

def call(env)
	http_method = env[Rack::REQUEST_METHOD].to_sym
	parts       = SplitPath[env[Rack::PATH_INFO]]

	block, params = @root.match_route(parts, http_method)
	return [404, { 'content-type' => 'text/plain' }, ['Not Found']] unless block

	@res = Response.new
	req  = Request.new(env, params)

	catch(:halt) do
		instance_exec(req, @res, &block)
		finish!
	end
end

#delete(path, &block) ⇒ Object



112
# File 'lib/lennarb.rb', line 112

def delete(path, &block)  = add_route(path, :DELETE, block)

#finish!Object

Finish the request



77
# File 'lib/lennarb.rb', line 77

def finish! = halt(@res.finish)

#freeze!Object

Freeze the routes



98
# File 'lib/lennarb.rb', line 98

def freeze! = @root.freeze

#get(path, &block) ⇒ Object

Add a routes



107
# File 'lib/lennarb.rb', line 107

def get(path, &block)     = add_route(path, :GET, block)

#halt(response) ⇒ Object

Immediately stops the request and returns ‘response` as per Rack’s specification.

halt([200, { "Content-Type" => "text/html" }, ["hello"]])
halt([res.status, res.headers, res.body])
halt(res.finish)


90
91
92
# File 'lib/lennarb.rb', line 90

def halt(response)
	throw(:halt, response)
end

#head(path, &block) ⇒ Object



110
# File 'lib/lennarb.rb', line 110

def head(path, &block)    = add_route(path, :HEAD, block)

#options(path, &block) ⇒ Object



113
# File 'lib/lennarb.rb', line 113

def options(path, &block) = add_route(path, :OPTIONS, block)

#patch(path, &block) ⇒ Object



111
# File 'lib/lennarb.rb', line 111

def patch(path, &block)   = add_route(path, :PATCH, block)

#post(path, &block) ⇒ Object



109
# File 'lib/lennarb.rb', line 109

def post(path, &block)    = add_route(path, :POST, block)

#put(path, &block) ⇒ Object



108
# File 'lib/lennarb.rb', line 108

def put(path, &block)     = add_route(path, :PUT, block)