Antonio
Antonio

Reputation: 751

Implementing twitter gem in rails 5 app

I'm having issues trying to experiment with the twitter gem. My rails app is pretty simple. No devise or authentication at the moment. I'm more interesting in just making API calls and collect batch data to experiment with D3 at a later point.

I'm trying to follow the vague documentation and this is as far as I got.

# config/initializers/twitter_client.rb    
require 'twitter'

twitter = Twitter::REST::Client.new do |config|
  config.consumer_key =         ENV['##############']
  config.consumer_secret =      ENV['##############']
  config.access_token =         ENV['##############']
  config.access_token_secret =  ENV['##############']
end

So now I want to byebug during an action in order to poke around. However, the controller doesn't seem to understand the twitter variable. I've tried making them instance and global. I am probably failing to require something. But, I was under the impression initializer files are accessable everywhere by default.

Upvotes: 0

Views: 203

Answers (1)

dnsh
dnsh

Reputation: 3633

If you want global variable. Use :: operator

# config/initializers/twitter_client.rb    
require 'twitter'

::MyTwitter = Twitter::REST::Client.new do |config|
  config.consumer_key =         ENV['##############']
  config.consumer_secret =      ENV['##############']
  config.access_token =         ENV['##############']
  config.access_token_secret =  ENV['##############']
end

Then you will be able to use MyTwitter anywhere in your app.

Upvotes: 1

Related Questions