browshot.rb

Browshot is available on Rubygems.

gem install browshot

Here are a couple of basic code samples. You can find more code samples for each API call on the API Documentation page.

Download a screenshot (simple API)

#!/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

Download code sample

Download a screenshot (full API)

#!/usr/bin/env ruby

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

require 'browshot'

browshot = Browshot.new('my_api_key')

screenshot = browshot.screenshot_create('http://www.google.com/', { 'instance_id' => 12, 'size' => 'page' }) # all default parameters, 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
  image = browshot.screenshot_thumbnail(screenshot['id'])
  
  # save the screenshot
  File.open("browshot.png", 'w') {|f| f.write(image) }
end

Download code sample

Download a 640x480 thumbnail (simple API)

#!/usr/bin/env ruby

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

require 'browshot'

browshot = Browshot.new('my_api_key')

info = browshot.simple_file('google-640.png', { 'url' => 'http://www.google.com/', 'width' => 640, 'height' => 480 })

if (info[:file] == '' || info[:code].to_i != 200)
  puts "Screenshot failed\n"
else
  puts "Thumbnail saved at #{info[:file]}\n"
end

Download code sample

Download a 640x480 thumbnail (full API)

#!/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

Download code sample

Try it for free

close

sign up for free account

Already have an account?

no credit card required

About Us

Browshot is a web service to create real time web screenshots in a multitude of virtual devices, including mobile devices like the iPhone 3 & 4, iPad, Android Nexus, etc.

You can use the web dashboard, or our full-featured API.

  • Real time screenshots

  • 15+ mobile devices: iPhone, iPad, Android, etc.

  • 30+ desktop resolutions

  • Fast and reliable

  • Thumbnails of any size, any ratio

  • Full API, open-source libraries

Share