class Freedom::Controller
Public Class Methods
new()
click to toggle source
# File lib/freedom/core/controller.rb, line 3 def initialize @request = nil @response = nil end
Public Instance Methods
call(action, req)
click to toggle source
# File lib/freedom/core/controller.rb, line 8 def call(action, req) @request = req @response = Response.new(nil, 200) catch(:halt) do body = send(action) return_plain end @response end
erb(name)
click to toggle source
# File lib/freedom/core/controller.rb, line 50 def erb(name) html = ERB.new(File.read("#{Config.views_dir}/#{name}.erb", encoding: Config.encoding)).result(binding) return_html(html) end
halt(body: nil, status: 200)
click to toggle source
# File lib/freedom/core/controller.rb, line 74 def halt(body: nil, status: 200) @response.status = status if status @response.body = [body] if body throw :halt end
html(name)
click to toggle source
# File lib/freedom/core/controller.rb, line 45 def html(name) html = File.read("#{Config.views_dir}/#{name}.html") return_html(html) end
markdown(md, extension = {})
click to toggle source
# File lib/freedom/markdown.rb, line 5 def markdown(md, extension = {}) return_html(markdown_to_html(md, extension)) end
markdown_to_html(md, extension = {})
click to toggle source
# File lib/freedom/markdown.rb, line 9 def markdown_to_html(md, extension = {}) mkdown_obj = Redcarpet::Markdown.new(Redcarpet::Render::HTML, extension) mkdown_obj.render(md) end
params()
click to toggle source
# File lib/freedom/core/controller.rb, line 26 def params @request.params end
redirect(uri)
click to toggle source
# File lib/freedom/core/controller.rb, line 69 def redirect(uri) @response.location = uri halt status: 303 end
request()
click to toggle source
# File lib/freedom/core/controller.rb, line 18 def request @request end
response()
click to toggle source
# File lib/freedom/core/controller.rb, line 22 def response @response end
return_html(body)
click to toggle source
# File lib/freedom/core/controller.rb, line 35 def return_html(body) @response.content_type = "text/html;charset=#{Config.encoding}" halt(body: body) end
return_json(body)
click to toggle source
# File lib/freedom/core/controller.rb, line 40 def return_json(body) @response.content_type = "application/json" halt(body: body) end
return_plain(body)
click to toggle source
# File lib/freedom/core/controller.rb, line 30 def return_plain(body) @response.content_type = "text/plain;charset=#{Config.encoding}" halt(body: body) end
url(path, scheme = nil)
click to toggle source
# File lib/freedom/core/controller.rb, line 64 def url(path, scheme = nil) scheme ||= @request.scheme "#{scheme}://#{@request.domain + path}" end