lennarb 0.2.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a9951f0d799cadd7b4c3025eeaa9b050dd3920524b0a256530b4c53bb924a74
4
- data.tar.gz: 39e78cd19783b98bb9f88d064821207dbc506c94f6742b840bfc3e31ed6568e9
3
+ metadata.gz: 0e395a91c3650883770feaf41c00a2be532f9ac0b3919a6ed26fcc4758ae3319
4
+ data.tar.gz: 25f09b0d53fb906eee6cd73197b970a01213a64ee4e8e0052e8bc73dca506d0b
5
5
  SHA512:
6
- metadata.gz: 9ec6a17703b76f22f9f24f1d356c8bf190ed351d1629deeffaf5b871d3ae5907130d90d9329cc9b51f859659c75732fdb034dc029d81c7faed324979e536b090
7
- data.tar.gz: 5efe46152e8796c8f07d19d0117da18c7868f07bf472be060de06ae140ff6cccbf69daf9d0b0883584875e600ecf0855ec52cffbef253c277325cbdbd2363d72
6
+ metadata.gz: 034171202b487ef2b9de9fd21995df2918f6e4da6427088c665644646f34e8d3d4e4238b8708de2fb89a3303fd9d3d3f334c8402ee66ba70fdb889f8be7eca00
7
+ data.tar.gz: 66308251d5eaf6361f8e4d464858a068dfeaa49051d2cfb1157e2ca52a21338639ba7af8ceb92bba15ca8bbed76124a493c30d58563b1c0403fc403c0946ad87
data/changelog.md CHANGED
@@ -5,6 +5,33 @@ 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
+ ## [0.4.0] - 2024-07-02
9
+
10
+ ### Added
11
+
12
+ - Add `Lennarb::ApplicationBase` class to be the base class of the `Lennarb` class. Now, the `Lennarb` class is a subclass of `Lennarb::ApplicationBase` class.
13
+
14
+ That permits to create a new application with the `Lennarb::ApplicationBase` class and use http methods to create the routes. Ex.
15
+
16
+ ```rb
17
+ # app.rb
18
+
19
+ require 'lennarb'
20
+
21
+ class MyApp
22
+ include Lennarb::ApplicationBase
23
+
24
+ get '/hello' do |req, res|
25
+ res.html('Hello World')
26
+ end
27
+ end
28
+ ```
29
+
30
+ ### Change
31
+
32
+ - Change the test/test_lenna.rb to test/test_lennarb.rb
33
+ - Change `add_route` method from `Lennarb` class to `__add_route` and remove from private section.
34
+
8
35
  ## [0.2.0] - 2024-08-01
9
36
 
10
37
  ### Removed
@@ -10,7 +10,7 @@ class Lennarb
10
10
  # @parameter [Hash] env
11
11
  # @parameter [Hash] route_params
12
12
  #
13
- # @return [Request]
13
+ # @returns [Request]
14
14
  #
15
15
  def initialize(env, route_params = {})
16
16
  super(env)
@@ -19,7 +19,7 @@ class Lennarb
19
19
 
20
20
  # Get the request body
21
21
  #
22
- # @return [String]
22
+ # @returns [String]
23
23
  def params
24
24
  @params ||= super.merge(@route_params)
25
25
  end
@@ -28,7 +28,7 @@ class Lennarb
28
28
 
29
29
  # Get the query string
30
30
  #
31
- # @return [String]
31
+ # @returns [String]
32
32
  #
33
33
  def query_params
34
34
  @query_params ||= Rack::Utils.parse_nested_query(query_string)
@@ -6,22 +6,22 @@
6
6
  class Lennarb
7
7
  class Response
8
8
  # @!attribute [rw] status
9
- # @return [Integer]
9
+ # @returns [Integer]
10
10
  #
11
11
  attr_accessor :status
12
12
 
13
13
  # @!attribute [r] body
14
- # @return [Array]
14
+ # @returns [Array]
15
15
  #
16
16
  attr_reader :body
17
17
 
18
18
  # @!attribute [r] headers
19
- # @return [Hash]
19
+ # @returns [Hash]
20
20
  #
21
21
  attr_reader :headers
22
22
 
23
23
  # @!attribute [r] length
24
- # @return [Integer]
24
+ # @returns [Integer]
25
25
  #
26
26
  attr_reader :length
27
27
 
@@ -41,7 +41,7 @@ class Lennarb
41
41
 
42
42
  # Initialize the response object
43
43
  #
44
- # @return [Response]
44
+ # @returns [Response]
45
45
  #
46
46
  def initialize
47
47
  @status = 404
@@ -54,7 +54,7 @@ class Lennarb
54
54
  #
55
55
  # @parameter [String] key
56
56
  #
57
- # @return [String] value
57
+ # @returns [String] value
58
58
  #
59
59
  def [](key)
60
60
  @headers[key]
@@ -65,7 +65,7 @@ class Lennarb
65
65
  # @parameter [String] key
66
66
  # @parameter [String] value
67
67
  #
68
- # @return [String] value
68
+ # @returns [String] value
69
69
  #
70
70
  def []=(key, value)
71
71
  @headers[key] = value
@@ -75,7 +75,7 @@ class Lennarb
75
75
  #
76
76
  # @parameter [String] str
77
77
  #
78
- # @return [String] str
78
+ # @returns [String] str
79
79
  #
