#!/usr/bin/env ruby

#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################

require 'browshot'

browshot = Browshot.new('my_api_key', true) # more debug information


list = browshot.screenshot_multiple({ 'urls' => ['http://www.google.com/', 'https://browshot.com/', 'http://mobilito.net/'], 'instances' => [12, 24, 72], 'size' => 'page' }) # up to 5 URLs

sleep(20)
ids = list.keys() #  hold all screenshot IDs


while (ids.count() > 0) # go through each screenshot
  i = 0
  while (i < ids.count())
    id = ids[i]
    
    info = browshot.screenshot_info(id)
    if (info['status'] == 'finished')
      browshot.screenshot_thumbnail_file(id, "#{id}.png")
      ids.delete_at(i) # remove ID from the list
    elsif (info['status'] == 'error')
      puts "Screenshot failed: #{info['error']}\n"
      puts "\tURL: #{info['url']}\n"
      puts "\tinstance_id: #{info['instance_id']}\n"
      puts "\n"
      
      ids.delete_at(i) # remove ID from the list
    else
      # wait
      i += 1
    end
  end
  
  if (ids.count() > 0)
    sleep(10)
  end
end