Module: RailsSunset::Controller::ClassMethods

Defined in:
lib/rails_sunset/controller.rb

Instance Method Summary collapse

Instance Method Details

#sunset(datetime, link: nil, only: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



7
8
9
# File 'lib/rails_sunset/controller.rb', line 7

def sunset_method(method, datetime, link: nil)
  sunset(datetime, link: link, only: [method])
end