rackr 0.0.56 → 0.0.57

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: 8e0064b397729023c9354b47f6b98abb0526fd42d67905dbf12f34283368ff4d
4
- data.tar.gz: e0fa0a69981b67e151af1763f46c802719f8ea68af1d426eb53972c68e70e577
3
+ metadata.gz: 817c73cfeca9d09f9bba0002c6bc19b34d5b6bcc0299a24a3e9bb87b075c12b5
4
+ data.tar.gz: e1c4710e34bf53a780a7d6b132469151fc2e4fafaf8a27205266414a02ac7299
5
5
  SHA512:
6
- metadata.gz: 8f7cdc5a620b6eb51e30ea81276f4d3c2ca70d10e2bb4d12f4721319ca9a89bafd53885ff5fe51390f83980b281996ec6551217482407d740fb39a9b203f5b81
7
- data.tar.gz: ca6b207e9089104fe1bde0cad0e162cb0c126ee6f11d8420615dae7e1e7c8eaf2c293fc2d40341ca9c2fc59ff3160ad375e1ece5a001aecd4a6a76e690320421
6
+ metadata.gz: 63ff89e8f20793339587a042e626ab483ff524a21b90195e0b28e00c02a9e741b665b306fe30f07e05e0d7922b4ec6f6064bda8a019b0b6e0daf32cdd7a17be6
7
+ data.tar.gz: a308d0fe7e5df0c9d69ed789338bb7ded223b48c05bfac49230d588ee5b926dbb73473c4aadd1690594025779d82ba46b06a71b6b0577acf3b3bd712d1446ecf
data/lib/rackr/action.rb CHANGED
@@ -1,11 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'byebug'
3
4
  require 'erubi'
4
5
  require 'oj'
5
6
  require 'rack'
6
7
 
7
8
  class Rackr
8
9
  module Action
10
+ METHODS = {
11
+ html: lambda do |val, **opts|
12
+ [opts[:status] || 200, { 'content-type' => 'text/html' }.merge(opts[:headers] || {}), [val]]
13
+ end,
14
+ text: lambda do |val, **opts|
15
+ [opts[:status] || 200, { 'content-type' => 'text/plain' }.merge(opts[:headers] || {}), [val]]
16
+ end,
17
+ json: lambda do |val, **opts|
18
+ val = Oj.dump(val, mode: :compat) unless val.is_a?(String)
19
+ [opts[:status] || 200, { 'content-type' => 'application/json' }.merge(opts[:headers] || {}), [val]]
20
+ end
21
+ }
22
+
9
23
  def self.included(base)
10
24
  base.class_eval do
11
25
  attr_reader :routes, :config, :db if self != Rackr
@@ -16,13 +30,19 @@ class Rackr
16
30
  @db = config[:db]
17
31
  end
18
32
 
33
+ def render(**opts)
34
+ type, content = opts.first
35
+
36
+ METHODS[type]&.(content, **opts) || _render_view(content, **opts)
37
+ end
38
+
19
39
  def view_response(
20
40
  paths,
21
41
  status: 200,
22
42
  headers: {},
23
43
  layout_path: 'layout'
24
44
  )
