0

我被困在试图提取 Twitter 哈希并且似乎无法找到如何获取友谊属性?

我的代码:

require 'twitter'
require 'rubygems'

client = Twitter::REST::Client.new do |config|
  config.consumer_key = "..."
  config.consumer_secret = "..."
  config.access_token = "..."
  config.access_token_secret = "..."
end

puts client.friendship('user1','user2').to_s

回复:

#<Twitter::Relationship:0x007f88c1e58de0>

有没有办法提取信息,以便我可以检索关系信息的哈希数组?即像友谊是什么时候建立的?

4

1 回答 1

0

我认为你找不到 created_at 但你得到了其他属性

您可以使用直接属性名称,例如

data = client.friendship('user1','user2')
data.source.screen_name # user1 

如果不将其转换为 JSON

data = client.friendship('user1','user2').as_json
handle = data["target"]["screen_name"] # user2

as_json 之后的示例响应

{"source"=>
 {
  "id"=>7697764211,
  "id_str"=>"7697764211",
  "screen_name"=>"user1",
  "following"=>true,
  "followed_by"=>false,
  "following_received"=>nil,
  "following_requested"=>nil,
  "notifications_enabled"=>nil,
  "can_dm"=>false,
  "blocking"=>nil,
  "blocked_by"=>nil,
  "muting"=>nil,
  "want_retweets"=>nil,
  "all_replies"=>nil,
  "marked_spam"=>nil},
"target"=>
 { "id"=>2053615711,
   "id_str"=>"2053615711",
   "screen_name"=>"use2",
   "following"=>false,
   "followed_by"=>true,
   "following_received"=>nil,
   "following_requested"=>nil
 }}
于 2015-04-29T17:30:50.847 回答