こんにちは、私はこれを短くしようとしましたが、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 を入れようとしましたが、うまくいきませんでした。重複を制限する方法が必要だと思います。