lennarb 0.1.3 → 0.1.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: 4f3ac7cfbc2516f8bae7fd4e5d9fdbab7b02fc8c8545423bc434f7462e7e11b2
4
- data.tar.gz: 00ada2a17115680425bc16d478f22419e769944b2919d5d2ab5ad266884aba94
3
+ metadata.gz: ef231d5c56c92f02c7b978419c3d51a54ec8fff36e3362a8f207ccd9c4a6d7f6
4
+ data.tar.gz: ab5d46ad9197d9016dae19f7c224e56f41157925e3fc6927de3aba8d0699304b
5
5
  SHA512:
6
- metadata.gz: bbdef1cf8b5415106566aaa73dfe94ea99e56f5fe4bb808f57742bcf0d2e575952b1b4cb6cdbd0edcce102fbb8d6b9ec386ec7f60aa4d29e0edcce699f3950e7
7
- data.tar.gz: 75f1097c8a992d9256f8faf3d5a967d9e7ab1926703e7228b87a75bc95d119ba1d52281726d8d6e8bd5e62ebf0bf693d5c9dab2de3cc4cb64f68e0a30024daa0
6
+ metadata.gz: d8e84bf114e914d51cdec74713def249cca76b76e839ed77d76d94a1bd40dd18bce4f13fec36d324d69cf468047044ec159d5442977b2178010414d2a8eb0510
7
+ data.tar.gz: acc76543c293b34bc8618894d69fff79b996bd2af6603e1c7c5c45dd4ca686d05cd60591483cdae91d2e3f5fb08de3174004a4c58f8fc73c0546c5598eaa0488
data/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.1.4] - 2023-25-11
8
+
9
+ ### Fixed
10
+
11
+ - Internal docmentation methods
12
+ - Fix `post_params` from Resquest router class
13
+
14
+ ### Added
15
+
16
+ - Add basic documentation for usage. See [README.md](README.md) for more details.
17
+
18
+ ## [0.1.3] - 2023-24-11
19
+
7
20
  ## [0.1.2] - 2023-23-11
8
21
 
9
22
  ### Added
