如何在模型方法和Rails 3中的控制器中使用DRY重复代码?

时间:2011-05-02 04:06:10

标签: ruby-on-rails model dry linkedin

我有以下方法 对于单个模型,可能有更多。我也可能在帮助器中有一些重复的代码。我怎么能让它变干?

25   def full_name
 26     client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
 27     client.authorize_from_access(self.atoken, self.asecret)
 28     client.profile(id => self.uid)
 29     client.profile.first_name + " " + client.profile.last_name
 30   end
 31 
 32   def image_url
 33     client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
 34     client.authorize_from_access(self.atoken, self.asecret)
 35     client.profile(id => self.uid)
 36     client.profile(:fields => "picture-url").picture_url
 37   end

每次我需要在API上进行方法调用时,我会重复实例化客户端并访问大部分配置文件ID的代码。这只是改变的API。

当我还需要调用控制器(不同型号?)时会发生什么?

29     if @review.save
 30       flash[:notice] = "Successfully created review."
 31       # check if allowed to post to LinkedIn and post
 32       if @review.post_linkedin?
 33         client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
 34         client.authorize_from_access(current_user.atoken, current_user.asecret)
 35         debugger 
 36         client.update_network("has shared expertise on <a")
 37       end

我怎么能让它更干燥?

3 个答案:

答案 0 :(得分:1)

我同意@thekindofme,但我会添加一些缓存,因此您不必每次都进行LinkedIn API调用:

def linkedin_profile
  @linkedin_profile ||= set_linkedin_profile
end

def set_linkedin_profile
  client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
  client.authorize_from_access(self.atoken, self.asecret)
  client.profile(id => self.uid)
  client.profile
end

def full_name
  linkedin_profile.first_name + " " + linkedin_profile.last_name
end

def image_url
  linkedin_profile(:fields => "picture-url").picture_url
end

答案 1 :(得分:0)

一般规则是:提取重复的代码,将其放入可重用的类/方法中。

以上代码。你可以这样做:

def get_client_profile
      client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
      client.authorize_from_access(self.atoken, self.asecret)
      client.profile(id => self.uid)
      client.profile
end

def full_name
  p=get_client_profile
  p.first_name + " " + p.last_name
end

...等

在这种情况下,您可以在模型中使用get_client_profile(可能在模型客户端?)。因为'它属于他们'。

答案 2 :(得分:0)

我会将以下方法添加到您的用户类:

class User
  def linkedin_client
    @linkedin_client || = get_linkedin_client 
  end

  def linkedin_profile
    linkedin_client.profile(id => self.uid)
    linkedin_client.profile
  end

  def full_name
    linkedin_profile.first_name + " " + user.linkedin_profile.last_name
  end

  def image_url
    linkedin_profile(:fields => "picture-url").picture_url
  end


  private

  def get_linkedin_client
    client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
    client.authorize_from_access(atoken, asecret)   
    client     
  end
end

在控制器内你会写:

if @review.save
  flash[:notice] = "Successfully created review."
  # check if allowed to post to LinkedIn and post
  if @review.post_linkedin?
    current_user.linkedin_client.update_network("has shared expertise on <a")
  end
end

希望这有帮助。