eredor 0.1.0 → 0.2.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +33 -16
  3. data/lib/eredor/version.rb +1 -1
  4. data/lib/eredor.rb +45 -32
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49c19000db63cb02454327189e781e1e0329a5d7eee156cb3b92399b3d51961a
4
- data.tar.gz: 596d2f66edf7dfa31ae3230232aae2a281e3d6b24dec91faafddce08667371ed
3
+ metadata.gz: 15c1d4fafd9ce4bb8c38fdab4c657392cdb8af9ef2a1179eb1d615948062604a
4
+ data.tar.gz: 2ce2e3ce4769c37e0e41ddd1da9e0bc2222c1a16f29dec4c42cf8a04e870c6e8
5
5
  SHA512:
6
- metadata.gz: 98a9e1b1aead447f706bf0861f33138790cf4bb1c98862fa6002d82f57a9d6897baf99594f82a20d202a79154d3e5a18eb88e445e86f9054f638ef0ae2381768
7
- data.tar.gz: af4dc482d81d57055f8b8bf817e8bae29dd1350762e5817ce8b9a3fe3a62182dc9404a2aef235f9c8b3ed085c8022c6ab93e26a203adef74650753540cd6c3ef
6
+ metadata.gz: cda31701aaae451ce87c7abb7661164e0952a62180d74abe967126fb737127553352a0edfc274f703396eec4f0f97841028d5bd46e715d0227b728d1e5237f30
7
+ data.tar.gz: 4b4f5d92c2025c212fb5026c9c7f9df3b0e7241ecf585d08c882dc4e025808e6fa9877d3de365e6f6b7d8bf1bbd06249b1c62403959f8b96f212982ce1e348bf
data/README.md CHANGED
@@ -1,38 +1,55 @@
1
- # Eredor
1
+ # 🏔️ **Eredor**
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
3
 
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/eredor`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+ [![Gem Version](https://badge.fury.io/rb/eredor.svg)](https://rubygems.org/gems/eredor)
5
+ [![Downloads](https://badgen.net/rubygems/dt/eredor)](https://rubygems.org/gems/eredor)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
7
+ [![GitHub Repo stars](https://img.shields.io/github/stars/devguilhermeribeiiro/eredor?style=social)](https://github.com/devguilhermeribeiiro/eredor)
6
8
 
7
- ## Installation
9
+ > Eredor is a minimal set of OOP-based classes to help you develop small web applications without a dedicated framework. No magic, just Ruby.
8
10
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_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.
11
+ ---
12
+ ## ⚡ **Instalation**
10
13
 
11
- Install the gem and add to the application's Gemfile by executing:
14
+ Install via [RubyGems.org](https://rubygems.org/gems/eredor):
12
15
 
13
16
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
17
+ gem install eredor
15
18
  ```
16
-
17
- If bundler is not being used to manage dependencies, install the gem by executing:
19
+ Or add to your Gemfile:
18
20
 
19
21
  ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
22
+ bundle add eredor
23
+ bundle install
21
24
  ```
22
25
 
23
26
  ## Usage
24
27
 
25
- TODO: Write usage instructions here
28
+ Add to your config.ru:
29
+
30
+ ```bash
31
+ require 'rack'
32
+ require 'eredor'
26
33
 
27
- ## Development
34
+ run do |env|
35
+ request = Rack::Request.new(env)
36
+ router = Eredor::Router.new(request)
28
37
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+ router.get '/' do
39
+ '<h1>Hello from Eredor</h1>'
40
+ end
30
41
 
