Module: Ruil::Controller

Included in:
Authorizer
Defined in:
lib/ruil/controller.rb

Overview

The module Controller allow us to a group the creation of resources in classes.

Usage

class MyController
  include Ruil::Controller

  resource 'GET', '/mycontroller/:your_name' do |request|
    ok :text, 'hello ' + request[:path_info_params][:your_name]
  end
end

Constant Summary collapse

@@content_types =
{
  :html  => 'text/html',
  :xhtml => 'application/xhtml+xml',
  :text  => 'plain/text',
  :json  => 'application/json'
}

Instance Method Summary collapse

Instance Method Details

#ok(type, body, headers = {}) ⇒ Object

Respond OK

Parameters:

  • body (String)

    the response body



44
45
46
47
48
49
50
51
52
53
# File 'lib/ruil/controller.rb', line 44

def ok(type, body, headers = {})
  # Apply custom skins.
  Ruil::Skin.call body if [:html, :xhtml].include? type
  # Setup headers.
  headers = {
    'ContentType' => @@content_types[type] || 'plain/text'
  }.merge(headers)
  # Respond.
  [200, headers, [body]]
end

#redirect(request, url) ⇒ Object

Redirect the request to other URL.



36
37
38
39
# File 'lib/ruil/controller.rb', line 36

def redirect(request, url)
  headers = {'Location'=> url + "?redirected_from=" + request.path_info}
  [302, headers, []]
end

#resource(methods, pattern, acl = false, &block) ⇒ Object

The method #resource creates a new Resource to answer requests matching the specific methods and url pattern.

Parameters:

  • methods
  • pattern


31
32
33
# File 'lib/ruil/controller.rb', line 31

def resource(methods, pattern, acl = false, &block)
  Ruil::Resource.new(methods, pattern, acl, &block)
end