Ruby Twitter Gem簡易リファレンス
Ruby Twitter Gemの日本語リファレンス(というかメモ)です。内容はバージョン0.9.8に準拠しています。
ログインが要らない操作
public_timelineの取得
Twitter.firehose
あるユーザの情報を取得
Twitter.user("yhara")
あるユーザのフォロワーを取得
Twitter.follower_ids("yhara")
あるユーザのフォローを取得
Twitter.friend_ids("yhara")
あるユーザのタイムラインを取得 (※発言が非公開のときは要ログイン)
Twitter.timeline("yhara")
あるリストのタイムラインを取得
Twitter.list_timeline("(リストの作者名)", "(リスト名)")
ここでは解説しませんが、検索とか位置情報とかトレンドとかもアクセスできます。
- Twitter::Search
- Twitter::Geo
- Twitter::Trends, Twitter::LocalTrends?
ログインが要る操作
昔はメールアドレスとパスワードによるログイン(Twitter::HTTPAuth)が使われていましたが、Twitterの仕様変更より、2010/8/16までで使えなくなる予定です。
代わりに、OAuthによるログインを行うようにしてください(下の「OAuthによるログイン方法」の項を参照)。
- ちなみに
- RailsでTwitterアプリを作る場合は、これじゃなくて、twitter-authというプラグインを使うのがお手軽です。
メソッド一覧
以下、
tw = Twitter::Base.new(oauth)
としてあるものとします。
自分
- tw.home_timeline(query={})
- 注:公式RTを含む
- tw.friends_timeline(query={})
- 注:公式RTを含まない
- [options] since_id, max_id, count, page, since
- tw.mentions(query={})
ユーザ
- tw.user(id, query={})
- tw.users(*ids_or_usernames)
- tw.followers(query={})
- tw.friends(query={})
- tw.user_timeline(query={})
- [options] id, user_id, screen_name, since_id, max_id, page, since, count
発言の情報
- tw.status(id)
- tw.status_destroy(id)
リツイート(RT)
- tw.retweets(id, query={})
- [options] :count
- tw.retweeted_by_me(query={})
- tw.retweeted_to_me(query={})
- tw.retweets_of_me(query={})
- tw.retweeters_of(id, options={})
- [options] :count, :page, :ids_only
- tw.retweet(id)
ダイレクトメッセージ(DM)
- tw.direct_messages(query={})
- [options] :since, :since_id, :page
- tw.direct_messages_sent(query={})
- tw.direct_message_create(user, text)
- tw.direct_message_destroy(id)
フォロー関係
- tw.friendship_create(id, follow=false)
- tw.friendship_destroy(id)
- tw.friendship_exists?(a, b)
- tw.friendship_show(query)
- tw.friend_ids(query={})
- [options] :id, :user_id, :screen_name
- tw.follower_ids(query={})
お気に入り(fav)
- tw.favorites(query={})
- [options] :id, :page
- tw.favorite_create(id)
- tw.favorite_destroy(id)
プロフィール
- tw.update_profile_colors(colors={})
- tw.update_profile_image(file)
- 注: fileはreadメソッドとpathメソッドを持つオブジェクト(FileとかTempfileとか)
- tw.update_profile_background(file, tile = false)
- tw.update_profile(body={})
- [options] :name, :email, :url, :location, :description
リスト関係
- tw.list_create(list_owner_username, options)
- tw.list_update(list_owner_username, slug, options)
- tw.list_delete(list_owner_username, slug)
- tw.list(list_owner_username, slug)
- tw.list_timeline(list_owner_username, slug, query = {})
- オプション::per_page, :page
- tw.memberships(list_owner_username, query={})
- tw.subscriptions(list_owner_username, query = {})
- tw.list_members(list_owner_username, slug, query = {})
- tw.list_add_member(list_owner_username, slug, new_id)
- tw.list_remove_member(list_owner_username, slug, id)
- tw.is_list_member?(list_owner_username, slug, id)
- tw.list_subscribers(list_owner_username, slug)
- tw.list_subscribe(list_owner_username, slug)
- tw.list_unsubscribe(list_owner_username, slug)
ブロック
- tw.block(id)
- tw.unblock(id)
- tw.blocked_ids
- tw.blocking(options={})
保存された検索
- tw.saved_searches
- tw.saved_search(id)
- tw.saved_search_create(query)
- tw.saved_search_destroy(id)
その他
- tw.user_search(q, query={})
- tw.verify_credentials
- tw.update_delivery_device(device)
- [device] sms, im, none
- tw.rate_limit_status
- tw.enable_notifications(id)
- tw.disable_notifications(id)
- tw.report_spam(options)
- tw.help
OAtuhによるログイン方法
ステップ1:アプリをTwitterに登録する
- 公式サイトを用意する*1
- http://twitter.com/apps/new を開く
- アプリ名を入力
- 概要を10文字以上入力
- サイトのURLを入力
- 「クライアントアプリケーション」を選択。「Read & Write」「Read only」のいずれかを選択(投稿を行う場合はRead&Write)。
- 登録すると、アプリのURL (http://twitter.com/oauth_clients/details/1234567 みたいな) に遷移する
- 「Consumer key」と「Consumer secret」をメモる (他人に見せないように) *2
ここまでで、アプリの登録は終了です。お疲れさまでした。
ステップ2:atokenを取得する
HTTPAuthの時代は「メールアドレスとパスワード」がキーでしたが、 OAuthの場合は「Access tokenとAccess token secret」がキーになります。長いので以下「atoken」と略します。
まず以下のスクリプトを実行して、表示されるURLをブラウザで開きます。 「承認しますか?」というおなじみの画面が出るので、OKするとPINナンバーがもらえます。 Enterを押し、PINナンバーを入力すると、atokenが表示されます。
require 'twitter'
APP_TOKEN = ['(アプリのtoken)', '(アプリのsecret)']
oauth = Twitter::OAuth.new(*APP_TOKEN)
request_token = oauth.consumer.get_request_token
puts request_token.authorize_url
puts "Press Enter when ready"
gets
puts "Input the PIN number (eg. 0123456)"
print "> "
atoken = request_token.get_access_token(:oauth_verifier => gets.chomp)
puts <<EOD
user_atoken = [
'#{atoken.token}',
'#{atoken.secret}'
]
EOD
(上の手順がめんどい、という人はこちらをどうぞ → http://atoken4me.heroku.com/ )
- アプリを自分だけで使う場合
- Twitter botなど、自分の手元でだけ動かす場合はステップ3に進んでください。
- アプリを他の人にも使わせたい場合
- Twitterクライアントなど、いろんな人に使ってもらう場合は、アプリ内からブラウザを起動するなどしてPINナンバーを利用者に取得してもらう必要があります。そこからatokenを取得する方法は上と同じです。atokenがもらえたらステップ3のようにしてログインします。
ステップ3:atokenを使ってログインする
こんな感じです。
require 'pp'
require 'twitter'
APP_TOKEN = ['(アプリのtoken)', '(アプリのsecret)']
user_atoken = [
'(ユーザのatoken)',
'(ユーザのsecret)'
]
oauth = Twitter::OAuth.new(*APP_TOKEN)
oauth.authorize_from_access(*user_atoken)
tw = Twitter::Base.new(oauth)
#自分のタイムラインの取得
pp tw.home_timeline
#自分宛てのReplyの取得
pp tw.mentions
#自分宛てのDMの取得
pp tw.direct_messages
#発言する
# tw.update("テストです。")
Twitter::Baseのメソッドは、上の「メソッド一覧」の項を参照してください。
Keyword(s):
References: