簡體   English   中英

無法解析來自twitter gem的哈希值

[英]Can't parse hash from twitter gem

我有寶石工作:可以從它發推文並提取信息。

我可以抓住這個哈希並打印出它的.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}>, 

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]

為什么它適用於: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

請試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM