检查Twitter用户关系created_at?

时间:2015-04-29 17:13:04

标签: ruby-on-rails ruby twitter twitter-bootstrap-3 social-network-friendship

我试图提取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>

有没有办法提取信息,以便我可以检索关系信息的哈希数组?就像友谊创造时一样?

1 个答案:

答案 0 :(得分: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
 }}