80
80
  def write(str)
81
81
  str = str.to_s
@@ -88,7 +88,7 @@ class Lennarb
88
88
  #
89
89
  # @parameter [String] str
90
90
  #
91
- # @return [String] str
91
+ # @returns [String] str
92
92
  #
93
93
  def text(str)
94
94
  @headers[CONTENT_TYPE] = ContentType[:TEXT]
@@ -99,7 +99,7 @@ class Lennarb
99
99
  #
100
100
  # @parameter [String] str
101
101
  #
102
- # @return [String] str
102
+ # @returns [String] str
103
103
  #
104
104
  def html(str)
105
105
  @headers[CONTENT_TYPE] = ContentType[:HTML]
@@ -110,7 +110,7 @@ class Lennarb
110
110
  #
111
111
  # @parameter [String] str
112
112
  #
113
- # @return [String] str
113
+ # @returns [String] str
114
114
  #
115
115
  def json(str)
116
116
  @headers[CONTENT_TYPE] = ContentType[:JSON]
@@ -129,7 +129,7 @@ class Lennarb
129
129
 
130
130
  # Finish the response
131
131
  #
132
- # @return [Array] response
132
+ # @returns [Array] response
133
133
  #
134
134
  def finish
135
135
  [@status, @headers, @body]
@@ -12,7 +12,7 @@ class Lennarb
12
12
  # @return [RouteNode]
13
13
  #
14
14
  def initialize
15
- @children = {}
15
+ @children = {}
16
16
  @blocks = {}
17
17
  @param_key = nil
18
18
  end
@@ -4,7 +4,7 @@
4
4
  # Copyright, 2023-2024, by Aristóteles Coutinho.
5
5
 
6
6
  class Lennarb
7
- VERSION = '0.2.0'
7
+ VERSION = '0.4.1'
8
8
 
9
9
  public_constant :VERSION
10
10
  end
data/lib/lennarb.rb CHANGED
@@ -23,15 +23,16 @@ class Lennarb
23
23
  class LennarbError < StandardError; end
24
24
 
25
25
  # @attribute [r] root
26
- # @return [RouteNode]
26
+ # @returns [RouteNode]
27
27
  #
28
28
  attr_reader :root
29
29
 
30
+ @routes = []
30
31
  # Initialize the application
31
32
  #
32
33
  # @yield { ... } The application
33
34
  #
34
- # @return [Lennarb]
35
+ # @returns [Lennarb]
35
36
  #
36
37
  def initialize
37
38
  @root = RouteNode.new
@@ -42,7 +43,7 @@ class Lennarb
42
43
  #
43
44
  # @parameter [String] path
44
45
  #
45
- # @return [Array] parts. Ex. ['users', ':id']
46
+ # @returns [Array] parts. Ex. ['users', ':id']
46
47
  #
47
48
  SplitPath = ->(path) { path.split('/').reject(&:empty?) }
48
49
  private_constant :SplitPath
@@ -51,16 +52,16 @@ class Lennarb
51
52
  #
52
53
  # @parameter [Hash] env
53
54
  #
54
- # @return [Array] response
55
+ # @returns [Array] response
55
56
  #
56
57
  def call(env)
57
- res = Response.new
58
58
  http_method = env.fetch('REQUEST_METHOD').to_sym
59
59
  parts = SplitPath[env.fetch('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
64
65
  req = Request.new(env, params)
65
66
  instance_exec(req, res, &block)
66
67
 
@@ -72,7 +73,7 @@ class Lennarb
72
73
  # @parameter [String] path
73
74
  # @parameter [Proc] block
74
75
  #
75
- # @return [void]
76
+ # @returns [void]
76
77
  #
77
78
  def get(path, &block) = add_route(path, :GET, block)
78
79
  def post(path, &block) = add_route(path, :POST, block)
@@ -88,10 +89,65 @@ class Lennarb
88
89
  # @parameter [String] http_method
89
90
  # @parameter [Proc] block
90
91
  #
91
- # @return [void]
92
+ # @returns [void]
92
93
  #
93
94
  def add_route(path, http_method, block)
94
95
  parts = SplitPath[path]
95
96
  @root.add_route(parts, http_method, block)
96
97
  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
97
153
  end
data/readme.md CHANGED
@@ -19,6 +19,10 @@ end
19
19
 
20
20
  ### 1. Requests per Second (RPS)
21
21
 
22
+ ![RPS](https://raw.githubusercontent.com/aristotelesbr/lennarb/main/benchmark/rps.png)
23
+
24
+ See all [graphs](https://github.com/aristotelesbr/lennarb/blob/main/benchmark)
25
+
22
26
  | Position | Application | 10 RPS | 100 RPS | 1.000 RPS | 10.000 RPS |
23
27
  | -------- | ----------- | ---------- | ---------- | --------- | ---------- |
24
28
  | 1 | Lenna | 126.252,36 | 108.086,55 | 87.111,91 | 68.460,64 |
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.2.0
4
+ version: 0.4.1
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-01-10 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -210,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
210
  - !ruby/object:Gem::Version
211
211
  version: '0'
212
212
  requirements: []
213
- rubygems_version: 3.5.3
213
+ rubygems_version: 3.5.5
214
214
  signing_key:
215
215
  specification_version: 4
216
216
  summary: A lightweight and experimental web framework for Ruby.