Search code examples
rubyapimethodstwitterrubygems

I want to obtain following list in text instead of objects using twitter api in ruby programming language


I'd love to obtain following list in text (screen names) instead of objects using Twitter Api. I am new to Ruby programming language and this's my first attempt using api with ruby especially Twitter api. What I expect is list of screen names instead of objects and I will show you examples bellow:

Results I get currently: current results

Desired and expected results

I've tried methods such as .full_text and .text appended to the objects and didn't get my desired results.I've searched almost everywhere especially here in Stackoverflow and didn't find my answer yet.

This's my code below:

require 'rubygems'
require 'bundler/setup'

require 'twitter'
require 'json'
require 'yaml'


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


following_list = client.friends('User-exmaple')
begin
  for friend in following_list
    puts friend
  end
rescue Twitter::Error::TooManyRequests => error
  # NOTE: Your process could go to sleep for up to 15 minutes but if you
  # retry any sooner, it will almost certainly fail with the same exception.
  sleep error.rate_limit.reset_in + 1
  retry
end

I hope this explains everything, thank you so much.


Solution

  • I solved it by appending screen_name method to friend, example below:

    require 'rubygems'
    require 'bundler/setup'
    
    require 'twitter'
    require 'json'
    require 'yaml'
    
    
    client = Twitter::REST::Client.new do |config|
      config.consumer_key        = ""
      config.consumer_secret     = ""
      config.access_token        = ""
      config.access_token_secret = ""
    end
    
    
    following_list = client.friends('User-exmaple')
    begin
      for friend in following_list
        puts friend.screen_name
      end
    rescue Twitter::Error::TooManyRequests => error
      # NOTE: Your process could go to sleep for up to 15 minutes but if you
      # retry any sooner, it will almost certainly fail with the same exception.
      sleep error.rate_limit.reset_in + 1
      retry
    end
    
    

    I hope this explains the solution I found.