【问题标题】:Twitter API - Ruby Twitter GemTwitter API - Ruby Twitter Gem
【发布时间】:2014-12-27 08:36:48
【问题描述】:

如何访问 Twitter API 返回的 Twitter::Cursor 哈希值?

我正在关注 Jumpstartlab 微博教程,通过 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::Cursor 对象中的每个“朋友对象”。我需要访问该对象中的键/值对,但我尝试的哈希访问导致未定义的方法或变量。

如果是版本问题,我使用的是 Twitter5.11.0 和 Jumpstart_auth 0.6.0。

【问题讨论】:

    标签: ruby twitter methods hash


    【解决方案1】:

    使用与本教程前面访问“关注者”对象相同的方式访问“朋友”对象,以获取关注者的屏幕名称列表。

    获取关注者的屏幕名称数组

    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
    

    我希望这会有所帮助。我也被这个问题困扰了一段时间。

    【讨论】:

    • 谢谢 StlgLy,今天晚些时候再试一下。
    • @user3346954 你有没有让这个工作到你可以获取 id 和名称而不是主题标签响应的地方?我被卡住了:\
    【解决方案2】:

    这些答案并没有帮助我得到最后一条消息(可能同时 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
    

    我对那个返回字符串不满意

    【讨论】:

      猜你喜欢
      • 2013-05-24
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2013-06-09
      • 2016-12-18
      • 1970-01-01
      • 2013-12-13
      相关资源
      最近更新 更多