initial commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
# 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
|
||||
Reference in New Issue
Block a user