Rails + Twitter
昨日から、Twitter周りを作ってみてるんだけど。
OAuth
Tiwtterの情報を取得するには、OAuth認証が必要なんだけど、
この部分は以下のGemで簡単にできました。
github.com
トレンドの取得
使ったのは↑のGEMなんだけど、よくわからん。
↓「@return [Array
# Returns the top 50 trending topics for a specific WOEID # # @see https://dev.twitter.com/rest/reference/get/trends/place # @rate_limited Yes # @authentication Requires user context # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. # @param id [Integer] The {https://developer.yahoo.com/geo/geoplanet Yahoo! Where On Earth ID} of the location to return trending information for. WOEIDs can be retrieved by calling {Twitter::REST::Trends#trends_available}. Global information is available by using 1 as the WOEID. # @param options [Hash] A customizable set of options. # @option options [String] :exclude Setting this equal to 'hashtags' will remove all hashtags from the trends list. # @return [Array<Twitter::Trend>] def trends(id = 1, options = {}) options = options.dup options[:id] = id response = perform_get('/1.1/trends/place.json', options).first Twitter::TrendResults.new(response) end
TrendResults を見てみると「/1.1/trends/place.json」の戻り値が入ったattrsを、@correctionに入れてんだけど、インスタンス変数だから外部から参照できん。
attr_readerが設定されているのはattrsだけなので、自前で@correctionを作る作業をしないといけないっぽい。うーむ。。
# Initializes a new TrendResults object # # @param attrs [Hash] # @return [Twitter::TrendResults] def initialize(attrs = {}) @attrs = attrs @collection = @attrs.fetch(:trends, []).collect do |trend| Trend.new(trend) end end