Skip to content

Instantly share code, notes, and snippets.

@toolmantim
Created April 7, 2010 08:41
Show Gist options
  • Select an option

  • Save toolmantim/358667 to your computer and use it in GitHub Desktop.

Select an option

Save toolmantim/358667 to your computer and use it in GitHub Desktop.
# In answer to: http://twitter.com/jimwhimpey/status/11727440383
#
# "I want to output 10 Flickr photos one at a time as the REST response
# is returned for each rather than wait for all 10."
class FlickrStreamer
def initialize(photos)
@photos = photos
end
def each
@photos.each do |photo|
yield fetch_photo_from_flickr(photo)
end
end
def fetch_photo_from_flickr(photo)
# Call the flickr API, return the response
end
end
get '/photos' do
halt FlickrStreamer.new(["photo1", "photo2", "photo3"])
end
@jimwhimpey

Copy link
Copy Markdown

So where does the output happen? In fetch_photo_from_flickr?

def fetch_photo_from_flickr(photo)

haml :index

end

@jimwhimpey

Copy link
Copy Markdown

Basically I'm asking, is it possible to output stuff to a template before everything has finished executing?

@toolmantim

Copy link
Copy Markdown
Author

No, it's really how the stack is designed (especially with layouts, etc).

Why the hell are you wanting to do that though? Maybe there's a better way you should be approaching it...

@jimwhimpey

Copy link
Copy Markdown

I wanted to do it because I'm getting more than just basic info about each photo so it takes 2 extra REST calls for each photo which for 10 photos results in 21 individual REST calls before you see even one photo. It'd be nice if there was a Flickr method which returned way more data, then it'd only require one call and be nice and fast.

I guess what I could do is only grab 2 or 3 photos originally and then make individual JS calls for the rest.

@toolmantim

Copy link
Copy Markdown
Author

JS calls is totally what you want to do. You get to control the experience then too, and if you had lots of little sinatra instances working away you'd end up getting all that stuff loading in quicker than just one-at-a-time.

@jimwhimpey

Copy link
Copy Markdown

Yeah, looks like JS calls to my own Sinatra back end will be the way to go. Loving this stuff so far. Thanks for your help!

@toolmantim

Copy link
Copy Markdown
Author

nice! You could also call Flickr directly in the browser via json-p. Just an idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment