twitter4r cheet sheet

twitter4rのチートシートです。
追加したほうがいいことがあったらコメントください。

twitter4rについて

Twitter APIRubyラッパです。gemで提供されています。

作者 Susan Potter http://susanpotter.net/

リポジトリ Rubyforge http://twitter4r.rubyforge.org/

Google User Group http://groups.google.com/group/twitter4r-users

インストール

$ sudo gem install twitter4r

クライアント生成

 require 'time'
 require 'rubygems'
 require 'twitter'
 client = Twitter::Client.new(:login => 'YourAccount', :password => 'YourPassword'))

その他、コードに直接かかずにfrom_configメソッドなどで認証することもできます。

require 'time'
require 'rubygems'
require 'twitter'

の部分はおまじないだと思ってOK

API制限を取得

 limit = client.account_info
 limit.hourly_limit
 => 150 (とか)

その他の情報は、下記、「API制限情報として取得可能な情報」を参照

ユーザ情報等を取得

自分をフォロー(followers)しているアカウントを取得
 friends = client.my(:followers)
自分がフォローしているアカウントを取得
 friends = client.my(:friends)


取得個数を指定する。

 friends = client.my(:friends, :count=>10)


to_hashする。

 friends = client.my(:friends).map { |f| f.to_hash }

タイムライン取得

自分のタイムラインを取得する(認証必要)
 tl = client.timeline_for(:me)


取得個数を指定する。

 tl = client.timeline_for(:me, :count=>10)


to_hashする。

 tl = client.timeline_for(:me).map {|s| s.to_hash }
友達のタイムラインを取得
 tl = client.timeline_for(:friends)


順に処理する。

 tl = client.timeline_for(:friends) {|status| statusに対する処理(下記、「ステータスから取得できる情報」参照) }
 


ステータスを処理する。

 tl = client.timeline_for(:friends) {|status| p status.text }


ステータスからユーザ情報を取得する。

 tl = client.timeline_for(:friends) {|status| p status.user.name }
特定のユーザのタイムラインを取得(認証不要)
 tl = Twitter::Client.new.timeline_for(:user, :id=>"login name")


取得個数を指定する。

 tl = Twitter::Client.new.timeline_for(:user, :id=>"login name", :count=>10)

検索

たぶん自分のだけ

 client.search(:q => "バルス", :lang => 'ja')

参考 twitter4rでtwitter検索するのに思ったより苦戦 そしてtwitterの仕様

つぶやき

つぶやく
 client.status(:post, 'つぶやき内容')

詳細情報

API制限情報として取得可能な情報
 hourly_limit: 150
 remaining_hits: 136
 reset_time: Sun Nov 22 04:30:03 +0000 2009
 reset_time_in_seconds: 1258864203
ステータスから取得できる情報
 created_at: 2009-11-22 13:17:18 +09:00
 favorited: true/false
 id: 投稿ID
 in_reply_to_screen_name: 
 in_reply_to_status_id: 
 in_reply_to_user_id: 
 source: <a href="http://www.tweetdeck.com/" rel="nofollow">TweetDeck</a>(例です)
 text: "ついっと内容(日本語はエンコードされるので文字化けするかも)"
 truncated: true/false
 user: ユーザ情報(下記「ユーザ情報から取得できる情報」参照)
ユーザ情報から取得できる情報
 created_at: Thu Dec 20 04:21:50 +0000 2007
 description: "あまりにもまとまった情報がないから自分用にチートシート作り中"
 favourites_count: 少ないw
 followers_count: 少ないw
 following: true/false
 friends_count: 少ないw
 id: ユーザID
 location: "" (これなんのlocationだろ?geoじゃないよね?空なのはyourfilehost対策じゃないです。ほんとです。)
 name: yoshi
 notifications: false
 profile_background_color: 9ae4e8
 profile_background_image_url: http://s.twimg.com/a/1258674567/images/themes/theme1/bg.png
 profile_background_tile: true/false
 profile_image_url: http://a3.twimg.com/profile_images/279683789/green_1105_8_480_normal.jpg
 profile_link_color: 0000ff
 profile_sidebar_border_color: 87bc44
 profile_sidebar_fill_color: e0ff92
 profile_text_color: "000000"
 protected: false
 screen_name: yotena
 statuses_count: 653
 time_zone: Tokyo
 url: http://quuzu.jp (うちのサイトです。よかったらどうぞ。)
 utc_offset: 32400