class Restfulness::Resource

Attributes

request[R]
response[R]

Public Class Methods

new(request, response) click to toggle source
# File lib/restfulness/resource.rb, line 9
def initialize(request, response)
  @request  = request
  @response = response
end

Private Class Methods

supported_methods() click to toggle source
# File lib/restfulness/resource.rb, line 109
def supported_methods
  @_actions ||= (instance_methods & [:get, :put, :post, :delete, :head, :patch, :options])
end

Public Instance Methods

allowed?() click to toggle source
# File lib/restfulness/resource.rb, line 42
def allowed?
  true
end
authorized?() click to toggle source
# File lib/restfulness/resource.rb, line 38
def authorized?
  true
end
call() click to toggle source
# 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_callbacks() click to toggle source
# 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
etag() click to toggle source
# File lib/restfulness/resource.rb, line 50
def etag
  nil
end
exists?() click to toggle source
# File lib/restfulness/resource.rb, line 34
def exists?
  true
end
last_modified() click to toggle source
# File lib/restfulness/resource.rb, line 46
def last_modified
  nil
end
method_allowed?() click to toggle source

Callbacks

# File lib/restfulness/resource.rb, line 30
def method_allowed?
  self.class.supported_methods.include?(request.action)
end
options() click to toggle source

Options is the only HTTP method support by default

# 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

Protected Instance Methods

locale() click to toggle source
# File lib/restfulness/resource.rb, line 77
def locale
  request.http_accept_language.compatible_language_from(I18n.available_locales)
end
logger() click to toggle source
# File lib/restfulness/resource.rb, line 85
def logger
  Restfulness.logger
end
set_locale() click to toggle source
# File lib/restfulness/resource.rb, line 81
def set_locale
  I18n.locale = locale
end

Private Instance Methods

check_etag() click to toggle source
# File lib/restfulness/resource.rb, line 99
def check_etag
  tag = request.headers[:if_none_match]
  if tag && tag == etag.to_s
    not_modified!
  end
  response.headers['ETag'] = etag
end
check_if_modified() click to toggle source
# File lib/restfulness/resource.rb, line 91
def check_if_modified
  date = request.headers[:if_modified_since]
  if date && date == last_modified.to_s
    not_modified!
  end
  response.headers['Last-Modified'] = last_modified
end