简体   繁体   English

Twitter API-Ruby Twitter宝石

[英]Twitter API - Ruby Twitter Gem

How can I access Twitter::Cursor hash values returned by the Twitter API? 如何访问Twitter API返回的Twitter :: Cursor哈希值?

I am following the Jumpstartlab Microblogger tutorial for using the Twitter gem via the jumpstart_auth gem . 我正在关注Jumpstartlab Microblogger教程,该教程通过jumpstart_auth gem使用Twitter gem

I am on iteration 4 step 1. I can return a friends object with the following code: 我在迭代4步骤1上。我可以使用以下代码返回一个friends对象:

def friends_last_tweets
  friends = client.friends
  puts friends
end

=>    Twitter::Cursor:0x00000104051928

However, the example account, 'client' in this case, has two 'friends' not just one so why does it only return one object? 但是,示例帐户“客户”在这种情况下有两个“朋友”而不仅仅是一个,那么为什么它只返回一个对象? I thought maybe that object is the array or arrays with all of the friends accordingly in hash values within, thus use [] to access, but this returns "undefined method for Twitter::Cursor". 我以为该对象可能是其中所有朋友都在哈希值内的数组,因此使用[]进行访问,但这将返回“ Twitter :: Cursor的未定义方法”。 I run each on the Twitter::Cursor object and it returns two fixnums: 我在Twitter :: Cursor对象上运行each对象,它返回两个fixnums:

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

=> 18908095
108528349

So surely these numbers must represent each 'friend object' within the Twitter::Cursor object me thinks. 因此,可以肯定的是,这些数字必须代表我认为的Twitter :: Cursor对象中的每个“朋友对象”。 I need to access the key/value pairs within that object, yet my attempted hash accessing results in undefined method or variable. 我需要访问该对象内的键/值对,但是尝试进行哈希访问会导致未定义的方法或变量。

In case it's version issue related, I'm using Twitter5.11.0 and Jumpstart_auth 0.6.0. 如果与版本问题相关,我将使用Twitter5.11.0和Jumpstart_auth 0.6.0。

those answers didn't helped me to get the last message (maybe the API changed in the meantime), that's how I finally did it: 这些答案并没有帮助我获得最后一条消息(也许与此同时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

I'm not happy with that return string though 我对那个返回字符串不满意

Access the 'friends' object in the same way you accessed the 'followers' object earlier in the tutorial in order to get a list of your followers' screen names. 以与本教程前面访问“关注者”对象相同的方式访问“朋友”对象,以获取关注者的屏幕名称列表。

To get an array of followers' screen names : 要获取关注者的屏幕名称数组

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

To get an array of friends' screen names : 要获得朋友的屏幕名称数组

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

To get the last tweet of a friend, you can use the object_id's you posted above, as: 要获取朋友的最后一条推文,可以使用上面发布的object_id,如下所示:

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

I hope this helps. 我希望这有帮助。 I was caught on this issue for a while too. 我也被这个问题困扰了一段时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM