Class: Restfulness::Resource

Inherits:
Object
  • Object
show all
Includes:
Restfulness::Resources::Events
Defined in:
lib/restfulness/resource.rb

Constant Summary

Constants included from Restfulness::Resources::Events

Restfulness::Resources::Events::SUPPORTED_EVENTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Restfulness::Resources::Events

#error!

Constructor Details

#initialize(request, response) ⇒ Resource

Returns a new instance of Resource.



8
9
10
11
# File 'lib/restfulness/resource.rb', line 8

def initialize(request, response)
  @request  = request
  @response = response
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/restfulness/resource.rb', line 6

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/restfulness/resource.rb', line 6

def response
  @response
end

Class Method Details

.supported_methodsObject



108
109
110
# File 'lib/restfulness/resource.rb', line 108

def supported_methods
  @_actions ||= (instance_methods & [:get, :put, :post, :delete, :head, :patch, :options])
end

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/restfulness/resource.rb', line 41

def allowed?
  true
end

#authorized?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/restfulness/resource.rb', line 37

def authorized?
  true
end

#callObject



21
22
23
24
25
# File 'lib/restfulness/resource.rb', line 21

def call
  # At some point, we might add custom callbacks here. If you really need them though,
  # you can wrap around the call method easily.
  send(request.action)
end

#check_callbacksObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/restfulness/resource.rb', line 53

def check_callbacks
  # Locale Handling
  set_locale

  # Access control
  method_not_allowed! unless method_allowed?
  unauthorized!       unless authorized?
  forbidden!          unless allowed?

  # The following callbacks only make sense for certain methods
  if [:head, :get, :put, :patch, :delete].include?(request.action)
    resource_not_found! unless exists?

    if [:get, :head].include?(request.action)
      # Resource status
      check_etag if etag
      check_if_modified if last_modified
    end
  end
end

#etagObject



49
50
51
# File 'lib/restfulness/resource.rb', line 49

def etag
  nil
end

#exists?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/restfulness/resource.rb', line 33

def exists?
  true
end

#last_modifiedObject



45
46
47
# File 'lib/restfulness/resource.rb', line 45

def last_modified
  nil
end

#method_allowed?Boolean

Callbacks

Returns:

  • (Boolean)


29
30
31
# File 'lib/restfulness/resource.rb', line 29

def method_allowed?
  self.class.supported_methods.include?(request.action)
end

#optionsObject

Options is the only HTTP method support by default



14
15
16
17
18
19
# File 'lib/restfulness/resource.rb', line 14

def options
  response.headers['Allow'] = self.class.supported_methods.map{ |m|
    m.to_s.upcase
  }.join(', ') 
  nil
end