Implementing twitter gem in rails 5 app - ruby-on-rails

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.

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.

Related

Rails gem tweetstream with Encryption not available on this event-machine error

I'm trying to display tweets using gem tweetstream and following the guide at https://github.com/tweetstream/tweetstream
In my tweets_helper.rb
require "twitter" require 'tweetstream'
module TweetsHelper
##client = Twitter::REST::Client.new do |config|
config.consumer_key = Rails.application.config.twitter_key
config.consumer_secret = Rails.application.config.twitter_secret
config.access_token = Rails.application.config.twitter_oauth_token
config.access_token_secret = Rails.application.config.twitter_oauth_secret
end
def user_timeline
##client.user_timeline
end
TweetStream.configure do |config|
config.consumer_key = Rails.application.config.twitter_key
config.consumer_secret = Rails.application.config.twitter_secret
config.oauth_token = Rails.application.config.twitter_oauth_token
config.oauth_token_secret = Rails.application.config.twitter_oauth_secret
config.auth_method= :oauth
end
TweetStream::Client.new.track('Pink Floyd') do |status|
puts "#{status.text}"
end
end
But this throws me this error in the terminal and shuts down the localserver
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/tweetstream-2.6.1/lib/tweet
stream/client.rb:400: warning: epoll is not supported on this platform
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/tweetstream-2.6.1/lib/tweet
stream/client.rb:401: warning: kqueue is not supported on this
platform terminate called after throwing an instance of
'std::runtime_error' what(): Encryption not available on this
event-machine
This application has requested the Runtime to terminate it in an
unusual way. Please contact the application's support team for more
information.
However the server runs if I remove
TweetStream::Client.new.track('Pink Floyd') do |status|
puts "#{status.text}"
end
How do I fix this error?
UPDATE
I think all signs point to installing libssl-dev https://github.com/plamoni/SiriProxy/issues/41. On Ubuntu it is aptitude install libssl-dev.
How do I do it on windows 8?

Can't initialize Twitter Gem For Rails

I've been trying to get the Twitter Gem running. I followed the documentation and placed
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
I placed it in a custom initializer file called Twitter.rb
When I try to run rails console it gives me this error " uninitialized constant Twitter::REST (NameError)"
This error was reported here
The solution is to use Twitter::Client.new (no ::REST)
I faced with same issue. But I have installed two gems https://github.com/sferik/twitter and https://github.com/twitter/twitter-text-rb, they both have 'Twitter' as main module and when I call Twitter, I reached second instead first...
Twitter::Client.new - not working to.
add require 'twitter' on the top of your file

How to implement the search for the twitter APIs in controller as well as in Erb template?

I am new to Ruby on Rails as i want to develop a twitter app with the search option. Using which an user can search for the other users. I am using twitter gem and oauth gem for my project. I Have tried using below given code but its not working though get_client.search("user") it working in Rails Console.
def get_client
Twitter::REST::Client.new do |config|
config.consumer_key = "ABCD"
config.consumer_secret = "XYZ"
config.access_token = "PQRS"
config.access_token_secret = "LMNO"
end
end
def search
query = (param[:search])
get_client.search("query")
end

Rails twitter gem set up

I've installed the twitter gem :https://github.com/sferik/twitter to my site, I've added an application to my twitter account and I've put the following code in an initializer:
Twitter.configure do |config|
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
config.oauth_token = YOUR_OAUTH_TOKEN
config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
end
now on my view I have:
- Twitter.user_timeline('username')
I don't have experience with the twitter gem. But usually you would put something like this in an initializer.
So i would put it in myproject/config/initializers/twitter.rb
Update
Try
= Twitter.user_timeline('username')
Note the = instead of the -

Tweetstream not connecting?

I'm using a recent version of Tweetstream and can't seem to connect to the twitter streaming API. I'm authenticating through oAuth like so:
require "rubygems"
require "tweetstream"
def index
TweetStream.configure do |config|
config.consumer_key = 'aaa'
config.consumer_secret = 'aaa'
config.oauth_token = '11329872-aaa'
config.oauth_token_secret = 'aaa'
config.auth_method = :oauth
config.parser = :yajl
end
TweetStream::Client.new.track('words') do |status|
puts "streaming"
puts "#{status.text}"
end
end
It eventually times out though with the following:
TweetStream::ReconnectError (Failed to reconnect after 7 tries.)
Any idea what's wrong?
Found the solution to this one was a missing required gem. All I needed to add was:
require 'yajl'
I also noticed that this is only required if you're using oAuth to authenticate with twitter.

Resources