lennarb 0.2.0 → 0.4.0

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: 3a9951f0d799cadd7b4c3025eeaa9b050dd3920524b0a256530b4c53bb924a74
4
- data.tar.gz: 39e78cd19783b98bb9f88d064821207dbc506c94f6742b840bfc3e31ed6568e9
3
+ metadata.gz: 9dfa4cf1e25284df8669a9fdd9faf45806575137b0b2a2cd131e4af205426176
4
+ data.tar.gz: 879b617cb51177961a055e2dfd069437e9cae0c7e781fb50aad410c14b271dd8
5
5
  SHA512:
6
- metadata.gz: 9ec6a17703b76f22f9f24f1d356c8bf190ed351d1629deeffaf5b871d3ae5907130d90d9329cc9b51f859659c75732fdb034dc029d81c7faed324979e536b090
7
- data.tar.gz: 5efe46152e8796c8f07d19d0117da18c7868f07bf472be060de06ae140ff6cccbf69daf9d0b0883584875e600ecf0855ec52cffbef253c277325cbdbd2363d72
6
+ metadata.gz: '087d53fc3627a7c61f028826f0af5ea8fc26e6fb02a2a461c54154852bbc9b71d9d65becf7faf5451e81612eeb9129b307002861aac49b3e85ecc1dab4e6a6de'
7
+ data.tar.gz: 74d0156c558d8619e66b2daa5d5449216d064b38c511efb01cea802397efc2323b149e71cb1a3756af591c58b10510aef48dcc77ec40e20cbbae6f2a350a38a7
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.0'
8
8
 
9
9
  public_constant :VERSION
10
10
  end
data/lib/lennarb.rb CHANGED
@@ -23,7 +23,7 @@ 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
 
@@ -31,7 +31,7 @@ class Lennarb
31
31
  #
32
32
  # @yield { ... } The application
33
33
  #
34
- # @return [Lennarb]
34
+ # @returns [Lennarb]
35
35
  #
36
36
  def initialize
37
37
  @root = RouteNode.new
@@ -42,7 +42,7 @@ class Lennarb
42
42
  #
43
43
  # @parameter [String] path
44
44
  #
45
- # @return [Array] parts. Ex. ['users', ':id']
45
+ # @returns [Array] parts. Ex. ['users', ':id']
46
46
  #
47
47
  SplitPath = ->(path) { path.split('/').reject(&:empty?) }
48
48
  private_constant :SplitPath
@@ -51,16 +51,16 @@ class Lennarb
51
51
  #
52
52
  # @parameter [Hash] env
53
53
  #
54
- # @return [Array] response
54
+ # @returns [Array] response
55
55
  #
56
56
  def call(env)
57
- res = Response.new
58
57
  http_method = env.fetch('REQUEST_METHOD').to_sym
59
58
  parts = SplitPath[env.fetch('PATH_INFO')]
60
59
 
61
60
  block, params = @root.match_route(parts, http_method)
62
61
  return [404, { 'content-type' => 'text/plain' }, ['Not Found']] unless block
63
62
 
63
+ res = Response.new
64
64
  req = Request.new(env, params)
65
65
  instance_exec(req, res, &block)
66
66
 
@@ -72,15 +72,13 @@ class Lennarb
72
72
  # @parameter [String] path
73
73
  # @parameter [Proc] block
74
74
  #
75
- # @return [void]
75
+ # @returns [void]
76
76
  #
77
- def get(path, &block) = add_route(path, :GET, block)
78
- def post(path, &block) = add_route(path, :POST, block)
79
- def put(path, &block) = add_route(path, :PUT, block)
80
- def patch(path, &block) = add_route(path, :PATCH, block)
81
- def delete(path, &block) = add_route(path, :DELETE, block)
82
-
83
- private
77
+ def get(path, &block) = __add_route__(path, :GET, block)
78
+ def post(path, &block) = __add_route__(path, :POST, block)
79
+ def put(path, &block) = __add_route__(path, :PUT, block)
80
+ def patch(path, &block) = __add_route__(path, :PATCH, block)
81
+ def delete(path, &block) = __add_route__(path, :DELETE, block)
84
82
 
85
83
  # Add a route
86
84
  #
@@ -88,10 +86,54 @@ class Lennarb
88
86
  # @parameter [String] http_method
89
87
  # @parameter [Proc] block
90
88
  #
91
- # @return [void]
89
+ # @returns [void]
92
90
  #
93
- def add_route(path, http_method, block)
91
+ def __add_route__(path, http_method, block)
94
92
  parts = SplitPath[path]
95
93
  @root.add_route(parts, http_method, block)
96
94
  end
95
+
96
+ # Base module for the application. The main purpose is to include the class methods
97
+ # and call the Lennarb instance.
98
+ #
99
+ module ApplicationBase
100
+ # Include the class methods
101
+ #
102
+ # @parameter [Class] base
103
+ #
104
+ # @returns [void]
105
+ #
106
+ def self.included(base) = base.extend(ClassMethods)
107
+
108
+ # Call the Lennarb instance
109
+ #
110
+ # @parameter [Hash] env
111
+ #
112
+ # @returns [Array]
113
+ #
114
+ def call(env) = self.class.lennarb_instance.call(env)
115
+
116
+ # Class methods
117
+ #
118
+ module ClassMethods
119
+ # Get the Lennarb instance
120
+ #
121
+ # @returns [Lennarb]
122
+ #
123
+ def lennarb_instance = @lennarb_instance ||= Lennarb.new
124
+
125
+ # Add a route
126
+ #
127
+ # @parameter [String] path
128
+ # @parameter [Proc] block
129
+ #
130
+ # @returns [void]
131
+ #
132
+ def get(path, &block) = lennarb_instance.__add_route__(path, :GET, block)
133
+ def put(path, &block) = lennarb_instance.__add_route__(path, :PUT, block)
134
+ def post(path, &block) = lennarb_instance.__add_route__(path, :POST, block)
135
+ def patch(path, &block) = lennarb_instance.__add_route__(path, :PATCH, block)
136
+ def delete(path, &block) = lennarb_instance.__add_route__(path, :DELETE, block)
137
+ end
138
+ end
97
139
  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.0
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-07 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.