From 22fd90eee93596d48ce82d02bfcc65e9b1c34bfb Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 8 Sep 2019 15:32:31 -0500 Subject: [PATCH] api: support the `expiry` param on all GET requests. --- app/controllers/artists_controller.rb | 1 - app/controllers/pools_controller.rb | 7 +------ app/controllers/tags_controller.rb | 2 -- app/controllers/users_controller.rb | 7 +------ app/controllers/wiki_pages_controller.rb | 4 ---- app/logical/application_responder.rb | 5 +++++ test/functional/application_controller_test.rb | 7 +++++++ 7 files changed, 14 insertions(+), 19 deletions(-) diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index afe8d3afd..fdb683d6a 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -44,7 +44,6 @@ class ArtistsController < ApplicationController end format.json do render :json => @artists.to_json(:include => [:urls]) - expires_in params[:expiry].to_i.days if params[:expiry] end end end diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index b8c7a0251..952a67c5d 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -18,12 +18,7 @@ class PoolsController < ApplicationController def index @pools = Pool.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) - respond_with(@pools) do |format| - format.json do - render json: @pools.to_json - expires_in params[:expiry].to_i.days if params[:expiry] - end - end + respond_with(@pools) end def gallery diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 4ed37c405..b60438b68 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -21,8 +21,6 @@ class TagsController < ApplicationController @tags = Tag.names_matches_with_aliases(params[:search][:name_matches]) end - expires_in params[:expiry].to_i.days if params[:expiry] - respond_with(@tags, root: "tags") end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 390d8bacc..c078eb908 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -30,12 +30,7 @@ class UsersController < ApplicationController redirect_to user_path(@user) else @users = User.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) - respond_with(@users) do |format| - format.json do - render json: @users.to_json - expires_in params[:expiry].to_i.days if params[:expiry] - end - end + respond_with(@users) end end diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index 7e4a8e80f..75f12d004 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -26,10 +26,6 @@ class WikiPagesController < ApplicationController end end end - format.json do - render json: @wiki_pages.to_json - expires_in params[:expiry].to_i.days if params[:expiry] - end end end diff --git a/app/logical/application_responder.rb b/app/logical/application_responder.rb index 2368cd3c0..502578284 100644 --- a/app/logical/application_responder.rb +++ b/app/logical/application_responder.rb @@ -3,6 +3,11 @@ class ApplicationResponder < ActionController::Responder # this is called by respond_with for non-html, non-js responses. def to_format + if get? + expiry = request.params["expiry"] + controller.expires_in expiry.to_i.days if expiry.present? + end + if format == :xml options[:root] ||= resource.table_name.dasherize if resource.respond_to?(:table_name) end diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 580e1cbc9..e56b048bd 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -192,6 +192,13 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest assert_equal(tags.first.id, response.parsed_body.first.fetch("id")) end + should "support the expiry parameter" do + get posts_path, as: :json, params: { expiry: "1" } + + assert_response :success + assert_equal("max-age=#{1.day}, private", response.headers["Cache-Control"]) + end + should "return the correct root element name for empty xml responses" do get tags_path, as: :xml