【发布时间】:2011-05-02 04:06:09
【问题描述】:
我有以下方法 对于单个模型,并且可能有更多。我也可能在助手中有一些重复的代码。我怎样才能让它变干?
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
我怎样才能让它更干?
【问题讨论】:
标签: ruby-on-rails model dry linkedin