Class: Vundabar::BaseController

Inherits:
Object
  • Object
show all
Defined in:
lib/vundabar/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ BaseController

Returns a new instance of BaseController.



5
6
7
# File 'lib/vundabar/controller.rb', line 5

def initialize(request)
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'lib/vundabar/controller.rb', line 4

def request
  @request
end

Instance Method Details

#controller_nameObject



68
69
70
71
# File 'lib/vundabar/controller.rb', line 68

def controller_name
  klass = self.class.to_s.gsub(/Controller$/, "")
  klass.to_snake_case
end

#get_responseObject



29
30
31
# File 'lib/vundabar/controller.rb', line 29

def get_response
  @response
end

#invalid_route(endpoints) ⇒ Object



33
34
35
36
37
# File 'lib/vundabar/controller.rb', line 33

def invalid_route(endpoints)
  template = File.join(APP_ROOT, "public", "invalid_route.html.erb")
  locals = { path: request.path_info, endpoints: endpoints }
  Tilt::ERBTemplate.new(template).render(self, locals)
end

#paramsObject



9
10
11
# File 'lib/vundabar/controller.rb', line 9

def params
  request.params
end

#prepare_view_template(view_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vundabar/controller.rb', line 47

def prepare_view_template(view_name)
  layout_file = File.join(
    APP_ROOT,
    "app",
    "views",
    "layouts",
    "application.html.erb"
  )
  layout_template = Tilt::ERBTemplate.new(layout_file)
  view = "#{view_name}.html.erb"
  view_file = File.join(
    APP_ROOT,
    "app",
    "views",
    controller_name,
    view
  )
  view_template = Tilt::ERBTemplate.new(view_file)
  [layout_template, view_template]
end

#process_hash(hash) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/vundabar/controller.rb', line 73

def process_hash(hash)
  status = hash.fetch(:status, 200)
  body = hash[:json].to_hsh
  suuplied_header = hash.fetch(:headers, {})
  headers = suuplied_header.merge!("Content-Type" => "application/json")
  if hash.key? :json
    [:headers, :json, :status].each {|key| hash.delete(key) }
    response(body.merge!(hash).to_json, status, headers)
  end
end

#redirect_to(address, status: 301) ⇒ Object



13
14
15
# File 'lib/vundabar/controller.rb', line 13

def redirect_to(address, status: 301)
  response([], status, "Location" => address)
end

#render(*args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/vundabar/controller.rb', line 17

def render(*args)
  if args[0].instance_of? Hash
    return process_hash(args[0])
  else
    response(render_template(*args))
  end
end

#render_template(view_name, locals = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/vundabar/controller.rb', line 39

def render_template(view_name, locals = {})
  layout_template, view_template = prepare_view_template(view_name)
  title = view_name.to_s.tr("_", " ").capitalize
  layout_template.render(self, title: title) do
    view_template.render(self, locals)
  end
end

#response(body, status = 200, header = {}) ⇒ Object



25
26
27
# File 'lib/vundabar/controller.rb', line 25

def response(body, status = 200, header = {})
  @response = Rack::Response.new(body, status, header)
end