#!/usr/bin/env ruby

require 'browshot'

browshot = Browshot.new('my_api_key')

screenshot = browshot.screenshot_create('http://www.google.com/', { 'instance_id' => 12, 'size' => 'screen' }) # all default paramters, instance_id = 12 (free)

# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while (screenshot['status'] != 'finished' && screenshot['status'] != 'error')
  puts "Wait...\n"
  sleep(10)
  screenshot = browshot.screenshot_info(screenshot['id'])
end

# screenshot is done: finished (sucessful) or error (failed)
if (screenshot['status'] == 'error')
  puts  "Screenshot failed: #{screenshot['error']}\n" # display the reason for the error
else # request the thumbnail
  browshot.screenshot_thumbnail_file(screenshot['id'], 'google-640.png', { 'width' => 640, 'height' => 480 })
  puts "Screenshot was saved to google-640.png\n"
end