2014-10-31 128 views
0

如何訪問Twitter API返回的Twitter :: Cursor哈希值?Twitter API - Ruby Twitter Gem

我遵循Jumpstartlab Microblogger教程,通過jumpstart_auth gem使用Twitter gem。

我在迭代4步1.我可以返回一個朋友用下面的代碼對象:

def friends_last_tweets 
    friends = client.friends 
    puts friends 
end 

=> Twitter::Cursor:0x00000104051928 

然而,例如帳戶,「客戶」在這種情況下,有兩個「朋友」不只是一個爲什麼它只返回一個對象?我想也許這個對象是數組或數組中的所有朋友相應的哈希值,因此使用[]來訪問,但是這會返回「未定義的Twitter :: Cursor方法」。我Twitter的:: Cursor對象上運行each,它返回兩個fixnums:

def friends_last_tweets 
    friends = client.friends 
    friends.each { |f| puts f } 
end 

=> 18908095 
108528349 

那麼想必這些數字必須代表的Twitter中的每個「朋友對象」 ::光標對象我想。我需要訪問該對象中的鍵/值對,但我嘗試的哈希訪問會導致未定義的方法或變量。

如果它的版本問題相關,我使用的是Twitter5.11.0和Jumpstart_auth 0.6.0。

回答

0

訪問「朋友」對象的方式與訪問本教程前面的「追隨者」對象的方式相同,以獲取追隨者的屏幕名稱列表。

要獲得的追隨者的陣列屏幕名稱

screen_names = @client.followers.collect {|f| @client.user(f).screen_name } 

爲了得到朋友的陣列屏幕名稱

screen_names = @client.friends.collect {|f| @client.user(f).screen_name } 

爲了得到朋友的最後鳴叫,您可以使用上面張貼的object_id,如下所示:

last_tweet = @client.user(object_id).status.tweet 

我希望這有助於。我在這個問題上也陷入了一段時間。

+0

感謝StlgLy,今天晚些時候會再試一次。 – jbk 2014-11-14 07:01:57

+0

@ user3346954你有沒有得到這個工作,你可以在哪裏得到的ID和名稱,而不是標籤的答覆?我卡住了:\ – marriedjane875 2015-04-20 04:09:43

1

這些問題的答案並沒有幫助我得到的最後消息(也許在此期間改變了API),這就是如何我終於做到了:

def everyones_last_tweet 
     puts "\n\n here are the latest tweets of your friends:" 
     friends = @client.friends.collect { |f| @client.user(f) } 
     friends.each do |friend| 
     puts "\n\n#{friend.screen_name} wrote: \n\t #{friend.status.text}" 
     end 
    return "" 
end 

我不開心與返回的字符串,雖然

相關問題