Class: Restfulness::Resource

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

Constant Summary

Constants included from Restfulness::Resources::Events

Restfulness::Resources::Events::EXCEPTION_EVENTS, Restfulness::Resources::Events::SUCCESS_EVENTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Restfulness::Resources::Authentication

#authenticate_with_http_basic

Methods included from Restfulness::Resources::Events

#error!

Constructor Details

#initialize(request, response) ⇒ Resource

Returns a new instance of Resource.


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

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

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request


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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response


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

def response
  @response
end

Class Method Details

.supported_methodsObject


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

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

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)

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

def allowed?
  true
end

#authorized?Boolean

Returns:

  • (Boolean)

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

def authorized?
  true
end

#callObject


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

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


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

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, :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


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

def etag
  nil
end

#exists?Boolean

Returns:

  • (Boolean)

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

def exists?
  true
end

#last_modifiedObject


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

def last_modified
  nil
end

#method_allowed?Boolean

Callbacks

Returns:

  • (Boolean)

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

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

#optionsObject

Options is the only HTTP method support by default


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

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