このRailsCast on Facebook API をフォローしています。次のコードでは、ブロックをfacebook
メソッドに渡して、 の恩恵を受けることができますrescue
。
def facebook
@facebook ||= Koala::Facebook::API.new(oauth_token)
block_given? ? yield(@facebook) : @facebook
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil # or consider a custom null object
end
def friends_count
facebook { |fb| fb.get_connection("me", "friends").size }
end
ただし、ここで定義されたメソッドを呼び出す多数のメソッドがあり、それぞれの中でfacebook
繰り返したくありません。facebook {}
(構文はあまり良くありません)。
これを簡単にする方法はありますか?を呼び出す各メソッドをラップするフィルターのようなものfacebook
。