karthick nagarajan karthick nagarajan - 1 month ago 8
Ruby Question

Get likes from twitter in Ruby on rails

I can't get likes and other basic information.
I use

omniauth-twitter
gem.

This is my
home_controller.rb


if session[:twitter_token].present?
twitter_token = current_user.social_medias.find_by_media_type("twitter").token
client = Twitter::Streaming::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
p 'twitter'
p client
end


I got this error in my console..

NameError (uninitialized constant HomeController::Twitter):
app/controllers/home_controller.rb:277:in `edit_profile'

Answer

yes you must required twitter gem also..

gem 'omniauth-twitter'
gem 'twitter'

And i got response like this in my console..

#<Twitter::Streaming::Client:0xb2ed008c @consumer_key="OxVFLj95hQXTLmln1bBfNPYqJ", @consumer_secret="c0JqZs5jUc6G0M8UTWA4AqErxZV7ztKMQ5iwR0llk5M7E7xXiq", @access_token="786800200815751168-rvqqfX8kRPgx2LiNflHXazyPGrwQDry", @access_token_secret="09ZK5WKwWB1ECvOJwOslGtJBnHZHw32aJmk5PSy6QZVOD", @connection=#<Twitter::Streaming::Connection:0xb7b7fcc @tcp_socket_class=TCPSocket, @ssl_socket_class=OpenSSL::SSL::SSLSocket>>

and this is get basic information from twitter(i.e, likes, followers, followings)

if session[:twitter_token].present?
      twitter_token = current_user.social_medias.find_by_media_type("twitter").token
      @client = Twitter::REST::Client.new do |config|
        config.consumer_key        = "consumer_key"
        config.consumer_secret     = "consumer_secret"
        config.access_token        = "access_token"
        config.access_token_secret = "access_token_secret"
      end
      p 'twitter'
      p @client
      p 'followers'
      @twitter_followers = @client.followers.count
      p @followers = @client.followers.count
      p 'following'
      p @twitter_following = @client.friends.count
    end

Thanks all :) :)