31
- 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).
42
+ router.handle
43
+ end
44
+ ```
45
+ Ensure you have the rackup gem installed and run:
46
+ ```bash
47
+ rackup
48
+ ```
32
49
 
33
50
  ## Contributing
34
51
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/eredor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/eredor/blob/master/CODE_OF_CONDUCT.md).
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/devguilhermeribeiiro/eredor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/devguilhermeribeiiro/eredor/blob/master/CODE_OF_CONDUCT.md).
36
53
 
37
54
  ## License
38
55
 
@@ -40,4 +57,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
57
 
41
58
  ## Code of Conduct
42
59
 
43
- Everyone interacting in the Eredor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/eredor/blob/master/CODE_OF_CONDUCT.md).
60
+ Everyone interacting in the Eredor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/devguilhermeribeiiro/eredor/blob/master/CODE_OF_CONDUCT.md).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eredor
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/eredor.rb CHANGED
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
1
+ # frozen_string_literal: false
2
2
 
3
3
  require_relative "eredor/version"
4
4
 
@@ -26,18 +26,17 @@ module Eredor
26
26
  def handle
27
27
  method = @request.request_method.to_sym
28
28
  path = @request.path
29
- body = %w[POST PUT PATCH].include?(method.to_s) ? @request.POST : nil
30
- query = @request.GET
29
+ params = @request.params
31
30
 
32
31
  @routes[method].each do |route|
33
32
  if route[:pattern]
34
33
  match = route[:pattern].match(path)
35
34
  next unless match
36
35
 
37
- params = extract_params(match, route[:keys])
38
- return call_handler(route, params, body, query)
36
+ params.merge!(extract_params(match))
37
+ return call_handler(route, params)
39
38
  elsif route[:path] == path
40
- return call_handler(route, {}, body, query)
39
+ return call_handler(route, params)
41
40
  end
42
41
  end
43
42
 
@@ -47,46 +46,60 @@ module Eredor
47
46
  private
48
47
 
49
48
  def register_route(method, path, &block)
50
- raise(StandardError, 'A block must be given') unless block_given?
49
+ raise(StandardError, "A block must be given") unless block_given?
51
50
 
52
- keys = []
53
- regex_path = path.gsub(/:(\w+)/) do |match|
54
- keys << match[1..-1].to_sym
55
- "(?<#{keys.last}>[^/]+)"
56
- end
57
-
58
- route = { path: path, handler: block }
59
- route[:pattern] = /\A#{regex_path}\z/ if keys.any?
60
- route[:keys] = keys if keys.any?
51
+ regex_path = path.gsub(/:(\w+)/) { |m| "(?<#{m[1..-1]}>[^/]+)" }
52
+ pattern = /\A#{regex_path}\z/
61
53
 
62
- @routes[method] << route
54
+ @routes[method] << {
55
+ path: path,
56
+ pattern: path.include?(":") ? pattern : nil,
57
+ handler: block
58
+ }
63
59
  end
64
60
 
65
- def extract_params(match, keys)
66
- keys.each_with_object({}) do |key, h|
67
- h[key] = match[key.to_s]
68
- end
61
+ def extract_params(match)
62
+ match.named_captures.transform_keys(&:to_sym)
69
63
  end
70
64
 
71
- def call_handler(route, params, body, query)
72
- env = {
73
- params: params,
74
- body: body,
75
- query: query
76
- }
77
- result = route[:handler].call(env)
65
+ def call_handler(route, params)
66
+ result = route[:handler].call(params)
78
67
 
79
- # Permitir retorno flexível
80
68
  case result
81
69
  when Array || Rack::Response then result # já está no formato [status, headers, body]
82
- when String then [200, { 'content-type' => 'text/html' }, [result]]
70
+ when String then [200, { "content-type" => "text/html" }, [result]]
83
71
  else
84
- [500, { 'content-type' => 'text/plain' }, ['Internal Server Error']]
72
+ [500, { "content-type" => "text/plain" }, ["Internal Server Error"]]
85
73
  end
86
74
  end
87
75
 
88
76
  def not_found
89
- [404, { 'content-type' => 'text/plain' }, ['Not Found']]
77
+ [404, { "content-type" => "text/plain" }, ["Not Found"]]
78
+ end
79
+ end
80
+
81
+ class BaseController
82
+ attr_reader :params
83
+
84
+ def initialize(params)
85
+ @params = params
86
+ end
87
+
88
+ def render(file)
89
+ content = get_view(file)
90
+ template = ERB.new(content)
91
+ template.result(binding)
92
+ end
93
+
94
+ private
95
+
96
+ def get_view(file)
97
+ File.read("./app/views/#{controller_name}/#{file}.html.erb")
98
+ end
99
+
100
+ def controller_name
101
+ controller = self.class.name.to_s
102
+ controller.gsub(/Controller/, "").downcase
90
103
  end
91
104
  end
92
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eredor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Ribeiro