51 lines
997 B
Ruby
51 lines
997 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rack'
|
|
require 'json'
|
|
|
|
require_relative '../renderer'
|
|
|
|
module FO::HTTP
|
|
class Response < Rack::Response
|
|
attr_reader :request
|
|
|
|
def initialize(request)
|
|
super('')
|
|
@request = request
|
|
end
|
|
|
|
def json(thingy)
|
|
body(JSON.generate(thingy))
|
|
content_type('application/json; charset=utf-8')
|
|
self
|
|
end
|
|
|
|
def template(template_name, locals = {})
|
|
path = File.expand_path("#{__dir__}/../views/#{template_name}.erb")
|
|
layout_name = locals['layout'].nil? ? 'default' : locals['layout']
|
|
layout_path = File.expand_path("#{__dir__}/../views/layouts/#{layout_name}.erb")
|
|
|
|
body(FO::Renderer.new(layout_path, path, locals).render)
|
|
|
|
content_type('text/html; charset=utf-8')
|
|
self
|
|
end
|
|
|
|
def status(s)
|
|
@status = s
|
|
self
|
|
end
|
|
|
|
def body(body)
|
|
write(body)
|
|
self
|
|
end
|
|
|
|
def content_type(type)
|
|
set_header('Content-Type', type)
|
|
self
|
|
end
|
|
|
|
end
|
|
end
|