Class: Noodles::Http::Controller

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Controller

Returns a new instance of Controller.



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

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

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



10
11
12
# File 'lib/noodles/http/controller.rb', line 10

def env
  @env
end

#requestObject (readonly)

Returns the value of attribute request.



10
11
12
# File 'lib/noodles/http/controller.rb', line 10

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/noodles/http/controller.rb', line 10

def response
  @response
end

#sessionObject (readonly)

Returns the value of attribute session.



10
11
12
# File 'lib/noodles/http/controller.rb', line 10

def session
  @session
end

Class Method Details

.action(action, routing_params) ⇒ Object



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

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

.view(view) ⇒ Object



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

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

Instance Method Details

#dispatch(action, routing_params) ⇒ Object



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

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

#html(template_name) ⇒ Object



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

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

#json(jsonized_response) ⇒ Object



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

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



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

def params
  request.params.merge @routing_params
end

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



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

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

#text(textual_response) ⇒ Object



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

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

#view_clazzObject



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

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