data/README.md CHANGED
@@ -1,31 +1,229 @@
1
1
  # Lennarb
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ Lennarb is a experimental lightweight, fast, and modular web framework for Ruby based on Rack.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lennarb`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ image
6
+
7
+ ## Table of Contents
8
+
9
+ - [About](#about)
10
+ - [Installation](#installation)
11
+ - [Usage](#usage)
12
+ - [Routing](#routing)
13
+ - [Parameters](#parameters)
14
+ - [Namespaces](#namespaces)
15
+ - [Middlewares](#middlewares)
16
+ - [Render HTML templates](#render-html-templates)
17
+ - [Render JSON](#render-json)
18
+ - [TODO](#todo)
19
+ - [Development](#development)
20
+ - [Contributing](#contributing)
21
+
22
+ ## About
23
+
24
+ Lennarb is designed to be simple and easy to use, while still providing the power and flexibility of a full-featured web framework. Also, that's how I affectionately call my wife.
6
25
 
7
26
  ## Installation
8
27
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
28
+ Add this line to your application's Gemfile:
10
29
 
11
- Install the gem and add to the application's Gemfile by executing:
30
+ ```rb
31
+ gem 'lennarb'
32
+ ```
12
33
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
34
+ And then execute:
14
35
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
36
+ ```bash
37
+ $ bundle install
38
+ ```
16
39
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
40
+ Or install it yourself as:
41
+
42
+ ```bash
43
+ $ gem install lennarb
44
+ ```
18
45
 
19
46
  ## Usage
20
47
 
21
- TODO: Write usage instructions here
48
+ After installing, you can begin using Lennarb to build your modular web applications with Ruby. Here is an example of how to get started:
49
+
50
+ ```rb
51
+ app = Lenna::Base.new
52
+
53
+ app.get('/hello/:name') do |req, res|
54
+ name = req.params[:name]
55
+
56
+ res.html("Hello #{name}!")
57
+ end
58
+
59
+ app.listen(8000)
60
+ ```
61
+
62
+ This example creates a new Lennarb application, defines a route for the `/hello/:name` path, and then starts the application on port 8000. When a request is made to the `/hello/:name` path, the application will respond with `Hello <name>!`, where `<name>` is the value of the `:name` parameter in the request path.
63
+
64
+ ### Routing
65
+
66
+ Lennarb uses a simple routing system that allows you to define routes for your application. Routes are defined using the `get`, `post`, `put`, `patch`, `delete`. These methods take three arguments: the path to match, an Array of the middlewares that can be apply on the current route and a block to execute when a request is made to the path. The block is passed two arguments: the [`request`](https://github.com/aristotelesbr/lennarb/blob/main/lib/lenna/router/request.rb) object and the [`response`](https://github.com/aristotelesbr/lennarb/blob/main/lib/lenna/router/response.rb) object. The request object contains information about the request, such as the request method, headers, and body. The response object contains methods for setting the response status code, headers, and body. Ex.
67
+
68
+ ```rb
69
+ app.get('/hello') do |_req, res|
70
+ res.html('Hello World!')
71
+ end
72
+ ```
73
+
74
+ The example above defines a route for the `/hello` path. When a request is made to the `/hello` path, the application will respond with `Hello World!`.
75
+
76
+ #### Parameters
77
+
78
+ Lennarb allows you to define parameters in your routes. Parameters are defined using the `:` character, followed by the name of the parameter. Parameters are passed to the route block as a hash in the request object's `params` property.
79
+
80
+ ```rb
81
+ app.get('/hello/:name') do |req, res|
82
+ name = req.params[:name]
83
+
84
+ res.html("Hello #{name}!")
85
+ end
86
+ ```
87
+
88
+ The example above defines a route for the `/hello/:name` path. When a request is made to the `/hello/:name` path, the application will respond with `Hello <name>!`, where `<name>` is the value of the `:name` parameter in the request path.
89
+
90
+ #### namespaces
91
+
92
+ Lennarb allows you to define namespaces in your routes. Namespaces are defined using the `namespace` method on the application object. Namespaces are passed to the route block as a hash in the request object's `params` property.
93
+
94
+ ```rb
95
+ app.namespace('/api') do |router|
96
+ roter.get('/hello') do |_req, res|
97
+ res.html('Hello World!')
98
+ end
99
+ end
100
+ ```
101
+
102
+ The example above defines a namespace for the `/api` path. When a request is made to the `/api/hello` path, the application will respond with `Hello World!`.
103
+
104
+ #### Middlewares
105
+
106
+ The Lennarb application object has a `use` method that allows you to add middleware to your application. Middleware is defined using the `use` method on the application object. Ex.
107
+
108
+ ```rb
109
+ app.get('/hello') do |_req, res|
110
+ res.html('Hello World!')
111
+ end
112
+
113
+ app.use(Lenna::Middleware::Logger)
114
+ ```
115
+
116
+ You can also define middleware for specific route.
117
+
118
+ ```rb
119
+ app.get('/hello', TimeMiddleware) do |_req, res|
120
+ res.html('Hello World!')
121
+ end
122
+ ```
123
+
124
+ You can create your own middleware by creating a class that implements the `call` method. This methods receive three
125
+
126
+ ```rb
127
+ class TimeMiddleware
128
+ def call(req, res, next_middleware)
129
+ puts Time.now
130
+
131
+ req.headers['X-Time'] = Time.now
132
+
133
+ next_middleware.call
134
+ end
135
+ end
136
+ ```
137
+
138
+ Or using a lambda functions.
139
+
140
+ ```rb
141
+ TimeMiddleware = ->(req, res, next_middleware) do
142
+ puts Time.now
143
+
144
+ req.headers['X-Time'] = Time.now
145
+
146
+ next_middleware.call
147
+ end
148
+ ```
149
+
150
+ So you can use it like this:
151
+
152
+ ```rb
153
+ app.get('/hello', TimeMiddleware) do |_req, res|
154
+ res.html('Hello World!')
155
+ end
156
+ ```
157
+
158
+ And you can use multiple middlewares on the same route.
159
+
160
+ ```rb
161
+ app.get('/hello', [TimeMiddleware, LoggerMiddleware]) do |_req, res|
162
+ res.html('Hello World!')
163
+ end
164
+ ```
165
+
166
+ ### Render HTML templates
167
+
168
+ Lennarb allows you to render HTML templates using the `render` method on the response object. The `render` method takes two arguments: the name of the template file, and a hash of variables to pass to the template.
169
+
170
+ ```rb
171
+ app.get('/hello') do |_req, res|
172
+ res.render('hello', locals: { name: 'World' })
173
+ end
174
+ ```
175
+
176
+ The example above renders the `hello.html.erb` template, passing the `name` variable to the template.
177
+ By default, Lennarb looks for templates in the `views` directory in root path. You can change this specifying the path for `views` in render method. Ex.
178
+
179
+ ```rb
180
+ app.get('/hello') do |_req, res|
181
+ res.render('hello', path: 'app/web/templates', locals: { name: 'World' })
182
+ end
183
+ ```
184
+
185
+ ### Render JSON
186
+
187
+ Lennarb allows you to render JSON using the `json` method on the response object. The `json` method takes one argument: the object to render as JSON.
188
+
189
+ ```rb
190
+ app.get('/hello') do |_req, res|
191
+ res.json(data: { message: 'Hello World!' })
192
+ end
193
+ ```
194
+
195
+ The example above renders the `{ message: 'Hello World!' }` object as JSON.
196
+
197
+
198
+
199
+ ### TODO
200
+
201
+ - [ ] Add support for mime types
202
+ - [ ] Add support for sessions
203
+ - [ ] Add support for websockets
204
+ - [ ] Add support for streaming
205
+ - [ ] Add support for CORS
206
+ - [ ] Add support for CSRF
207
+ - [ ] Add support for caching
208
+ - [ ] Add support for gzip compression
209
+ - [ ] Add support for SSL
210
+ - [ ] Add support for HTTP/2
211
+ - [ ] Add support for HTTP/3
22
212
 
23
213
  ## Development
24
214
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
215
+ To set up the development environment after cloning the repo, run:
216
+
217
+ ```bash
218
+ $ bin/setup
219
+ ```
220
+
221
+ To run the tests, run:
26
222
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
223
+ ```bash
224
+ $ rake test
225
+ ```
28
226
 
29
227
  ## Contributing
30
228
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lennarb.
229
+ Bug reports and pull requests are welcome on GitHub at https://github.com/aristotelesbr/lennarb.
data/lib/lenna/base.rb CHANGED
@@ -39,7 +39,7 @@ module Lenna
39
39
  #
40
40
  # @since 0.1.0
41
41
  def listen(port = DEFAULT_PORT, host: DEFAULT_HOST, **)
42
- puts "⚡ Listening on #{host}:#{port}"
42
+ puts "⚡ Listening on #{host}:#{port}. \n Use Ctrl-C to stop the server."
43
43
 
44
44
  # Add the logging middleware to the stack
45
45
  use(Middleware::Default::Logging, Middleware::Default::ErrorHandler)
@@ -40,7 +40,7 @@ module Lenna
40
40
  content_type = env['CONTENT_TYPE']
41
41
  @headers ||= env.select { |k, _| k.start_with?('HTTP_') }
42
42
  .transform_keys { |k| format_header_name(k) }
43
-
43
+
44
44
  @headers['Content-Type'] = content_type if content_type
45
45
  @headers
46
46
  end
@@ -62,6 +62,11 @@ module Lenna
62
62
  {}
63
63
  end
64
64
 
65
+ # This method parses the body params.
66
+ #
67
+ # @return [Hash] the request body params
68
+ #
69
+ # @api private
65
70
  def parse_body_params
66
71
  case media_type
67
72
  when 'application/json'
@@ -73,6 +78,27 @@ module Lenna
73
78
  end
74
79
  end
75
80
 
81
+ # This method parses the post params.
82
+ #
83
+ # @return [Hash] the request post params
84
+ #
85
+ # @api private
86
+ def post_params
87
+ @post_params ||=
88
+ if body_content.empty?
89
+ {}
90
+ else
91
+ ::Rack::Utils.parse_nested_query(body_content)
92
+ end
93
+ end
94
+
95
+ # This method formats the header name.
96
+ #
97
+ # @param name [String] the header name
98
+ #
99
+ # @return [String] the formatted header name
100
+ #
101
+ # @api private
76
102
  def format_header_name(name)
77
103
  name.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
78
104
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lennarb
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
 
6
6
  public_constant :VERSION
7
7
  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.1.3
4
+ version: 0.1.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: 2023-11-24 00:00:00.000000000 Z
11
+ date: 2023-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize