rackr 0.0.53 → 0.0.56

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: 207a61eead2347b17a9f17c396934954640a8658ebede9625ad62e05e2db9465
4
- data.tar.gz: e8e7c162619f150483c1c1000f4cfff16a65cc68596871d9f034c9d6fcdf5e1a
3
+ metadata.gz: 8e0064b397729023c9354b47f6b98abb0526fd42d67905dbf12f34283368ff4d
4
+ data.tar.gz: e0fa0a69981b67e151af1763f46c802719f8ea68af1d426eb53972c68e70e577
5
5
  SHA512:
6
- metadata.gz: 9b5c019b83ffd983b74a35b07588d190f64c1ac01e1c7efffd9cc5595d5901f653878c5ad3ba2496b4557841d12a0886cdab68d9eda262604290a73345474c00
7
- data.tar.gz: c3b6d38fa59027763530fe14f1e4630fe78de3aa568c7e998d93725e06bb3f30308be706fa26f5c30cf2fe5d32c568065f39617dd1db4d4a7222d9558a6068a6
6
+ metadata.gz: 8f7cdc5a620b6eb51e30ea81276f4d3c2ca70d10e2bb4d12f4721319ca9a89bafd53885ff5fe51390f83980b281996ec6551217482407d740fb39a9b203f5b81
7
+ data.tar.gz: ca6b207e9089104fe1bde0cad0e162cb0c126ee6f11d8420615dae7e1e7c8eaf2c293fc2d40341ca9c2fc59ff3160ad375e1ece5a001aecd4a6a76e690320421
data/lib/rackr/action.rb CHANGED
@@ -52,7 +52,7 @@ class Rackr
52
52
  ::File.read("#{base_path}/#{paths}.html.erb")
53
53
  end
54
54
 
55
- layout_content = file_or_nil.("#{base_path}/#{layout_path}.html.erb")
55
+ layout_content = file_or_nil.call("#{base_path}/#{layout_path}.html.erb")
56
56
 
57
57
  parsed_erb =
58
58
  if layout_content
@@ -76,6 +76,12 @@ class Rackr
76
76
  end
77
77
  end
78
78
 
79
+ def load_json(val)
80
+ return Oj.load(val.body.read) if val.is_a?(Rack::Request)
81
+
82
+ Oj.load(val)
83
+ end
84
+
79
85
  def html(content = '', status: 200, headers: {}, &block)
80
86
  if content == '' && block_given? && respond_to?(:html_slice)
81
87
  if respond_to?(:layout)
@@ -103,12 +109,14 @@ class Rackr
103
109
  end
104
110
 
105
111
  def json(content = {}, status: 200, headers: {})
106
- [status, { 'content-type' => 'application/json' }.merge(headers), [Oj.dump(content, mode: :compat)]]
112
+ content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
113
+ [status, { 'content-type' => 'application/json' }.merge(headers), [content]]
107
114
  end
108
115
 
109
116
  def json_response(content = {}, status: 200, headers: {})
117
+ content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
110
118
  Rack::Response.new(
111
- Oj.dump(content, mode: :compat),
119
+ content,
112
120
  status,
113
121
  { 'content-type' => 'application/json' }.merge(headers)
114
122
  )
@@ -126,7 +134,7 @@ class Rackr
126
134
  )
127
135
  end
128
136
 
129
- def erb(content, binding_context: nil, &block)
137
+ def erb(content, binding_context: nil)
130
138
  eval(Erubi::Engine.new(content).src, binding_context)
131
139
  end
132
140
 
data/lib/rackr/html.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Rackr
2
4
  module HTML
3
5
  def self.included(base)
@@ -11,24 +11,15 @@ class Rackr
11
11
  def call(route = nil)
12
12
  request = Rack::Request.new(@env)
13
13
 
14
- return request if route.nil?
15
- return request unless route.has_params
14
+ return request if route.nil? || !route.has_params
16
15
 
17
- update_request_params(request, route)
18
- end
19
-
20
- private
21
-
22
- def update_request_params(request, route)
23
16
  i = 0
24
-
25
17
  while i < route.splitted_path.size
26
18
  route_word = route.splitted_path[i]
27
19
  if route_word.start_with?(':')
28
20
  param = @spplited_request_path[i]
29
- param = param.to_i if is_a_integer_string?(param)
30
21
 
31
- update_request_param(request, route_word, param)
22
+ request.params[route_word[1..].to_sym] = param
32
23
  end
33
24
 
34
25
  i += 1
@@ -36,14 +27,6 @@ class Rackr
36
27
 
37
28
  request
38
29
  end
39
-
40
- def is_a_integer_string?(string)
41
- string =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
42
- end
43
-
44
- def update_request_param(request, word, param)
45
- request.update_param(word.sub(':', '').to_sym, param)
46
- end
47
30
  end
48
31
  end
49
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Rackr
2
4
  class Router
3
5
  module Errors
@@ -9,7 +11,7 @@ class Rackr
9
11
  html do
10
12
  tag :head do
11
13
  title 'Application error'
12
- _ %q(<style>
14
+ _ '<style>
13
15
  html * { padding:0; margin:0; }
14
16
  body * { padding:10px 20px; }
15
17
  body * * { padding:0; }
@@ -25,7 +27,7 @@ class Rackr
25
27
  padding: 1em;
26
28
  margin-bottom: 1em;
27
29
  }
28
- </style>)
30
+ </style>'
29
31
  end
30
32
  tag :body do
31
33
  div id: 'summary' do
@@ -50,39 +52,36 @@ class Rackr
50
52
  span '(innermost first)'
51
53
  end
52
54
 
53
- tag :p, first, class: "first-p"
55
+ tag :p, first, class: 'first-p'
54
56
  br
55
57
 
56
58
  line_number = extract_line_number(first)
57
- match = first.match(%r{^(\/[\w\/.-]+)})
59
+ match = first.match(%r{^(/[\w/.-]+)})
58
60
  file_path = (match ? match[1] : nil)
59
- if file_path != nil
61
+ unless file_path.nil?
60
62
  lines = []
61
63
  File.open(file_path) do |file|
62
64
  lines = file.readlines
63
65
  end
64
66
 
65
67
  lines.map!.with_index do |line, i|
66
- "#{i+1}: #{line} \n"
68
+ "#{i + 1}: #{line} \n"
67
69
  end
68
70
 
69
- tag :pre, slice_around_index(lines, line_number).join("").chomp
71
+ tag :pre, slice_around_index(lines, line_number).join('').chomp
70
72
  end
71
73
 
72
74
  tag :p, tail.join("\n")
73
75
  end
74
76
 
75
-
76
77
  def extract_line_number(input)
77
- if match = input.match(/:(\d+):in/)
78
+ if (match = input.match(/:(\d+):in/))
78
79
  match[1].to_i
79
- else
80
- nil
81
80
  end
82
81
  end
83
82
 
84
83
  def slice_around_index(array, index)
85
- return array if index == nil || index < 1
84
+ return array if index.nil? || index < 1
86
85
 
87
86
  index -= 1
88
87
  start_index = [index - 2, 0].max
@@ -13,15 +13,15 @@ class Rackr
13
13
 
14
14
  class << self
15
15
  def check_scope_name(path)
16
- return if path.is_a?(String) || path.is_a?(Symbol) || path == nil
16
+ return if path.is_a?(String) || path.is_a?(Symbol) || path.nil?
17
17
 
18
18
  raise(InvalidBranchNameError, "Route scope name must be a `string` or a `symbol`, got: '#{path}'")
19
19
  end
20
20
 
21
21
  def check_scope_slashes(path)
22
- if path.is_a?(String) && path.include?('/')
23
- raise(InvalidBranchNameError, "Avoid slashes in scope name, use nested scopes instead, got: '#{path}'")
24
- end
22
+ return unless path.is_a?(String) && path.include?('/')
23
+
24
+ raise(InvalidBranchNameError, "Avoid slashes in scope name, use nested scopes instead, got: '#{path}'")
25
25
  end
26
26
 
27
27
  def check_path(path)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Rackr
2
4
  class Router
3
5
  class Route
data/lib/rackr/router.rb CHANGED
@@ -35,14 +35,16 @@ class Rackr
35
35
  end
36
36
 
37
37
  def call(env)
38
- @splitted_request_path_info = env['PATH_INFO'].split('/')
38
+ path_info = env['PATH_INFO']
39
+
40
+ @splitted_request_path_info = path_info.split('/')
39
41
  @current_request_path_info =
40
- (env['PATH_INFO'] == '/') ? env['PATH_INFO'] : env['PATH_INFO'].sub(/\/\z/, '') # remove trailing "/"
42
+ path_info == '/' ? path_info : path_info.chomp('/') # remove trailing "/"
41
43
 
42
44
  request_builder = BuildRequest.new(env, @splitted_request_path_info)
43
45
  env['REQUEST_METHOD'] = 'GET' if env['REQUEST_METHOD'] == 'HEAD'
44
46
 
45
- route_instance = match_route(env)
47
+ route_instance = match_route(env['REQUEST_METHOD'])
46
48
  return call_endpoint(@not_found, request_builder.call) if route_instance.nil?
47
49
 
48
50
  rack_request = request_builder.call(route_instance)
@@ -74,7 +76,7 @@ class Rackr
74
76
  rescue Exception => e
