Class: Noodles::Http::Controller

Inherits:
Object
  • Object
show all
Includes:
Sessionable
Defined in:
lib/noodles/http/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sessionable

#session

Constructor Details

#initialize(env) ⇒ Controller

Returns a new instance of Controller.



14
15
16
17
18
19
# File 'lib/noodles/http/controller.rb', line 14

def initialize(env)
  @env = env
  @routing_params = {}
  @request = Rack::Request.new(env)
  @response = Rack::Response.new([], 200, {'Content-Type' => 'text/html'})
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



12
13
14
# File 'lib/noodles/http/controller.rb', line 12

def env
  @env
end

#requestObject (readonly)

Returns the value of attribute request.



12
13
14
# File 'lib/noodles/http/controller.rb', line 12

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/noodles/http/controller.rb', line 12

def response
  @response
end

Class Method Details

.action(action, routing_params) ⇒ Object



57
58
59
# File 'lib/noodles/http/controller.rb', line 57

def self.action(action, routing_params)
  proc { |e| self.new(e).dispatch(action, routing_params) }
end

.view(view) ⇒ Object



67
68
69
# File 'lib/noodles/http/controller.rb', line 67

def self.view(view)
  @@view = view
end

Instance Method Details

#dispatch(action, routing_params) ⇒ Object



61
62
63
64
65
# File 'lib/noodles/http/controller.rb', line 61

def dispatch(action, routing_params)
  @routing_params = routing_params
  self.send(action)
  @response.finish
end

#html(template_name) ⇒ Object



26
27
28
# File 'lib/noodles/http/controller.rb', line 26

def html(template_name)
  @response.body = [read_file(template_name, :html)]
end

#json(jsonized_response) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/noodles/http/controller.rb', line 30

def json(jsonized_response)
  @response['Content-Type'] = 'application/json'
  unless jsonized_response.is_a? String
    jsonized_response = jsonized_response.to_json
  end
  @response.body = [jsonized_response]
end

#paramsObject



45
46
47
# File 'lib/noodles/http/controller.rb', line 45

def params
  request.params.merge @routing_params
end

#redirect(redirect_path, status = 302) ⇒ Object Also known as: redirect_to



49
50
51
52
53
# File 'lib/noodles/http/controller.rb', line 49

def redirect(redirect_path, status=302)
  @response.body = []
  @response['Location'] = redirect_path
  @response.status = status
end

#text(textual_response) ⇒ Object



21
22
23
24
# File 'lib/noodles/http/controller.rb', line 21

def text(textual_response)
  @response['Content-Type'] = 'text/plain'
  @response.body = [textual_response]
end

#view_clazzObject



71
72
73
74
75
76
77
# File 'lib/noodles/http/controller.rb', line 71

def view_clazz
  if defined? @@view
    @@view
  else
    View
  end
end