Class: Freedom::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/freedom/markdown.rb,
lib/freedom/core/controller.rb

Instance Method Summary collapse

Constructor Details

#initializeController

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


55
56
57
# File 'lib/freedom/core/controller.rb', line 55

def get_cookie(key)
  @request.cookies[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

#paramsObject



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

#requestObject



18
19
20
# File 'lib/freedom/core/controller.rb', line 18

def request
  @request
end

#responseObject



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


59
60
61
62
# File 'lib/freedom/core/controller.rb', line 59

def set_cookie(key, cookie)
  hash = cookie.is_a?(String) ? { value: cookie} : cookie
  @response.set_cookie(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