0

I'm trying to go through the first 5 shots on dribbble with Jeremys awesome dribbble library (https://github.com/jeremyw/swish), get their titles and then print those items in the view, but I keep getting the error: undefined method `title' for #.

@dribbbletimes = []
(1..5).each do |i| 
    @dribbbletimes << Dribbble::Shot.find(i).title 
end 

If I replace i with a number, then it can find the title with no problems and print it 5 times. Any guesses why this is happening?

Holger Sindbaek
  • 2,278
  • 6
  • 41
  • 68

1 Answers1

2

I just tried your code and it seems that the find method returns a different kind of object when nothing is found, so calling the title method doesn't work when the shot number is not valid.

For example:

1.9.2-p290 :017 > Dribbble::Shot.find(2)
 => #<Dribbble::Shot:0x007ff96b3ed110 @player=#<Dribbble::Player:0x007ff96b3ed048 @created_at="2009/07/21 19:32:24 -0400", @shots_count=172, @twitter_screen_name="owltastic", @avatar_url="http://dribbble.s3.amazonaws.com/users/4/avatars/original/Meagan-6.jpg?1309446649", @likes_received_count=14937, @name="Meagan Fisher", @location="Brooklyn, NY", @following_count=445, @likes_count=1080, @website_url="http://owltastic.com", @username="owltastic", @url="http://dribbble.com/owltastic", @rebounds_count=3, @draftees_count=44, @id=4, @drafted_by_player_id=1, @followers_count=9979, @comments_received_count=1795, @comments_count=211, @rebounds_received_count=13>, @created_at="2009/07/23 07:07:53 -0400", @short_url="http://drbl.in/c", @image_url="http://dribbble.com/system/users/4/screenshots/2/Picture-2.png?1309017100", @title="Playing with Dribbble", @likes_count=9, @rebound_source_id=nil, @url="http://dribbble.com/shots/2-Playing-with-Dribbble", @rebounds_count=0, @id=2, @image_teaser_url="http://dribbble.com/system/users/4/screenshots/2/Picture-2_teaser.png?1309017100", @height=300, @views_count=13299, @comments_count=2, @width=400> 

1.9.2-p290 :018 > Dribbble::Shot.find(3)
 => #<Dribbble::Shot:0x007ff96b3ced50 @created_at=nil, @message="Not found"> 

You should distinguish if the returned object is really valid before adding it to the array.

Update:
After digging around a little bit, I found that an invalid shot returns a nil value on the created_at attribute, so you might do something like this:

@dribbbletimes = []
(1..5).each do |i|
    shot = Dribbble::Shot.find(i)  
    @dribbbletimes << shot.title unless shot.created_at.blank? 
end 
bbonamin
  • 30,042
  • 7
  • 40
  • 49