嗨,我试图缩短这个时间,但 CodeClimate(代码审查员)总是说仍然存在一些重复和复杂性。
到目前为止,这是我在尝试重构它之后所拥有的。
这些是我的应用程序的 API 代码:
class CallbackController < ApplicationController
def gmail
unless params[:error].present?
code = current_user.tokens.for(:gmail).create(:hash_key => params[:code], :hash_type => "code")
response = API::Gmail.new(gmail_callback_index_url).generate_tokens(params[:code])
if response['error'].present?
current_user.tokens.for(:gmail).using(:code).destroy_all
redirect_to(network_path(current_user.network), alert: "Authentication failed. Invalid request.")
else
access_token = current_user.tokens.for(:gmail).create(:hash_key => response['access_token'], :hash_type => "access_token", :primary => true)
id_token = current_user.tokens.for(:gmail).create(:hash_key => response['id_token'], :hash_type => "id_token")
refresh_token = current_user.tokens.for(:gmail).create(:hash_key => response['refresh_token'], :hash_type => "refresh_token")
Resque.enqueue(Jobs::Gmail::Today, current_user.id)
redirect_to network_path(current_user.network), notice: "GMail Access granted."
end
else
redirect_to network_path(current_user.network), alert: "GMail Access denied."
end
end
def googlecalendar
unless params[:error].present?
code = current_user.tokens.for(:googlecalendar).create(:hash_key => params[:code], :hash_type => "code")
response = API::Googlecalendar.new(googlecalendar_callback_index_url).generate_tokens(params[:code])
if response['error'].present?
current_user.tokens.for(:googlecalendar).using(:code).destroy_all
redirect_to(network_path(current_user.network), alert: "Authentication failed. Invalid request.")
else
access_token = current_user.tokens.for(:googlecalendar).create(:hash_key => response['access_token'], :hash_type => "access_token", :primary => true)
id_token = current_user.tokens.for(:googlecalendar).create(:hash_key => response['id_token'], :hash_type => "id_token")
refresh_token = current_user.tokens.for(:googlecalendar).create(:hash_key => response['refresh_token'], :hash_type => "refresh_token")
#Resque.enqueue(Jobs::Googlecalendar::Today, current_user.id)
redirect_to network_path(current_user.network), notice: "Google Calendar Access granted."
end
else
redirect_to network_path(current_user.network), alert: "Google Calendar Access denied."
end
end
def yammer
unless params[:error].present?
code = current_user.tokens.for(:yammer).create(:hash_key => params[:code], :hash_type => "code")
response = API::Yammer.new.generate_tokens(params[:code])
if response['error'].present?
current_user.tokens.for(:yammer).using(:code).destroy_all
redirect_to network_path(current_user.network), alert: "Authentication failed. Invalid request."
else
access_token = current_user.tokens.for(:yammer).create(:hash_key => response['access_token']['token'], :hash_type => "access_token", :primary => true)
Resque.enqueue(Jobs::Yammer::Latest, current_user.id)
redirect_to network_path(current_user.network), notice: "Yammer Access granted."
end
else
redirect_to network_path(current_user.network), alert: "Yammer Access denied."
end
end
end
任何关于如何使其更短和逻辑上不重复的解决方法、提示和建议将不胜感激。
更新:
试图放一个 before_filter 但没有运气。我想应该有一种方法来限制重复。