module RailsSunset::Controller::ClassMethods

Public Instance Methods

sunset(datetime, link: nil, only: nil) click to toggle source
# File lib/rails_sunset/controller.rb, line 11
def sunset(datetime, link: nil, only: nil)
  after_action(only: only) do |controller|
    datetime = normalize_datetime(datetime)
    link = normalize_link(link, params)
    user_agent = request.headers['User-Agent']

    # Shove a deprecation warning into the console or wherever it goes
    klass = controller.class
    method = params['action']
    ActiveSupport::Deprecation.warn("#{klass}##{method} deprecated endpoint (sunset date #{datetime.iso8601}) has been called by #{user_agent}")

    # Shove a Sunset header into HTTP Response for clients to sniff on
    # https://tools.ietf.org/html/draft-wilde-sunset-header-03
    response.headers['Sunset'] = datetime.httpdate

    if link.present?
      sunset_link_header = sprintf('<%s>; rel="sunset";', link)
      if response.headers['Link'].present?
        response.headers['Link'] += (', ' + sunset_link_header)
      else
        response.headers['Link'] = sunset_link_header
      end
    end
  end
end
sunset_method(method, datetime, link: nil) click to toggle source
# File lib/rails_sunset/controller.rb, line 7
def sunset_method(method, datetime, link: nil)
  sunset(datetime, link: link, only: [method])
end