Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 重构rails代码_Ruby On Rails - Fatal编程技术网

Ruby on rails 重构rails代码

Ruby on rails 重构rails代码,ruby-on-rails,Ruby On Rails,嗨,我试着把它缩短,但是CodeClimate(代码审阅者)总是说仍然存在一些重复和复杂性 到目前为止,这是我在尝试重构它之后得到的 以下是我的应用程序的API代码: class CallbackController < ApplicationController def gmail unless params[:error].present? code = current_user.tokens.for(:gmail).create(:hash_key =>

嗨,我试着把它缩短,但是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
class CallbackControllerparams[:code],:hash_type=>code)
response=API::Gmail.new(Gmail\u回调\u索引\u url)。生成\u令牌(参数[:代码])
如果响应['error'],是否存在?
当前用户。令牌。用于(:gmail)。使用(:代码)。销毁所有
重定向到(网络路径(当前用户.network),警告:“身份验证失败。请求无效。”)
其他的
access\u token=current\u user.tokens.for(:gmail).create(:hash\u key=>response['access\u token'],:hash\u type=>“access\u token”,:primary=>true)
id\u token=当前用户.tokens.for(:gmail).create(:hash\u key=>response['id\u token'],:hash\u type=>“id\u token”)
刷新令牌=当前用户.tokens.for(:gmail).create(:hash\u key=>response['refresh\u token'],:hash\u type=>“refresh\u token”)
Resque.enqueue(Jobs::Gmail::Today,current_user.id)
重定向到网络路径(当前用户.network),注意:“GMail访问权已授予。”
结束
其他的
重定向到网络路径(当前用户.network),警告:“GMail访问被拒绝。”
结束
结束
谷歌日历
除非参数[:错误]。是否存在?
代码=当前用户。令牌。for(:googlecalendar)。创建(:hash\u key=>params[:code],:hash\u type=>code)
response=API::Googlecalendar.new(Googlecalendar\u回调\u索引\u url)。生成\u令牌(参数[:代码])
如果响应['error'],是否存在?
当前用户。令牌。用于(:谷歌日历)。使用(:代码)。销毁所有
重定向到(网络路径(当前用户.network),警告:“身份验证失败。请求无效。”)
其他的
access\u token=current\u user.tokens.for(:googlecalendar).create(:hash\u key=>response['access\u token'],:hash\u type=>“access\u token”,:primary=>true)
id\u token=当前用户.tokens.for(:googlecalendar).create(:hash\u key=>response['id\u token'],:hash\u type=>“id\u token”)
刷新令牌=当前用户.tokens.for(:googlecalendar).create(:hash\u key=>response['refresh\u token'],:hash\u type=>“refresh\u token”)
#Resque.enqueue(作业::谷歌日历::今天,当前用户id)
重定向到网络路径(当前用户.network),注意:“Google日历访问权已授予。”
结束
其他的
重定向到网络路径(当前用户.network),警告:“谷歌日历访问被拒绝。”
结束
结束
def yammer
除非参数[:错误]。是否存在?
code=当前用户.tokens.for(:yammer).create(:hash\u key=>params[:code],:hash\u type=>“code”)
response=API::Yammer.new.generate_令牌(params[:code])
如果响应['error'],是否存在?
当前用户.tokens.for(:yammer).使用(:code).销毁所有
重定向到网络路径(当前用户.network),警告:“身份验证失败。请求无效。”
其他的
access\u token=current\u user.tokens.for(:yammer).create(:hash\u key=>response['access\u token']['token'],:hash\u type=>“access\u token”,:primary=>true)
Resque.enqueue(作业::Yammer::最新的、当前的_user.id)
重定向到网络路径(当前用户.network),注意:“Yammer访问权已授予。”
结束
其他的
重定向到网络路径(当前用户.network),警告:“Yammer访问被拒绝。”
结束
结束
结束
任何关于如何使其更短和逻辑上不重复的变通方法、提示和建议都将受到赞赏

更新:


试图在过滤器前放置一个过滤器,但没有成功。我想应该有一种方法来限制重复。

您可以按如下方式重新考虑这一点

在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
现在,控制器可以访问由模块定义的所有方法


确保新模块已自动加载。希望这有帮助

您可以按如下方式重新考虑这一点

在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
现在,控制器可以访问由模块定义的所有方法


确保新模块已自动加载。希望这有帮助

谢谢你的方法。非常感谢:)谢谢你的方法。非常感谢:)