Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Gem for Ruby on Rails not working. What am I missing?

I'm trying to get the Twitter gem version 5.1.1 to work in my application, but I'm having trouble with the configuration. I am running rails 4 and Ruby 2.0.0. In my Gemfile I have "gem 'twitter'" listed and have installed it via the bundler. As per the documentation, I created an initializer file in config/initializers/twitter_credentials.rb. That file looks like this (though I have the key's and secrets filled with my information from twitter):

require 'twitter'

client = Twitter::REST::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

When I try to use the client in the rails console, this is the error message I receive:

2.0.0p353 :001 > client.update("I'm tweeting with @gem!")
NameError: undefined local variable or method `client' for main:Object
from (irb):1
from /Users/user_name/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/user_name/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/user_name/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'

Any idea what the issue is?

like image 288
sojo2600 Avatar asked Feb 14 '23 10:02

sojo2600


1 Answers

Yes things kind of changed with later releases. So to access it in the app, make the variable a global variable to have full access. So try $client =

Now when you access it call $client.user_timeline etc.

like image 50
gdiaz Avatar answered Feb 17 '23 20:02

gdiaz