75
77
  return @error.call(request_builder.call, e) if !@dev_mode || ENV['RACKR_ERROR_DEV']
76
78
 
77
- call_endpoint(Errors::DevHtml, env.merge({'error' => e}))
79
+ call_endpoint(Errors::DevHtml, env.merge({ 'error' => e }))
78
80
  end
79
81
 
80
82
  def add(method, path, endpoint, as: nil, route_befores: [], route_afters: [])
@@ -86,8 +88,8 @@ class Rackr
86
88
 
87
89
  method = :get if method == :head
88
90
 
89
- wildcard = (path == '*') ? true : false
90
- path = path.sub(/\A\//, '')
91
+ wildcard = path == '*'
92
+ path = path.is_a?(Symbol) ? path.inspect : path.sub(%r{\A/}, '')
91
93
  path_with_scopes = "/#{not_empty_scopes.join('/')}#{put_path_slash(path)}"
92
94
  add_named_route(method, path_with_scopes, as)
93
95
 
@@ -147,7 +149,7 @@ class Rackr
147
149
  def call_endpoint(endpoint, content)
148
150
  return endpoint.call(content) if endpoint.respond_to?(:call)
149
151
 
150
- if endpoint.include?(Rackr::Action) || endpoint.include?(Rackr::Callback)
152
+ if endpoint < Rackr::Action || endpoint < Rackr::Callback
151
153
  return endpoint.new(routes: @routes, config: @config).call(content)
152
154
  end
153
155
 
@@ -165,8 +167,8 @@ class Rackr
165
167
  return @routes.send(method.downcase)[:root] = path_with_scopes if path_with_scopes == '/'
166
168
  return @routes.send(method.downcase)[as] = path_with_scopes unless as.nil?
167
169
 
168
- key = path_with_scopes.sub("/","").gsub(":","").gsub("/","_")
169
- @routes.send(method.downcase)["#{key}".to_sym] = path_with_scopes
170
+ key = path_with_scopes.sub('/', '').gsub(':', '').gsub('/', '_')
171
+ @routes.send(method.downcase)[key.to_s.to_sym] = path_with_scopes
170
172
  end
171
173
 
172
174
  def push_to_scope(method, route_instance)
@@ -186,6 +188,7 @@ class Rackr
186
188
  def put_path_slash(path)
187
189
  if not_empty_scopes != []
188
190
  return '' if ['/', ''].include?(path)
191
+
189
192
  return "/#{path}"
190
193
  end
191
194
 
@@ -196,35 +199,40 @@ class Rackr
196
199
  @scopes.reject { |v| (v == '') }
197
200
  end
198
201
 
199
- def match_route(env, last_tail = nil, found_scopes = [])
200
- instance_routes =
201
- if last_tail.nil?
202
- last_tail = @splitted_request_path_info.drop(1)
202
+ def match_route(request_method)
203
+ last_tail = @splitted_request_path_info.drop(1)
204
+ found_scopes = []
203
205
 
204
- @instance_routes[env['REQUEST_METHOD']]
205
- else
206
- @instance_routes[env['REQUEST_METHOD']].dig(*found_scopes)
207
- end
206
+ instance_routes = @instance_routes[request_method]
208
207
 
209
- segment, *tail = last_tail
208
+ while last_tail && !last_tail.empty?
209
+ segment = last_tail.shift
210
+ found_route = nil
210
211
 
211
- instance_routes.each do |scope, _v|
212
- next if scope == :__instances
212
+ instance_routes.each_key do |scope|
213
+ next if scope == :__instances
213
214
 
214
- if segment == scope || scope.start_with?(':')
215
- found_scopes.push(scope)
216
- break
217
- end
218
- end
215
+ if segment == scope
216
+ found_scopes << scope
217
+ instance_routes = @instance_routes[request_method].dig(*found_scopes)
218
+ break
219
+ elsif scope.start_with?(':')
220
+ found_route = @instance_routes[request_method].dig(
221
+ *(found_scopes + [:__instances])
222
+ )&.detect { |route_instance| route_instance.match?(@current_request_path_info) }
219
223
 
220
- if tail.empty? || found_scopes == []
221
- return @instance_routes[env['REQUEST_METHOD']].dig(
222
- *(found_scopes << :__instances)
223
- )
224
- &.detect { |route_instance| route_instance.match?(@current_request_path_info) }
224
+ return found_route if found_route
225
+
226
+ found_scopes << scope
227
+ instance_routes = @instance_routes[request_method].dig(*found_scopes)
228
+ break
229
+ end
230
+ end
225
231
  end
226
232
 
227
- match_route(env, tail, found_scopes)
233
+ @instance_routes[request_method].dig(
234
+ *(found_scopes + [:__instances])
235
+ )&.detect { |route_instance| route_instance.match?(@current_request_path_info) }
228
236
  end
229
237
  end
230
238
  end
data/lib/rackr.rb CHANGED
@@ -10,7 +10,7 @@ require_relative 'rackr/router'
10
10
  class Rackr
11
11
  class NotFound < StandardError; end
12
12
 
13
- HTTP_METHODS = %w[GET POST DELETE PUT TRACE OPTIONS PATCH]
13
+ HTTP_METHODS = %w[GET POST DELETE PUT TRACE OPTIONS PATCH].freeze
14
14
 
15
15
  include Action
16
16
  include HTML
@@ -41,7 +41,7 @@ class Rackr
41
41
  @router.append_scope(
42
42
  name,
43
43
  scope_befores: before,
44
- scope_afters: after,
44
+ scope_afters: after
45
45
  )
46
46
  instance_eval(&block)
47
47
 
@@ -64,10 +64,47 @@ class Rackr
64
64
  end
65
65
  end
66
66
 
67
+ # Beta
68
+ def resources(name, id:)
69
+ const_name = name.to_s.capitalize
70
+ id ||= :id
71
+
72
+ scope name do
73
+ get Object.const_get("Actions::#{const_name}::Index") if Object.const_defined?("Actions::#{const_name}::Index")
74
+ get 'new', Object.const_get("Actions::#{const_name}::New") if Object.const_defined?("Actions::#{const_name}::New")
75
+ post Object.const_get("Actions::#{const_name}::Index") if Object.const_defined?("Actions::#{const_name}::Index")
76
+
77
+ resource_actions = proc do
78
+ get Object.const_get("Actions::#{const_name}::Show") if Object.const_defined?("Actions::#{const_name}::Show")
79
+ if Object.const_defined?("Actions::#{const_name}::Edit")
80
+ get 'edit', Object.const_get("Actions::#{const_name}::Edit")
81
+ end
82
+ if Object.const_defined?("Actions::#{const_name}::Update")
83
+ put Object.const_get("Actions::#{const_name}::Update")
84
+ end
85
+ if Object.const_defined?("Actions::#{const_name}::Delete")
86
+ delete Object.const_get("Actions::#{const_name}::Delete")
87
+ end
88
+ end
89
+
90
+ if Object.const_defined?("Callbacks::#{const_name}::Assign")
91
+ scope(id.to_sym, before: Object.const_get("Callbacks::#{const_name}::Assign"), &resource_actions)
92
+ else
93
+ scope(id.to_sym, &resource_actions)
94
+ end
95
+ end
96
+ end
97
+
67
98
  HTTP_METHODS.each do |http_method|
68
99
  define_method(http_method.downcase.to_sym) do |*params, before: [], after: [], as: nil, &block|
69
100
  path = params[0] || ''
70
101
  endpoint = params[1] || ''
102
+ scopes = []
103
+ if path.is_a?(String) && path.include?('/')
104
+ scopes = path.split('/')[0...-1]
105
+ path = path.split('/').pop
106
+ end
107
+ scopes.each { |scope_name| @router.append_scope(scope_name) }
71
108
 
72
109
  if block.respond_to?(:call)
73
110
  @router.add(
@@ -93,6 +130,8 @@ class Rackr
93
130
  route_afters: after
94
131
  )
95
132
  end
133
+
134
+ scopes.length.times { @router.clear_last_scope }
96
135
  end
97
136
  end
98
137
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rackr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.53
4
+ version: 0.0.56
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique F. Teixeira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-12 00:00:00.000000000 Z
11
+ date: 2025-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubi
@@ -78,7 +78,7 @@ dependencies:
78
78
  - - "<"
79
79
  - !ruby/object:Gem::Version
80
80
  version: '1.0'
81
- description: A complete http router solution that fit well with pure rack apps.
81
+ description: A complete, simple and easy web micro-framework.
82
82
  email: hriqueft@gmail.com
83
83
  executables: []
84
84
  extensions: []
@@ -97,7 +97,7 @@ homepage: https://github.com/henrique-ft/rackr
97
97
  licenses:
98
98
  - MIT
99
99
  metadata: {}
100
- post_install_message:
100
+ post_install_message:
101
101
  rdoc_options: []
102
102
  require_paths:
103
103
  - lib
@@ -112,8 +112,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0'
114
114
  requirements: []
115
- rubygems_version: 3.5.16
116
- signing_key:
115
+ rubygems_version: 3.5.3
116
+ signing_key:
117
117
  specification_version: 4
118
- summary: A complete http router solution that fit well with pure rack apps.
118
+ summary: A complete, simple and easy web micro-framework.
119
119
  test_files: []