lennarb 0.4.2 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8f83fbde3862f89138c06960e7e10d92febf1b2568ec1ac7d5f414e56eb7861
4
- data.tar.gz: 2ae1191814fc395c684f7d3b52a3d16d41cdb722d61ebd1c18937ab7e2eeff00
3
+ metadata.gz: 00b277c3ca70e4b15e74cb88d343915623d6ae869d4c98b15f46cf6cd286821d
4
+ data.tar.gz: c539aa17c39cd2d861e2bfc16b8ae1b76c1d4e37304f95e918ded2235e8c7a82
5
5
  SHA512:
6
- metadata.gz: 9bf21b6aeaaafc3b1c1f795ae356883ad6c4581506527f6b92b2a10b823770e6d5a8249dc7ebacc0776e0d5a8b4d10a818dcb05ac22ec5cb48e84c7f027d9925
7
- data.tar.gz: 96a9067abdf1f6062da9cac71fd79df3a7da8d01a19d662419c9f7d9498438b57299074a5ac2ac86b44271f263e0a52c69d8f48e69c7843177191a653d68e508
6
+ metadata.gz: ab79db446aab1914aa18146f5b0f8814c68296f04fc7b300af3ac9e0079b11f002b284402335a03acba3a2d63d039c54ce20396e84b1c60b3cc9dfadbeb5f860
7
+ data.tar.gz: d16922a8773b300d364c2db69b3925555dfa3d29f91a4018c2d887360dd0cdb36c5b9c3a0821a4b6044bb14586d739ae4ce98168e6bdcb9e9a6d82a494337fe0
data/changelog.md CHANGED
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [Unreleased]
9
+
10
+ ## [0.4.3] - 2024-04-01
11
+
12
+ ### Added
13
+
14
+ ### Remove
15
+
16
+ - Remove `Lennarb::ApplicationBase` class from the project. Now, the `Lennarb` class is the main class of the project.
17
+
18
+ ### Changed
19
+
20
+ - Improve performance of the RPS (Requests per second), memory and CPU usage.
21
+ - Change the `finish` method from `Lennarb` class to call `halt(@res.finish)` method to finish the response.
22
+
23
+ ## [0.4.2] - 2024-08-02
24
+
25
+ ### Added
26
+
27
+ - Add `header` and `options` methods to `Lennarb` and `Lennarb::ApplicatiobBase`.
28
+
29
+ ### Fix
30
+
31
+ - Fix Content-Length header to be the length of the body in the response.
32
+
8
33
  ## [0.4.1] - 2024-08-02
9
34
 
10
35
  ### Change
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2023-2024, by Aristóteles Coutinho.
5
+
6
+ class Lennarb
7
+ # This module must be included in the class that will use the router.
8
+ #
9
+ module Router
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ base.instance_variable_set(:@router, Lennarb.new)
13
+ base.define_singleton_method(:router) do
14
+ base.instance_variable_get(:@router)
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ # Helper method to define a route
20
+ #
21
+ # returns [Lennarb] instance of the router
22
+ #
23
+ def route = router
24
+
25
+ # Use this in congi.ru to start the application
26
+ #
27
+ def app
28
+ router.freeze!
29
+ router
30
+ end
31
+ end
32
+ end
33
+ end
@@ -4,7 +4,7 @@
4
4
  # Copyright, 2023-2024, by Aristóteles Coutinho.
5
5
 
6
6
  class Lennarb
7
- VERSION = '0.4.2'
7
+ VERSION = '0.4.4'
8
8
 
9
9
  public_constant :VERSION
10
10
  end
data/lib/lennarb.rb CHANGED
@@ -15,6 +15,7 @@ require 'rack'
15
15
  require_relative 'lennarb/request'
16
16
  require_relative 'lennarb/response'
17
17
  require_relative 'lennarb/route_node'
18
+ require_relative 'lennarb/router'
18
19
  require_relative 'lennarb/version'
19
20
 
20
21
  class Lennarb
@@ -27,7 +28,6 @@ class Lennarb
27
28
  #
28
29
  attr_reader :root
29
30
 
30
- @routes = []
31
31
  # Initialize the application
32
32
  #
33
33
  # @yield { ... } The application
@@ -55,19 +55,48 @@ class Lennarb
55
55
  # @returns [Array] response
56
56
  #
57
57
  def call(env)
58
- http_method = env.fetch('REQUEST_METHOD').to_sym
59
- parts = SplitPath[env.fetch('PATH_INFO')]
58
+ http_method = env[Rack::REQUEST_METHOD].to_sym
59
+ parts = SplitPath[env[Rack::PATH_INFO]]
60
60
 
61
61
  block, params = @root.match_route(parts, http_method)
62
62
  return [404, { 'content-type' => 'text/plain' }, ['Not Found']] unless block
63
63
 
64
- res = Response.new
65
- req = Request.new(env, params)
66
- instance_exec(req, res, &block)
64
+ @res = Response.new
65
+ req = Request.new(env, params)
67
66
 
