Class: Webstract::ScreenCapture
- Inherits:
-
Object
- Object
- Webstract::ScreenCapture
- Includes:
- Capybara::DSL
- Defined in:
- lib/webstract/screen_capture.rb
Instance Attribute Summary collapse
-
#accept_language ⇒ Object
readonly
Returns the value of attribute accept_language.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#capture(url, path, opts = {}) ⇒ Object
Captures a screenshot of
url
saving it topath
. -
#initialize(opts = {}) ⇒ ScreenCapture
constructor
A new instance of ScreenCapture.
- #start_session(&block) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ ScreenCapture
Returns a new instance of ScreenCapture.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/webstract/screen_capture.rb', line 9 def initialize(opts = {}) Webstract::ScreenshotBackend. @width = opts.fetch(:width, Webstract::ScreenshotBackend.width) @height = opts.fetch(:height, Webstract::ScreenshotBackend.height) @user_agent = opts.fetch(:user_agent, Webstract::ScreenshotBackend.user_agent) @accept_language = opts.fetch(:accept_language, Webstract::ScreenshotBackend.accept_language) # Browser settings page.driver.resize(@width, @height) page.driver.headers = { "User-Agent" => @user_agent, 'Accept-Language' => @accept_language } end |
Instance Attribute Details
#accept_language ⇒ Object (readonly)
Returns the value of attribute accept_language.
7 8 9 |
# File 'lib/webstract/screen_capture.rb', line 7 def accept_language @accept_language end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
7 8 9 |
# File 'lib/webstract/screen_capture.rb', line 7 def height @height end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/webstract/screen_capture.rb', line 7 def path @path end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
7 8 9 |
# File 'lib/webstract/screen_capture.rb', line 7 def user_agent @user_agent end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
7 8 9 |
# File 'lib/webstract/screen_capture.rb', line 7 def width @width end |
Instance Method Details
#capture(url, path, opts = {}) ⇒ Object
Captures a screenshot of url
saving it to path
.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/webstract/screen_capture.rb', line 32 def capture(url, path, opts = {}) begin # Default settings @width = opts.fetch(:width, 120) if opts[:width] @height = opts.fetch(:height, 90) if opts[:width] # 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 if page.driver.status_code.to_i == 200 || page.driver.status_code.to_i / 100 == 3 page.driver.save_screenshot(path, :full => true) else raise Webstract::Error.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}") end rescue Capybara::Poltergeist::BrowserError, Capybara::Poltergeist::DeadClient, Capybara::Poltergeist::TimeoutError, Errno::EPIPE => e # TODO: Handle Errno::EPIPE and Errno::ECONNRESET raise Webstract::Error.new("Capybara error: #{e..inspect}") end end |
#start_session(&block) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/webstract/screen_capture.rb', line 24 def start_session(&block) Capybara.reset_sessions! Capybara.current_session.instance_eval(&block) if block_given? @session_started = true self end |