eredor 0.2.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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eredor/version.rb +1 -1
  3. data/lib/eredor.rb +22 -36
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '07877c607d2d4cd1c6ad3bf504d48f8cc19c70bd6cfacfe344dd17ac2d479796'
4
- data.tar.gz: cee85827f625435e7e93a205c3ba50403ca68cdb8794fb49bb5f5bf72051ff63
3
+ metadata.gz: 15c1d4fafd9ce4bb8c38fdab4c657392cdb8af9ef2a1179eb1d615948062604a
4
+ data.tar.gz: 2ce2e3ce4769c37e0e41ddd1da9e0bc2222c1a16f29dec4c42cf8a04e870c6e8
5
5
  SHA512:
6
- metadata.gz: 64e1c90f1d916710ba2af39917f1dcb59fe7ea381cc733a538e24ced5dad8c3e8aa4e432686aea5663dd05bbb8b589fc47849ced65f22c5909442c5bced64032
7
- data.tar.gz: 4a68d9b223c815b08c367bf932dbf2cde65212ba6bd39a91abbfc616c50d892708e57445acab89122f581eeb91cd55d32fe4072ba86b101a030255d029109dd0
6
+ metadata.gz: cda31701aaae451ce87c7abb7661164e0952a62180d74abe967126fb737127553352a0edfc274f703396eec4f0f97841028d5bd46e715d0227b728d1e5237f30
7
+ data.tar.gz: 4b4f5d92c2025c212fb5026c9c7f9df3b0e7241ecf585d08c882dc4e025808e6fa9877d3de365e6f6b7d8bf1bbd06249b1c62403959f8b96f212982ce1e348bf
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eredor
4
- VERSION = "0.2.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
 
@@ -49,34 +48,23 @@ module Eredor
49
48
  def register_route(method, path, &block)
50
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
70
  when String then [200, { "content-type" => "text/html" }, [result]]
@@ -91,11 +79,10 @@ module Eredor
91
79
  end
92
80
 
93
81
  class BaseController
94
- attr_reader :params, :request
82
+ attr_reader :params
95
83
 
96
- def initialize(request)
97
- @params = request.params
98
- @request = request
84
+ def initialize(params)
85
+ @params = params
99
86
  end
100
87
 
101
88
  def render(file)
@@ -107,13 +94,12 @@ module Eredor
107
94
  private
108
95
 
109
96
  def get_view(file)
110
- File.read("../views/#{controller_name}/#{file}.html.erb")
97
+ File.read("./app/views/#{controller_name}/#{file}.html.erb")
111
98
  end
112
99
 
113
100
  def controller_name
114
- controller = self.class.name
115
- controller.gsub!(/Controller/, "")
116
- controller.downcase!
101
+ controller = self.class.name.to_s
102
+ controller.gsub(/Controller/, "").downcase
117
103
  end
118
104
  end
119
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Ribeiro