51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/rackr/action.rb', line 51
def self.included(base)
base.class_eval do
attr_reader :routes, :config, :deps, :db if self != Rackr
def initialize(routes: nil, config: nil)
@routes = routes
@config = config
@deps = config[:deps]
@db = config.dig(:deps, :db)
end
def render(**opts)
type = opts.keys.first
content = opts[type]
@@render[type]&.call(content, **opts) || _render_view(content, **opts)
end
def view_response(
paths,
status: 200,
headers: {},
layout_path: 'layout'
)
_render_view(
paths,
status: status,
headers: ,
layout_path: layout_path,
response_instance: true
)
end
def _render_view(
paths,
status: 200,
headers: {},
layout_path: 'layout',
response_instance: false,
view: nil
)
base_path = config.dig(:views, :path) || 'views'
file_or_nil = lambda do |path|
::File.read(path)
rescue Errno::ENOENT
nil
end
file_content = if paths.is_a?(Array)
paths.map { |path| ::File.read("#{base_path}/#{path}.html.erb") }.join
else
::File.read("#{base_path}/#{paths}.html.erb")
end
layout_content = file_or_nil.call("#{base_path}/#{layout_path}.html.erb")
parsed_erb =
if layout_content
load_erb(layout_content) do
load_erb(file_content, binding_context: binding)
end
else
load_erb(file_content, binding_context: binding)
end
if response_instance
return Rack::Response.new(
parsed_erb,
status,
.call('text/html; charset=utf-8', , parsed_erb)
)
end
[status, .call('text/html; charset=utf-8', , parsed_erb), [parsed_erb]]
end
def not_found!
raise Rackr::NotFound
end
def load_json(val)
return Oj.load(val.body.read) if val.is_a?(Rack::Request)
Oj.load(val)
end
def html_response(content = '', status: 200, headers: {})
Rack::Response.new(content, status, .call('text/html; charset=utf-8', , content))
end
def json_response(content = {}, status: 200, headers: {})
content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
Rack::Response.new(content, status, .call('application/json', , content))
end
def text_response(content, status: 200, headers: {})
Rack::Response.new(content, status, .call('text/plain', , content))
end
def load_erb(content, binding_context: nil)
eval(Erubi::Engine.new(content).src, binding_context)
end
def head(status, headers: {})
[status, , []]
end
def head_response(status, headers: {})
Rack::Response.new(nil, status, )
end
def redirect_response(url, headers: {})
Rack::Response.new(
nil,
302,
{ 'location' => url }.merge()
)
end
def redirect_to(url, headers: {})
[302, { 'location' => url }.merge(), []]
end
def response(body = nil, status = 200, = {})
Rack::Response.new(body, status, )
end
end
end
|