Skip to content

Instantly share code, notes, and snippets.

@yhara
Created February 23, 2012 02:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhara/1889303 to your computer and use it in GitHub Desktop.
Save yhara/1889303 to your computer and use it in GitHub Desktop.
Ruby Twitter Gem 1.1.1 簡易リファレンス
Ruby Twitter Gemの簡易リファレンス(というかメモ)です。内容はバージョン1.1.1に準拠しています。
{{toc_here}}
! インストール
$ gem install twitter -v=1.1.1
(環境によっては sudo gem ~)
! 概要
* 公式サイト:http://twitter.rubyforge.org/
* ドキュメント:http://rdoc.info/gems/twitter
使い方はこんな感じ。
<<<
require 'rubygems' # ←Ruby 1.9では不要
require 'twitter'
require 'pp'
pp Twitter.user("shuzo_matsuoka")
>>>
:ちなみに:RailsでTwitterアプリを作る場合は、これじゃなくて、twitter-authというプラグインを使うのがお手軽です。
* http://github.com/mbleigh/twitter-auth
* http://kray.jp/blog/twitter_service_in_1hours/
! ログインが要らない操作
public_timelineの取得
Twitter.public_timeline
あるユーザの情報を取得
Twitter.user("yhara")
あるユーザのフォロワーを取得
Twitter.follower_ids("yhara")
あるユーザのフォローを取得
Twitter.friend_ids("yhara")
あるユーザのタイムラインを取得 (※発言が非公開のときは要ログイン)
Twitter.user_timeline("yhara")
あるリストのタイムラインを取得
Twitter.list_timeline("リストの作者名", "リスト名")
などなど…
! ログインが必要な操作
プログラムからTwitterにログインするには、アプリ登録が必要です。
!! ステップ1:アプリをTwitterに登録する
# 公式サイトを用意する{{fn "無理なら、自分のTwitterページなど、最低限作者と連絡が取れるもの"}}
# http://twitter.com/apps/new を開く
## アプリ名を入力
## 概要を10文字以上入力
## サイトのURLを入力
##「クライアントアプリケーション」を選択。「Read & Write」「Read only」のいずれかを選択(投稿を行う場合はRead&Write)。
# 登録後、「Consumer key」と「Consumer secret」をメモる (他人に見せないように) {{fn "他人に知られると、あなたのアプリ名で迷惑行為を行われる恐れなどがある(という理解でいいのかな?)。万一漏れてしまった場合は、この画面からkey/secretをリセットすることができる"}}
ここまでで、アプリの登録は終了です。お疲れさまでした。
!! ステップ2:access tokenを取得する
アプリが登録できたら、次はaccess tokenの取得です。access tokenは、「このアプリ専用のログインパスワード」みたいなものです。アプリごとにパスワードが違うので、セキュリティ的により安心というわけです{{fn "ユーザの判断でアプリ連携を削除したり"}}。
Twitterクライアントを作る場合は、プログラムを書いてaccess tokenを取得するわけですけど、botとか作る場合はログインするのは自分だけですし、
何回もやるのは面倒なので、https://github.com/jugyo/get-twitter-oauth-token みたいにコマンド一発でできるやつとか、簡略化してる人がいます。
筆者も[[atoken4me|https://github.com/yhara/atoken4me]]というWebベースのを作って使っています。使い方は以下のような感じです。
# http://atoken4me.heroku.com/ を開きます
# ステップ1で手に入れたConsumer keyとConsumer secretを入力します
# 表示されたURLを新しいウィンドウで開きます
# 「承認しますか?」というおなじみの画面が出るので、OKするとPINナンバーがもらえます
# 元の画面に戻って、PINナンバーを入力すると、access tokenが表示されます。
!! ステップ3:atokenを使ってログインする
<<<
require 'twitter'
require 'pp'
# ログイン
Twitter.configure do |config|
config.consumer_key = 'アプリのatoken'
config.consumer_secret = 'アプリのsecret'
config.oauth_token = 'ユーザのatoken'
config.oauth_token_secret = 'ユーザのsecret'
end
#自分のタイムラインの取得
pp Twitter.home_timeline
#自分宛てのReplyの取得
# pp Twitter.mentions
#自分宛てのDMの取得
# pp Twitter.direct_messages
#発言する
# Twitter.update("テストです。")
>>>
! メソッド一覧
以下、間違ってるかも知れないのでうまく動かなかったら http://rdoc.info/gems/twitter/1.1.1/Twitter/Client を参照してください。
自分
* Twitter.home_timeline(query={})
** 注:公式RTを含む
* Twitter.friends_timeline(query={})
** 注:公式RTを含まない
** [options] since_id, max_id, count, page, since
* Twitter.mentions(query={})
ユーザ
* Twitter.user(id, query={})
* Twitter.users(*ids_or_usernames)
* Twitter.followers(query={})
* Twitter.friends(query={})
* Twitter.user_timeline(query={})
** [options] id, user_id, screen_name, since_id, max_id, page, since, count
発言の情報
* Twitter.status(id)
* Twitter.status_destroy(id)
リツイート(RT)
* Twitter.retweets(id, query={})
** [options] :count
* Twitter.retweeted_by_me(query={})
* Twitter.retweeted_to_me(query={})
* Twitter.retweets_of_me(query={})
* Twitter.retweeters_of(id, options={})
** [options] :count, :page, :ids_only
* Twitter.retweet(id)
ダイレクトメッセージ(DM)
* Twitter.direct_messages(query={})
** [options] :since, :since_id, :page
* Twitter.direct_messages_sent(query={})
* Twitter.direct_message_create(user, text)
* Twitter.direct_message_destroy(id)
フォロー関係
* Twitter.friendship_create(id, follow=false)
* Twitter.friendship_destroy(id)
* Twitter.friendship_exists?(a, b)
* Twitter.friendship_show(query)
* Twitter.friend_ids(query={})
** [options] :id, :user_id, :screen_name
* Twitter.follower_ids(query={})
お気に入り(fav)
* Twitter.favorites(query={})
** [options] :id, :page
* Twitter.favorite_create(id)
* Twitter.favorite_destroy(id)
プロフィール
* Twitter.update_profile_colors(colors={})
* Twitter.update_profile_image(file)
** 注: fileはreadメソッドとpathメソッドを持つオブジェクト(FileとかTempfileとか)
* Twitter.update_profile_background(file, tile = false)
* Twitter.update_profile(body={})
** [options] :name, :email, :url, :location, :description
リスト関係
* Twitter.list_create(list_owner_username, options)
* Twitter.list_update(list_owner_username, slug, options)
* Twitter.list_delete(list_owner_username, slug)
* Twitter.list(list_owner_username, slug)
* Twitter.list_timeline(list_owner_username, slug, query = {})
** オプション::per_page, :page
* Twitter.memberships(list_owner_username, query={})
* Twitter.subscriptions(list_owner_username, query = {})
* Twitter.list_members(list_owner_username, slug, query = {})
* Twitter.list_add_member(list_owner_username, slug, new_id)
* Twitter.list_remove_member(list_owner_username, slug, id)
* Twitter.is_list_member?(list_owner_username, slug, id)
* Twitter.list_subscribers(list_owner_username, slug)
* Twitter.list_subscribe(list_owner_username, slug)
* Twitter.list_unsubscribe(list_owner_username, slug)
ブロック
* Twitter.block(id)
* Twitter.unblock(id)
* Twitter.blocked_ids
* Twitter.blocking(options={})
保存された検索
* Twitter.saved_searches
* Twitter.saved_search(id)
* Twitter.saved_search_create(query)
* Twitter.saved_search_destroy(id)
その他
* Twitter.user_search(q, query={})
* Twitter.verify_credentials
* Twitter.update_delivery_device(device)
** [device] sms, im, none
* Twitter.rate_limit_status
* Twitter.enable_notifications(id)
* Twitter.disable_notifications(id)
* Twitter.report_spam(options)
* Twitter.help
@yhara
Copy link
Author

yhara commented Feb 23, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment