Class: Ruil::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/ruil/resource.rb

Overview

Resource objects answer requests (see the method #call) with an array following the Rack interface. If the request not match the resource, false is returned. Also, a resource includes a set of templates to delegate the action of rendering a resource.

Use example

The next example shows how to create and use a resource.

resource = Resource.new('GET', "/index")
puts resource.call(request)        # => the response to the request

Every resource is automatically regitered into Register when it is created. Thus, you may use Ruil::Register.call to call resource instead using #call directly.

resource = Resource.new('GET', "/index")
puts Ruil::Register.call(request)  # => the response using the register

Path patterns

Path patterns are strings that are matched with the request path info. Patterns may include named parameters accessibles via the hash that the PathInfoParser#=== method returns after a match check.

resource = Ruil::Resource.new(:get, "/users/:user_id/pictures/:picture_id")
env['PATH_INFO'] = "/users/23/pictures/56"
resource.call(env)    # matches are { :user_id => "232, :picture_id => "56" }
env['PATH_INFO'] = "/users/23/pictures"
resource.call(env)    # match is false

After suscessfull matches the matching pattern and the matches are stored in the request.

request[:path_info_pattern]  # => the pattern
request[:path_info_params]   # => the matches

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_methods, pattern, acl = false, &block) ⇒ Resource

Initialize a new resource.

Parameters:

  • request_methods (String, Array<String>)

    indentify the request methods that this resource responds. Valid methods are: "GET", "POST", "PUT" and "DELETE".

  • pattern (String)

    defines a pattern to match paths. Patterns may include named parameters accessibles via the hash that the PathInfoParser#=== method returns after a match check.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruil/resource.rb', line 64

def initialize(request_methods, pattern, acl = false, &block)
  # Set request methods
  @request_methods =
    case request_methods
    when String
      [request_methods]
    when Array
      request_methods
    else
      raise "Invalid value for request_methods: #{request_methods.inspect}"
    end
  # Set the path info parser
  @path_info_parser = Ruil::PathInfoParser.new(pattern)
  @resource_pattern = pattern
  # Block that generates the response
  if block_given?
    @block = lambda { |request| yield request }
  else
    raise 'Block is obligatory!'
  end
  # Set ACL flag.
  @acl = acl
  # Register it
  Ruil::Register << self
end

Instance Attribute Details

#request_methodsArray<String> (readonly)

Methods that a resource responds.

Returns:

  • (Array<String>)


51
52
53
# File 'lib/ruil/resource.rb', line 51

def request_methods
  @request_methods
end

Instance Method Details

#call(request) ⇒ Array

Respond a request

Parameters:

  • request (Rack::Request)

    a request to the resource.

Returns:

  • (Array)

    a response for the request in the format [status, headers, [body]].



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruil/resource.rb', line 94

def call(request)
  if request[:path_info_params] = ( @path_info_parser === request.path_info )
    request[:path_info_pattern] = @path_info_pattern
    if @acl
      Ruil::Authorizer.call request, @block
    else
      @block.call request
    end
  else
    false
  end
end