Class: Freedom::Controller
- Inherits:
-
Object
- Object
- Freedom::Controller
- Defined in:
- lib/freedom/markdown.rb,
lib/freedom/core/controller.rb
Instance Method Summary collapse
- #call(action, req) ⇒ Object
- #erb(name) ⇒ Object
- #get_cookie(key) ⇒ Object
- #halt(body: nil, status: 200) ⇒ Object
- #html(name) ⇒ Object
-
#initialize ⇒ Controller
constructor
A new instance of Controller.
- #markdown(md, extension = {}) ⇒ Object
- #markdown_to_html(md, extension = {}) ⇒ Object
- #params ⇒ Object
- #redirect(uri) ⇒ Object
- #request ⇒ Object
- #response ⇒ Object
- #return_html(body) ⇒ Object
- #return_json(body) ⇒ Object
- #return_plain(body) ⇒ Object
- #set_cookie(key, cookie) ⇒ Object
- #url(path, scheme = nil) ⇒ Object
Constructor Details
#initialize ⇒ Controller
Returns a new instance of Controller.
3 4 5 6 |
# File 'lib/freedom/core/controller.rb', line 3 def initialize @request = nil @response = nil end |
Instance Method Details
#call(action, req) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# 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) ⇒ Object
50 51 52 53 |
# 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 |
#get_cookie(key) ⇒ Object
55 56 57 |
# File 'lib/freedom/core/controller.rb', line 55 def (key) @request.[key] end |
#halt(body: nil, status: 200) ⇒ Object
74 75 76 77 78 |
# 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) ⇒ Object
45 46 47 48 |
# 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 = {}) ⇒ Object
5 6 7 |
# File 'lib/freedom/markdown.rb', line 5 def markdown(md, extension = {}) return_html(markdown_to_html(md, extension)) end |
#markdown_to_html(md, extension = {}) ⇒ Object
9 10 11 12 |
# 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 ⇒ Object
26 27 28 |
# File 'lib/freedom/core/controller.rb', line 26 def params @request.params end |
#redirect(uri) ⇒ Object
69 70 71 72 |
# File 'lib/freedom/core/controller.rb', line 69 def redirect(uri) @response.location = uri halt status: 303 end |
#request ⇒ Object
18 19 20 |
# File 'lib/freedom/core/controller.rb', line 18 def request @request end |
#response ⇒ Object
22 23 24 |
# File 'lib/freedom/core/controller.rb', line 22 def response @response end |
#return_html(body) ⇒ Object
35 36 37 38 |
# 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) ⇒ Object
40 41 42 43 |
# File 'lib/freedom/core/controller.rb', line 40 def return_json(body) @response.content_type = "application/json" halt(body: body) end |
#return_plain(body) ⇒ Object
30 31 32 33 |
# File 'lib/freedom/core/controller.rb', line 30 def return_plain(body) @response.content_type = "text/plain;charset=#{Config.encoding}" halt(body: body) end |
#set_cookie(key, cookie) ⇒ Object
59 60 61 62 |
# File 'lib/freedom/core/controller.rb', line 59 def (key, ) hash = .is_a?(String) ? { value: } : @response.(key, hash) end |
#url(path, scheme = nil) ⇒ Object
64 65 66 67 |
# File 'lib/freedom/core/controller.rb', line 64 def url(path, scheme = nil) scheme ||= @request.scheme "#{scheme}://#{@request.domain + path}" end |