1

嗨,我试图缩短这个时间,但 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 但没有运气。我想应该有一种方法来限制重复。

4

1 回答 1

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

现在您的控制器可以访问您的模块定义的所有方法。

确保自动加载新模块。希望这可以帮助。

于 2013-08-08T09:33:09.123 回答