Files
danbooru/app/logical/application_responder.rb
evazion 65ab7f1eb5 API: fix regression in expires_in URL parameter.
Fix `https://danbooru.donmai.us/artists.json?expires_in=300` failing with
an `'300' is not a valid duration` error. This call pattern is used by the
Translate Pixiv Tags userscript.

Caused by a5ed8c72, which changed the `age:N` metatag to require time
units, but this inadvertently changed the `expires_in` parameter to
require them too.

Using `expires_in` without time units is deprecated and will be removed
in the future.
2021-11-04 03:51:39 -05:00

30 lines
967 B
Ruby

# Hooks into `respond_with` to add some custom behavior, including support for
# the `expires_in`, `expiry`, and `only` params.
#
# @see https://github.com/plataformatec/responders
# @see https://github.com/plataformatec/responders/blob/master/lib/action_controller/responder.rb
class ApplicationResponder < ActionController::Responder
# this is called by respond_with for non-html, non-js responses.
def to_format
params = request.params
if get?
if params["expires_in"]
expires_in = params["expires_in"]
expires_in += "seconds" if expires_in =~ /\d+\z/
controller.expires_in(DurationParser.parse(expires_in))
elsif request.params["expiry"]
controller.expires_in(params["expiry"].to_i.days)
end
end
if format == :xml
options[:root] ||= resource.table_name.dasherize if resource.respond_to?(:table_name)
end
options[:only] ||= params["only"] if params["only"]
super
end
end