#!/usr/bin/env ruby

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

require 'browshot'

browshot = Browshot.new('my_api_key')


data = browshot.simple({ 'url' => 'http://mobilito.net/', 'cache' => 60 * 60 * 24 * 365, 'instance_id' => 12 }) # 1 year cache, free screenshot
# code above is blocking, it will return when the screenshot finished or failed

if (data['code'].to_i == 200) # success
  File.open("screenshot.png", 'w') {|f| f.write(data[:png]) }
  
  puts "Screenshot saved to screenshot.png\n"
else
  puts "Screenshot failed!\n"
  # the reason for the error is sent as part of the HTTP response in the X-Error header but it is not exposed by this library
end

# quicker way to save a screenshot
info = browshot.simple_file("mobilito.png", { 'url' => 'http://mobilito.net/', 'cache' => 0, 'instance_id' => 65, 'screen_width' => 1280, 'screen_height' => 1024 }) # no cache, premium browser
if (info[:file] != ''
  puts "Screenshot saved to #{info[:file]}\n"
else
  puts "The screenshot failed\n"
end