68
- res.finish
67
+ catch(:halt) do
68
+ instance_exec(req, @res, &block)
69
+ finish!
70
+ end
69
71
  end
70
72
 
73
+ # Finish the request
74
+ #
75
+ # @returns [Array] Response
76
+ #
77
+ def finish! = halt(@res.finish)
78
+
79
+ # Immediately stops the request and returns `response`
80
+ # as per Rack's specification.
81
+ #
82
+ # halt([200, { "Content-Type" => "text/html" }, ["hello"]])
83
+ # halt([res.status, res.headers, res.body])
84
+ # halt(res.finish)
85
+ #
86
+ # @parameter [Array] response
87
+ #
88
+ # @returns [Array] response
89
+ #
90
+ def halt(response)
91
+ throw(:halt, response)
92
+ end
93
+
94
+ # Freeze the routes
95
+ #
96
+ # @returns [void]
97
+ #
98
+ def freeze! = @root.freeze
99
+
71
100
  # Add a routes
72
101
  #
73
102
  # @parameter [String] path
@@ -75,12 +104,12 @@ class Lennarb
75
104
  #
76
105
  # @returns [void]
77
106
  #
78
- def get(path, &block) = add_route(path, :GET, block)
79
- def put(path, &block) = add_route(path, :PUT, block)
80
- def post(path, &block) = add_route(path, :POST, block)
81
- def head(path, &block) = add_route(path, :HEAD, block)
82
- def patch(path, &block) = add_route(path, :PATCH, block)
83
- def delete(path, &block) = add_route(path, :DELETE, block)
107
+ def get(path, &block) = add_route(path, :GET, block)
108
+ def put(path, &block) = add_route(path, :PUT, block)
109
+ def post(path, &block) = add_route(path, :POST, block)
110
+ def head(path, &block) = add_route(path, :HEAD, block)
111
+ def patch(path, &block) = add_route(path, :PATCH, block)
112
+ def delete(path, &block) = add_route(path, :DELETE, block)
84
113
  def options(path, &block) = add_route(path, :OPTIONS, block)
85
114
 
86
115
  private
@@ -97,59 +126,4 @@ class Lennarb
97
126
  parts = SplitPath[path]
98
127
  @root.add_route(parts, http_method, block)
99
128
  end
100
-
101
- # Base class for Lennarb applications
102
- #
103
- class ApplicationBase < Lennarb
104
- @routes = []
105
-
106
- class << self
107
- attr_reader :routes
108
-
109
- # Add a route
110
- #
111
- # @parameter [String] path
112
- # @parameter [Proc] block
113
- #
114
- # @returns [void]
115
- #
116
- %i[get post put patch delete].each do |http_method|
117
- define_method(http_method) do |path, &block|
118
- routes << { method: http_method, path:, proc: block }
119
- end
120
- end
121
- end
122
-
123
- # Inherited hook
124
- #
125
- # @parameter [Class] subclass
126
- #
127
- # @returns [void]
128
- #
129
- def self.inherited(subclass)
130
- super
131
- subclass.instance_variable_set(:@routes, routes.dup)
132
- end
133
-
134
- # Initialize the application
135
- #
136
- # @returns [ApplicationBase]
137
- #
138
- def initialize
139
- super
140
- setup_routes
141
- end
142
-
143
- private
144
-
145
- # Setup the routes
146
- #
147
- # @returns [void]
148
- #
149
- def setup_routes
150
- self.class.routes.each do |route_info|
151
- __send__(route_info[:method], route_info[:path], &route_info[:proc])
152
- end
153
- end
154
- end
155
129
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lennarb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aristóteles Coutinho
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-08 00:00:00.000000000 Z
11
+ date: 2024-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -120,14 +120,14 @@ dependencies:
120
120
  requirements:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
- version: '13.0'
123
+ version: '13.1'
124
124
  type: :development
125
125
  prerelease: false
126
126
  version_requirements: !ruby/object:Gem::Requirement
127
127
  requirements:
128
128
  - - "~>"
129
129
  - !ruby/object:Gem::Version
130
- version: '13.0'
130
+ version: '13.1'
131
131
  - !ruby/object:Gem::Dependency
132
132
  name: rack-test
133
133
  requirement: !ruby/object:Gem::Requirement
@@ -183,6 +183,7 @@ files:
183
183
  - lib/lennarb/request.rb
184
184
  - lib/lennarb/response.rb
185
185
  - lib/lennarb/route_node.rb
186
+ - lib/lennarb/router.rb
186
187
  - lib/lennarb/version.rb
187
188
  - license.md
188
189
  - readme.md
@@ -210,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
211
  - !ruby/object:Gem::Version
211
212
  version: '0'
212
213
  requirements: []
213
- rubygems_version: 3.5.5
214
+ rubygems_version: 3.5.3
214
215
  signing_key:
215
216
  specification_version: 4
216
217
  summary: A lightweight and experimental web framework for Ruby.