How do you iterate through Twitter gem user timeline?
This is because you store data into a single @tweet
record, you need also create a new record just after the @tweet.save
, it is need in order to new data do not overwrite old ones. Do something like:
@content.each do |post|
if post.id > element.tweet_since_id
@tweet = Tweet.new :screen_name => element.screen_name, :content => post.text
@tweet.user_id = element.id
@tweet.save
element.tweet_since_id = post.id
end
end
Note, that it is good style that fields of a record of a view something_id
has belong to another record types in your DB, if you have no record assiciated with the field, use another naming, like something_xid
or somethind_sid
.
Pulling Images from User's Timeline with Twitter Gem
I don't know how you can do that with the gem currently, but you could filter the results with an .each loop:
@twitter.each do |tweet|
if tweet.media_url.present?
image_tag(tweet.media_url)
end
end
Problems with this method
You're looping through the whole array every time (not very efficient). I am looking at the source to see if anything can be done to filter the results so that a loop isn't necessary
Update
Working code from one of our apps:
In helper:
def twitter_feed
Twitter.user_timeline(21931601).take(5) #limits array to 5 items
end
In view:
twitter_feed.each do |tweet|
image_tag(tweet.media[0]["media_url"], :size => tweet.media[0]["sizes"]["thumb"]) if(tweet.media.present?)
end
More Options
There are more options on Twitter Gem's Github page
rails twitter gem list retrieve/display full tweet
Doing an API call :
$client.get(“https://api.twitter.com/1.1/lists/statuses.json?owner_screen_name=USERNAME&slug=LISTNAME&tweet_mode=extended”)
Need simple Twitter API v1.1 example to show timeline using jQuery or C# ASP.NET
Okay, I received an answer from a colleague who had to make a similar change at my company. Here is the coded solution. He received this from a company we contracted to build a new interface for us with a Twitter timeline.
<a class="twitter-timeline" href="https://twitter.com/companytwitterhandle" data-widget-id="18DigitMagicNumber" data-chrome="nofooter transparent noscrollbar noheader noborders" data-tweet-limit="1" >Tweets by @@companytwitterhandle</a>
<script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + "://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); } }(document, "script", "twitter-wjs");</script>
You will see in the data-widget-id
attribute 18DigitMagicNumber
. That number is apparently tied to our Twitter handle or our company. I don't know how that gets generated.
As indicated, a contracted company wrote that code for us, so I won't try to explain what it's all doing.
I cut and pasted this from one site into an unrelated site and it worked immediately. The only thing I changed was to remove the actual company handle and data-widget-id
, and to replace the single @
with @@
to escape it in MVC.
Hopefully this will help some of you who are also migrating to Twitter 1.1. Thanks.
How can I retrieve my profile with the twitter gem
You can do the following simple steps to use the Twitter client gem:
# get user id from user name
id = client.user( 'user_name' ).id
# read user's timeline by id
timeline = client.user_timeline id
# read user's timeline of yours
your_timeline = client.user_timeline
you can omit the username, and you get the timeline of current user, so try also to issue:
user_info = client.user
Related Topics
How to Sort So That "Vitamin B12" Is Not in Front of "Vitamin B6"
How to Create Two Routes in One Block in Grape
Iterate JSON with Ruby and Get a Key,Value in an Array
Ruby - Convert Integer to String
How to Escape the Vertical Bar When Doing Command Line with Ruby
Ruby on Rails - Add Condition on ':Include =>' to Load Limited Number of Objects
Ruby#Index Method VS Binary Search
Recursively Convert Hash Containing Non-Utf Chars to Utf
Trying to Find Vowels of a String Using Ruby While Loops
Ruby on Windows7 - Could Not Find Gem
Restart Rails Server Automatically After Every Change in Controllers
Will_Paginate Can It Order by Day
How to Stop Bundler from Adding Ruby Version to Gemfile.Lock
Gem Install JSON Fails with Redifinition of Struct Timezone/Timespec