25
- view(
45
+ _render_view(
26
46
  paths,
27
47
  status: status,
28
48
  headers: headers,
@@ -31,12 +51,13 @@ class Rackr
31
51
  )
32
52
  end
33
53
 
34
- def view(
54
+ def _render_view(
35
55
  paths,
36
56
  status: 200,
37
57
  headers: {},
38
58
  layout_path: 'layout',
39
- response_instance: false
59
+ response_instance: false,
60
+ view: nil
40
61
  )
41
62
  base_path = config.dig(:views, :path) || 'views'
42
63
 
@@ -56,11 +77,11 @@ class Rackr
56
77
 
57
78
  parsed_erb =
58
79
  if layout_content
59
- erb(layout_content) do
60
- erb(file_content, binding_context: binding)
80
+ load_erb(layout_content) do
81
+ load_erb(file_content, binding_context: binding)
61
82
  end
62
83
  else
63
- erb(file_content, binding_context: binding)
84
+ load_erb(file_content, binding_context: binding)
64
85
  end
65
86
 
66
87
  if response_instance
@@ -73,93 +94,62 @@ class Rackr
73
94
 
74
95
  [status, { 'content-type' => 'text/html' }.merge(headers), [parsed_erb]]
75
96
  end
76
- end
77
- end
78
97
 
79
- def load_json(val)
80
- return Oj.load(val.body.read) if val.is_a?(Rack::Request)
98
+ def load_json(val)
99
+ return Oj.load(val.body.read) if val.is_a?(Rack::Request)
81
100
 
82
- Oj.load(val)
83
- end
84
-
85
- def html(content = '', status: 200, headers: {}, &block)
86
- if content == '' && block_given? && respond_to?(:html_slice)
87
- if respond_to?(:layout)
88
- content = layout(&block)
89
- else
90
- html_layout(&block)
91
- content = html_slice
101
+ Oj.load(val)
92
102
  end
93
- end
94
103
 
95
- [status, { 'content-type' => 'text/html' }.merge(headers), [content]]
96
- end
97
-
98
- def html_response(content = '', status: 200, headers: {}, &block)
99
- if content == '' && block_given? && respond_to?(:html_slice)
100
- if respond_to?(:layout)
101
- content = layout(&block)
102
- else
103
- html_layout(&block)
104
- content = html_slice
104
+ def html_response(content = '', status: 200, headers: {})
105
+ Rack::Response.new(content, status, { 'content-type' => 'text/html' }.merge(headers))
105
106
  end
106
- end
107
-
108
- Rack::Response.new(content, status, { 'content-type' => 'text/html' }.merge(headers))
109
- end
110
-
111
- def json(content = {}, status: 200, headers: {})
112
- content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
113
- [status, { 'content-type' => 'application/json' }.merge(headers), [content]]
114
- end
115
-
116
- def json_response(content = {}, status: 200, headers: {})
117
- content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
118
- Rack::Response.new(
119
- content,
120
- status,
121
- { 'content-type' => 'application/json' }.merge(headers)
122
- )
123
- end
124
107
 
125
- def text(content, status: 200, headers: {})
126
- [status, { 'content-type' => 'text/plain' }.merge(headers), [content]]
127
- end
108
+ def json_response(content = {}, status: 200, headers: {})
109
+ content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
110
+ Rack::Response.new(
111
+ content,
112
+ status,
113
+ { 'content-type' => 'application/json' }.merge(headers)
114
+ )
115
+ end
128
116
 
129
- def text_response(content, status: 200, headers: {})
130
- Rack::Response.new(
131
- content,
132
- status,
133
- { 'content-type' => 'text/plain' }.merge(headers)
134
- )
135
- end
117
+ def text_response(content, status: 200, headers: {})
118
+ Rack::Response.new(
119
+ content,
120
+ status,
121
+ { 'content-type' => 'text/plain' }.merge(headers)
122
+ )
123
+ end
136
124
 
137
- def erb(content, binding_context: nil)
138
- eval(Erubi::Engine.new(content).src, binding_context)
139
- end
125
+ def load_erb(content, binding_context: nil)
126
+ eval(Erubi::Engine.new(content).src, binding_context)
127
+ end
140
128
 
141
- def head(status, headers: {})
142
- [status, headers, []]
143
- end
129
+ def head(status, headers: {})
130
+ [status, headers, []]
131
+ end
144
132
 
145
- def head_response(status, headers: {})
146
- Rack::Response.new(nil, status, headers)
147
- end
133
+ def head_response(status, headers: {})
134
+ Rack::Response.new(nil, status, headers)
135
+ end
148
136
 
149
- def redirect_response(url, headers: {})
150
- Rack::Response.new(
151
- nil,
152
- 302,
153
- { 'location' => url }.merge(headers)
154
- )
155
- end
137
+ def redirect_response(url, headers: {})
138
+ Rack::Response.new(
139
+ nil,
140
+ 302,
141
+ { 'location' => url }.merge(headers)
142
+ )
143
+ end
156
144
 
157
- def redirect_to(url, headers: {})
158
- [302, { 'location' => url }.merge(headers), []]
159
- end
145
+ def redirect_to(url, headers: {})
146
+ [302, { 'location' => url }.merge(headers), []]
147
+ end
160
148
 
161
- def response(body = nil, status = 200, headers = {})
162
- Rack::Response.new(body, status, headers)
149
+ def response(body = nil, status = 200, headers = {})
150
+ Rack::Response.new(body, status, headers)
151
+ end
152
+ end
163
153
  end
164
154
  end
165
155
  end
@@ -1,14 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'html_slice'
4
+
3
5
  class Rackr
4
6
  class Router
5
7
  module Errors
6
8
  class DevHtml
7
9
  include Rackr::Action
8
- include Rackr::HTML
10
+ include ::HtmlSlice
9
11
 
10
12
  def call(env)
11
- html do
13
+ html_layout do
12
14
  tag :head do
13
15
  title 'Application error'
14
16
  _ '<style>
data/lib/rackr.rb CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  require_relative 'rackr/action'
4
4
  require_relative 'rackr/callback'
5
- require 'html_slice'
6
- require_relative 'rackr/html'
7
5
  require_relative 'rackr/router/errors/dev_html'
8
6
  require_relative 'rackr/router'
9
7
 
@@ -13,7 +11,6 @@ class Rackr
13
11
  HTTP_METHODS = %w[GET POST DELETE PUT TRACE OPTIONS PATCH].freeze
14
12
 
15
13
  include Action
16
- include HTML
17
14
 
18
15
  def initialize(config = {}, before: [], after: [])
19
16
  @router = Router.new(config, before: before, after: after)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rackr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.56
4
+ version: 0.0.57
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique F. Teixeira
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-03-18 00:00:00.000000000 Z
10
+ date: 2025-03-30 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: erubi
@@ -87,7 +86,6 @@ files:
87
86
  - lib/rackr.rb
88
87
  - lib/rackr/action.rb
89
88
  - lib/rackr/callback.rb
90
- - lib/rackr/html.rb
91
89
  - lib/rackr/router.rb
92
90
  - lib/rackr/router/build_request.rb
93
91
  - lib/rackr/router/errors.rb
@@ -97,7 +95,6 @@ homepage: https://github.com/henrique-ft/rackr
97
95
  licenses:
98
96
  - MIT
99
97
  metadata: {}
100
- post_install_message:
101
98
  rdoc_options: []
102
99
  require_paths:
103
100
  - lib
@@ -112,8 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
109
  - !ruby/object:Gem::Version
113
110
  version: '0'
114
111
  requirements: []
115
- rubygems_version: 3.5.3
116
- signing_key:
112
+ rubygems_version: 3.6.2
117
113
  specification_version: 4
118
114
  summary: A complete, simple and easy web micro-framework.
119
115
  test_files: []
data/lib/rackr/html.rb DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Rackr
4
- module HTML
5
- def self.included(base)
6
- base.class_eval { include ::HtmlSlice }
7
- end
8
- end
9
- end