您好我试图缩短它,但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但没有运气。我想应该有办法限制重复。
答案 0 :(得分:1)
您可以将此重新计算如下,
在lib中创建另一个模块callback_helper.rb
module CallbackHelper
[:gmail, :googlecalendar, :yammer].each do |callback_method|
define_method("#{callback_method}") do
unless params[:error].present?
code = current_user.tokens.for(callback_method).create(:hash_key => params[:code], :hash_type => "code")
response = "API::#{callback_method.to_s.capitalize}".constantize.new.generate_tokens(params[:code])
if response['error'].present?
current_user.tokens.for(callback_method).using(:code).destroy_all
redirect_to network_path(current_user.network), alert: "Authentication failed. Invalid request."
else
access_token = current_user.tokens.for(callback_method).create(:hash_key => response['access_token']['token'], :hash_type => "access_token", :primary => true)
Resque.enqueue("Jobs::#{callback_method.to_s.capitalize}::Today".constantize, current_user.id)
redirect_to network_path(current_user.network), notice: "#{callback_method} Access granted."
end
else
redirect_to network_path(current_user.network), alert: "#{callback_method} Access denied."
end
end
end
end
将此包含在您的回调控制器中
include CallbackHelper
现在您的控制器可以访问您的模块定义的所有方法。
确保自动加载新模块。希望这可以帮助。