lennarb 0.4.1 → 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: 0e395a91c3650883770feaf41c00a2be532f9ac0b3919a6ed26fcc4758ae3319
4
- data.tar.gz: 25f09b0d53fb906eee6cd73197b970a01213a64ee4e8e0052e8bc73dca506d0b
3
+ metadata.gz: 00b277c3ca70e4b15e74cb88d343915623d6ae869d4c98b15f46cf6cd286821d
4
+ data.tar.gz: c539aa17c39cd2d861e2bfc16b8ae1b76c1d4e37304f95e918ded2235e8c7a82
5
5
  SHA512:
6
- metadata.gz: 034171202b487ef2b9de9fd21995df2918f6e4da6427088c665644646f34e8d3d4e4238b8708de2fb89a3303fd9d3d3f334c8402ee66ba70fdb889f8be7eca00
7
- data.tar.gz: 66308251d5eaf6361f8e4d464858a068dfeaa49051d2cfb1157e2ca52a21338639ba7af8ceb92bba15ca8bbed76124a493c30d58563b1c0403fc403c0946ad87
6
+ metadata.gz: ab79db446aab1914aa18146f5b0f8814c68296f04fc7b300af3ac9e0079b11f002b284402335a03acba3a2d63d039c54ce20396e84b1c60b3cc9dfadbeb5f860
7
+ data.tar.gz: d16922a8773b300d364c2db69b3925555dfa3d29f91a4018c2d887360dd0cdb36c5b9c3a0821a4b6044bb14586d739ae4ce98168e6bdcb9e9a6d82a494337fe0
data/changelog.md CHANGED
@@ -5,6 +5,56 @@ 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
+
33
+ ## [0.4.1] - 2024-08-02
34
+
35
+ ### Change
36
+
37
+ - Change behavior of `Lennarb::ApplicationBase` class to be the base class of the `Lennarb` class. Now, the `Lennarb` class is a subclass of `Lennarb::ApplicationBase` class.
38
+
39
+ That permits to create a new application with the `Lennarb::ApplicationBase` class and use http methods to create the routes. Ex.
40
+
41
+ ```rb
42
+ # app.rb
43
+
44
+ require 'lennarb'
45
+
46
+ class MyApp < Lennarb::ApplicationBase
47
+ get '/hello' do |req, res|
48
+ res.html('Hello World')
49
+ end
50
+ end
51
+ ```
52
+
53
+ ### Removed
54
+
55
+ - Remove `Lennarb::Application` module from the project. Now, the `Lennarb` class is the main class of the project.
56
+
57
+
8
58
  ## [0.4.0] - 2024-07-02
9
59
 
10
60
  ### Added
@@ -80,7 +80,7 @@ class Lennarb
80
80
  def write(str)
81
81
  str = str.to_s
82
82
  @length += str.bytesize
83
- @headers[CONTENT_LENGTH] ||= @length.to_s
83
+ @headers[CONTENT_LENGTH] = @length.to_s
84
84
  @body << str
85
85
  end
86
86
 
@@ -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.1'
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,11 +104,13 @@ class Lennarb
75
104
  #
76
105
  # @returns [void]
77
106
  #
78
- def get(path, &block) = add_route(path, :GET, block)
79
- def post(path, &block) = add_route(path, :POST, block)
80
- def put(path, &block) = add_route(path, :PUT, block)
81
- def patch(path, &block) = add_route(path, :PATCH, block)
82
- 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)
113
+ def options(path, &block) = add_route(path, :OPTIONS, block)
83
114
 
84
115
  private
85
116
 
@@ -95,59 +126,4 @@ class Lennarb
95
126
  parts = SplitPath[path]
96
127
  @root.add_route(parts, http_method, block)
97
128
  end
98
-
99
- # Base class for Lennarb applications
100
- #
101
- class ApplicationBase < Lennarb
102
- @routes = []
103
-
104
- class << self
105
- attr_reader :routes
106
-
107
- # Add a route
108
- #
109
- # @parameter [String] path
110
- # @parameter [Proc] block
111
- #
112
- # @returns [void]
113
- #
114
- %i[get post put patch delete].each do |http_method|
115
- define_method(http_method) do |path, &block|
116
- routes << { method: http_method, path:, proc: block }
117
- end
118
- end
119
- end
120
-
121
- # Inherited hook
122
- #
123
- # @parameter [Class] subclass
124
- #
125
- # @returns [void]
126
- #
127
- def self.inherited(subclass)
128
- super
129
- subclass.instance_variable_set(:@routes, routes.dup)
130
- end
131
-
132
- # Initialize the application
133
- #
134
- # @returns [ApplicationBase]
135
- #
136
- def initialize
137
- super
138
- setup_routes
139
- end
140
-
141
- private
142
-
143
- # Setup the routes
144
- #
145
- # @returns [void]
146
- #
147
- def setup_routes
148
- self.class.routes.each do |route_info|
149
- __send__(route_info[:method], route_info[:path], &route_info[:proc])
150
- end
151
- end
152
- end
153
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.1
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.