2

我有宝石在工作:可以从中推文并获取信息。

我可以抓住这个哈希并打印出它的 .text 部分,但我不知道如何获取 id_str 和 screen_name - 我真的需要解析出的信息。

不知道我做错了什么。

  <% Twitter.mentions_timeline.each do |mention| %>
      <%= mention.text %> <%# dies on some of em, like mention.id_str - not sure why %>
      <br /><hr />         
  <% end %>

以下是 Twitter.mentions_timeline 哈希:

[#<Twitter::Tweet:0x007fd070de24a0 




@attrs={
    :in_reply_to_status_id_str=>"265276218855809024", 
    :retweet_count=>0, 
    :contributors=>nil, 
    :truncated=>false, 
    :text=>"@doodleboots http://t.co/VJOR0MgQ", 
    :in_reply_to_user_id_str=>"119208666", 
    :geo=>nil, 
    :retweeted=>false, 
    :possibly_sensitive=>false, 
    :in_reply_to_screen_name=>"doodleboots", 
    :id_str=>"265276345704124416", 
    :entities=>{:hashtags=>[], 
        :urls=>[{:display_url=>"is.gd/S3uOyu", 
        :indices=>[13, 33], 
        :expanded_url=>"http://is.gd/S3uOyu", 
        :url=>"http://t.co/VJOR0MgQ"}], 
        :user_mentions=>[{:name=>"doodle", 
        :indices=>[0, 12], 
        :screen_name=>"doodleboots", 
        :id_str=>"119208666", 
        :id=>119208666}]}, 
        :in_reply_to_user_id=>119208666, 
        :in_reply_to_status_id=>265276218855809024, 
        :created_at=>"Mon Nov 05 02:16:24 +0000 2012", 
        :place=>nil, 
        :user=>{:profile_image_url_https=>"https://si0.twimg.com/profile_images/2808322935/d5da93a06dbd45959cb4dba0797689a5_normal.jpeg", 
        :id=>926557182, 
        :profile_background_image_url=>"http://a0.twimg.com/images/themes/theme1/bg.png", 
        :entities=>{:description=>nil}, 
        :screen_name=>"Roslyn1bdzn", 
        :profile_link_color=>"0084B4", 
        :default_profile_image=>false, 
        :created_at=>"Mon Nov 05 01:17:20 +0000 2012", 
        :contributors_enabled=>false, 
        :friends_count=>0, 
        :time_zone=>nil, 
        :favourites_count=>0, 
        :utc_offset=>nil, 
        :profile_use_background_image=>true, 
        :url=>nil, :geo_enabled=>false, 
        :lang=>"en", 
        :profile_image_url=>"http://a0.twimg.com/profile_images/2808322935/d5da93a06dbd45959cb4dba0797689a5_normal.jpeg", 
        :profile_text_color=>"333333", 
        :name=>"Roslyn Montalbo ", 
        :profile_background_image_url_https=>"https://si0.twimg.com/images/themes/theme1/bg.png", 
        :description=>"Amateur social media scholar. Friend of animals everywhere. Explorer. Unapologetic twitter advocate. Incurable travel geek. Writer", 
        :protected=>false, 
        :profile_sidebar_border_color=>"C0DEED", 
        :followers_count=>0, 
        :default_profile=>true, 
        :profile_background_tile=>false, 
        :location=>"Dublin", 
        :profile_sidebar_fill_color=>"DDEEF6", 
        :following=>false, 
        :statuses_count=>3, 
        :follow_request_sent=>false, 
        :listed_count=>0, 
        :id_str=>"926557182", 
        :profile_background_color=>"C0DEED", 
        :notifications=>false, 
        :verified=>false, 
        :is_translator=>false}, 
        :coordinates=>nil, 
        :source=>"web", 
        :favorited=>false, 
        :id=>265276345704124416}>,



#<Twitter::Tweet:0x007fd070de2478 

    @attrs={:in_reply_to_status_id_str=>nil,
    :retweet_count=>0, 
    :contributors=>nil, 
    :truncated=>false, 
    :text=>"test post @doodleboots", 
    :in_reply_to_user_id_str=>nil, 
    :geo=>nil, 
    :retweeted=>false, 
    :in_reply_to_screen_name=>nil,  
    :id_str=>"247909685582233600", 
    :entities=>{:hashtags=>[], 
        :urls=>[], 
        :user_mentions=>[{:name=>"doodle", 
            :indices=>[10, 22], 
            :screen_name=>"doodleboots", 
            :id_str=>"119208666", 
            :id=>119208666}]}, 
            :in_reply_to_user_id=>nil, 
            :in_reply_to_status_id=>nil, 
            :created_at=>"Tue Sep 18 04:07:29 +0000 2012", 
            :place=>nil, 
            :user=>{:profile_image_url_https=>"https://si0.twimg.com/profile_images/728693845/doodleBootsIcon_normal.png", 
            :id=>119208666, 
            :profile_background_image_url=>"http://a0.twimg.com/images/themes/theme1/bg.png", 
            :entities=>{:description=>{
                :urls=>[]}, 
                :url=>{:urls=>[{
                    :display_url=>nil, 
                    :indices=>[0, 22], 
                    :expanded_url=>nil, 
                    :url=>"http://doodleboots.com"}]}}, 
                    :screen_name=>"doodleboots", 
                    :profile_link_color=>"0084B4", 
                    :default_profile_image=>false, 
                    :created_at=>"Wed Mar 03 00:09:50 +0000 2010", 
                    :contributors_enabled=>false, 
                    :friends_count=>16, 
                    :time_zone=>nil, 
                    :favourites_count=>0, 
                    :utc_offset=>nil, 
                    :profile_use_background_image=>true, 
                    :url=>"http://doodleboots.com", 
                    :geo_enabled=>false, 
                    :lang=>"en", 
                    :profile_image_url=>"http://a0.twimg.com/profile_images/728693845/doodleBootsIcon_normal.png", 
                    :profile_text_color=>"333333", 
                    :name=>"doodle", 
                    :profile_background_image_url_https=>"https://si0.twimg.com/images/themes/theme1/bg.png", 
                    :description=>"Take a pic of a drawing/doodle and tweet it @doodleboots! It will be re-tweeted and posted to the website for all to see.", 
                    :protected=>false, 
                    :profile_sidebar_border_color=>"C0DEED", 
                    :followers_count=>14, 
                    :default_profile=>true, 
                    :profile_background_tile=>false, 
                    :location=>"", 
                    :profile_sidebar_fill_color=>"DDEEF6", 
                    :following=>false, 
                    :statuses_count=>16, 
                    :follow_request_sent=>false, 
                    :listed_count=>1, 
                    :id_str=>"119208666", 
                    :profile_background_color=>"C0DEED", 
                    :notifications=>false, 
                    :verified=>false, 
                    :is_translator=>false}, 
                    :coordinates=>nil, 
                    :source=>"web", 
                    :favorited=>false, 
                    :id=>247909685582233600}>, 
4

3 回答 3

5

该类Twitter::Tweet不公开该id_str属性。这些是公开的属性(来自https://github.com/sferik/twitter/blob/master/lib/twitter/tweet.rb的源代码):

:favorited, :favoriters, :from_user_id, :from_user_name,
  :in_reply_to_screen_name, :in_reply_to_attrs_id, :in_reply_to_status_id,
  :in_reply_to_user_id, :iso_language_code, :profile_image_url,
  :profile_image_url_https, :repliers, :retweeted, :retweeters, :source,
  :text, :to_user, :to_user_id, :to_user_name, :truncated

如果您需要访问其他任何内容,请通过attrs哈希访问它:

@tweet.attrs[:id_str]
于 2012-11-06T10:59:58.467 回答
2

为什么它起作用:text是因为您的mention哈希有一个名为的键:text,而您正在寻找的其他属性 (:screen_name:id_str) 深深地嵌套在其中。

如果您仔细查看 的结构,mention您会注意到它包含 key :entities(与 处于同一级别:text),其值是另一个包含 key 的哈希:user_mentions。的值:user_mentions是一个哈希数组。您正在寻找的是在这个哈希数组中。

puts mention[:entities][:user_mentions][0][:screen_name] #=> doodleboots
puts mention[:entities][:user_mentions][0][:id_str]      #=> 119208666
于 2012-11-06T10:49:43.160 回答
1

请试试这个:

<% Twitter.mentions_timeline.each do |mention| %>
      <%= mention['text'] %> <%# dies on some of em, like mention.id_str - not sure why %>
      <br /><hr />         
<% end %>
于 2012-11-06T10:34:44.240 回答