Reputation: 3
I'm programming an application in Ruby on Rails v(2.0.0/4.0.0) and trying to integrate a live twitter feed from a specific source (@u101) onto a basic HTML page and don't understand how. After Googling around for a few hours I've seen a few different gems that seem to integrate Twitter (tweetstream, twitter api, twitter), however, I can't get any of these to work. I've followed guides such as this http://www.phyowaiwin.com/how-to-download-and-display-twitter-feeds-for-new-year-resolution-using-ruby-on-rails and that one in particular gives me this error:
irb(main):002:0> Tweet.get_latest_new_year_resolution_tweets
NameError: uninitialized constant Twitter::Search
from C:/Sites/EasyBakeOven/blog/app/models/tweet.rb:6:in `get_latest_new
_year_resolution_tweets'
Which I don't understand. If someone could walk me through the necessary steps to accomplish my goal or point me in the right direction I'd be very thankful.
UPDATE: Model: tweet.rb
class Tweet < ActiveRecord::Base
#A method to grab latest tweets from Twitter
def self.get_latest_new_year_resolution_tweets
#create a Twitter Search object
search = Twitter::REST::Client.new
#grab recent 100 tweets which contain 'new year resolution' words, and loop each of them
search.containing("u101").result_type("recent").per_page(100).fetch.each do |tweet_results|
#parsing the string 'created_at' to DateTime object
twitter_created_at = DateTime.parse(tweet_results.created_at)
#making sure we are not saving exact same tweet from a person again
unless Tweet.exists?(['twitter_created_at = ? AND from_user_id_str = ?', DateTime.parse(tweet_results.created_at), tweet_results.from_user_id_str])
#finally creating the tweet record
Tweet.create!({
:from_user => tweet_results.from_user,
:from_user_id_str => tweet_results.from_user_id_str,
:profile_image_url => tweet_results.profile_image_url,
:text => tweet_results.text,
:twitter_created_at => twitter_created_at
})
end
end
end
end
tweets_controller.rb
class TweetsController < ApplicationController
def index
#Get the tweets (records) from the model Ordered by 'twitter_created_at' descending
@tweets = Tweet.order("twitter_created_at desc")
end
end
index.html.rrb:
<h1>Tweets#index</h1>
<div id="container">
<ul>
<% @tweets.each do |tweet| %>
<li class="<%=cycle('odd', '')%>">
<%= link_to tweet.from_user, "http://twitter.com/#{tweet.from_user}", :class => "username", :target => "_blank" %>
<div class="tweet_text_area">
<div class="tweet_text">
<%= tweet.text %>
</div>
<div class="tweet_created_at">
<%= time_ago_in_words tweet.twitter_created_at %> ago
</div>
</div>
</li>
<% end %>
</ul>
</div>
Also again I'm trying to display tweets with '#u101'
Upvotes: 0
Views: 1081
Reputation: 503
In the particular tutorial you used, go to line 7 in app/models/tweet.rb and you'll see:
search = Twitter::Search.new
Replace that with
search = Twitter::REST::Client.new
This was a recent change to the ruby gem. You can always see what's most current in the documentation!
Upvotes: 1
Reputation: 76774
Use TwitterFetcher (JS) (no gems)
This works by taking a widget (which is live & fully supported by Twitter) & parsing out any styling / HTML you may not want. We've implemented this numerous times:
#app/assets/javascripts/twitterfetch.js
[[twitter fetcher JS here]]
#app/assets/javascripts/application.js
//= require twitterfetch
$(function() {
twitterFetcher.fetch('WIDGET_ID', 'twitter_feed', 3, true, true, true, '', false, handleTweets, false);
function handleTweets(tweets){
var x = tweets.length;
var n = 0;
var element = document.getElementById('twitter_feed');
var html = '<ul>';
while(n < x) {
html += '<li>' + tweets[n] + '</li>';
n++;
}
html += '</ul>';
element.innerHTML = html;
};
});
I can help you more if you'd like to use this method
"Live" Streaming
There are several problems with "live" streaming from twitter:
- Their 1.1 API is rate-limited
- How will you store the data?
I think I get what you're trying to achieve, but in reality, their API policies have made it very difficult to implement any "live" functionality. If you want to just display your tweets in your own way, I'd recommend using a widget & using the JS provided above
Upvotes: 2