1

I am new to Ruby and had a quick question.

I am trying to get all of the posts from a user's timeline, so I figured I would need to do a user_timeline api call to twitter and then filter out the posts manually. Then, while reading the Ruby Twitter documentation, I found this:

puts Twitter.user_timeline("twitter_handle").first.text

...and that will return the post already parsed out.

Is there a way to get more than just the first post automatically parsed out like that, or is that just an array method for the first and last object in the array?

Thanks

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
startuprob
  • 1,917
  • 5
  • 30
  • 45

3 Answers3

2

It looks like user_timeline just returns an array, so you ought to be able to use Ruby's normal array methods with it.

Twitter.user_timeline("twitter_handle").each do |tweet|
    puts tweet
end
ben author
  • 2,855
  • 2
  • 25
  • 43
  • Code [golfing](http://stackoverflow.com/questions/6962883/how-to-unflatten-a-ruby-array/6963422#6963422): `Twitter.user_timeline("twitter_handle").each(&method(:puts))` – Andrew Grimm Oct 30 '11 at 22:15
  • but will that work for the tweet text only? how 'bout the more conventional `Twitter.user_timeline("twitter_handle").each{|t| puts t.text}` – ben author Nov 02 '11 at 03:31
1

You need to iterate over each object.

Twitter.user_timeline("twitter_handle").each do |tweet|
    puts tweet.text
end

The first and last methods are just convenience methods on arrays.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

As you can see from source, Twitter::Client#user_timeline returns array from 20 most recent Twitter::Status, so that you can use up to 20 parsed records.

WarHog
  • 8,622
  • 2
  • 29
  • 23