I don't like asking noobie questions but I've spent more time than I care to admit googling and trying to figure out this simple process. I'm trying to use the twitter gem to post tweets in my app's views.
I have this script that works when I run it on the command line, but I don't know how to access the data on my views. I have the gem installed. I tried adding this code to my controller but it still doesn't let me call it in the view.
require 'rubygems'
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "(redacted)"
config.consumer_secret = "(redacted)"
config.access_token = "(redacted)"
config.access_token_secret = "(redacted)"
end
client.search("help", :result_type => "recent").take(3).each do |tweet|
puts tweet.text
end
If anyone can point me in the right direction, that would be great.
Well, I'll post some (untested) code that might help set you in the right direction. It's not the best way to do it, as it's not very flexible, but it should do what your example does. Make sure add the Twitter gem to your Gemfile and bundle install. Create a Ruby class called TweetsFromAccount. Saving it in the /lib directory works just fine.
class TweetsFromAccount
def initialize
#client = Twitter::REST::Client.new do |config|
config.consumer_key = "(redacted)"
config.consumer_secret = "(redacted)"
config.access_token = "(redacted)"
config.access_token_secret = "(redacted)"
end
end
def recent_tweets(quantity)
array_of_tweets = []
#client.search("help", :result_type => "recent").take(quantity).each do |tweet|
array_of_tweets << tweet.text
end
end
end
In the controller, create a new TweetsFromAccount object and assign it to an instance variable, #tweets. Create an additional instance variable set the value of #tweets.recent_tweets(3). This should fetch the 3 most recent tweets.
def show
# other stuff
#tweets = TweetsFromAccount.new
#array_of_tweets = #tweets.recent_tweets(3)
end
Then in your view, just iterate over #array_of_tweets.
<ul>
<% #array_of_tweets.each do |tweet| %>
<li><%= tweet %></li>
<% end %>
</ul>
Related
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.
I am trying to do a simple rails app that will search Twitter. I am using the Twitter gem (http://sferik.github.io/twitter/). I created a twitter_credentials.rb in the initializer folder with following code:
require 'twitter'
Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
My view looks like this:
<% Twitter.search("to:justinbieber marry me",
:result_type => "recent").take(3).each do |tweet| %>
<%= tweet.text %>
<% end %>
However, whenever I try to call any method from the docs, I get an error "undefined method method name for Twitter:Module". I've also tried moving that block into the appropriate controller but I receive the same error. Any help would be really appreciated..
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...
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
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 -
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.