Class: Webshot::Screenshot
- Inherits:
-
Object
- Object
- Webshot::Screenshot
- Includes:
- Capybara::DSL, Singleton
- Defined in:
- lib/webshot/screenshot.rb
Instance Method Summary collapse
-
#capture(url, path, opts = {}) ⇒ Object
Captures a screenshot of
url
saving it topath
. -
#initialize(opts = {}) ⇒ Screenshot
constructor
A new instance of Screenshot.
- #start_session(&block) ⇒ Object
- #valid_status_code?(status_code, allowed_status_codes) ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ Screenshot
Returns a new instance of Screenshot.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/webshot/screenshot.rb', line 8 def initialize(opts = {}) Webshot. width = opts.fetch(:width, Webshot.width) height = opts.fetch(:height, Webshot.height) user_agent = opts.fetch(:user_agent, Webshot.user_agent) # Browser settings page.driver.resize(width, height) page.driver.headers = { "User-Agent" => user_agent } end |
Instance Method Details
#capture(url, path, opts = {}) ⇒ Object
Captures a screenshot of url
saving it to path
.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/webshot/screenshot.rb', line 36 def capture(url, path, opts = {}) begin # Default settings width = opts.fetch(:width, 120) height = opts.fetch(:height, 90) gravity = opts.fetch(:gravity, "north") quality = opts.fetch(:quality, 85) allowed_status_codes = opts.fetch(:allowed_status_codes, []) # Reset session before visiting url Capybara.reset_sessions! unless @session_started @session_started = false # Open page visit url # Timeout sleep opts[:timeout] if opts[:timeout] # Check response code status_code = page.driver.status_code.to_i unless valid_status_code?(status_code, allowed_status_codes) fail WebshotError, "Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}" end tmp = Tempfile.new(["webshot", ".png"]) tmp.close begin # Save screenshot to file page.driver.save_screenshot(tmp.path, :full => true) # Resize screenshot thumb = MiniMagick::Image.open(tmp.path) if block_given? # Customize MiniMagick options yield thumb else thumb. do |c| c.thumbnail "#{width}x" c.background "white" c.extent "#{width}x#{height}" c.gravity gravity c.quality quality end end # Save thumbnail thumb.write path thumb ensure tmp.unlink end rescue Capybara::Poltergeist::StatusFailError, Capybara::Poltergeist::BrowserError, Capybara::Poltergeist::DeadClient, Capybara::Poltergeist::TimeoutError, Errno::EPIPE => e # TODO: Handle Errno::EPIPE and Errno::ECONNRESET raise WebshotError.new("Capybara error: #{e..inspect}") end end |
#start_session(&block) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/webshot/screenshot.rb', line 21 def start_session(&block) Capybara.reset_sessions! Capybara.current_session.instance_eval(&block) if block_given? @session_started = true self end |
#valid_status_code?(status_code, allowed_status_codes) ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/webshot/screenshot.rb', line 28 def valid_status_code?(status_code, allowed_status_codes) return true if status_code == 200 return true if status_code / 100 == 3 return true if allowed_status_codes.include?(status_code) false end |