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.
-
#basic_auth ⇒ Object
readonly
Returns the value of attribute basic_auth.
-
#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
urlsaving it topath. -
#initialize(opts = {}) ⇒ ScreenCapture
constructor
A new instance of ScreenCapture.
- #start_session(&block) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ ScreenCapture
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 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) @accept_language = opts.fetch(:accept_language, Webstract::ScreenshotBackend.accept_language) @basic_auth = opts.fetch(:basic_auth, Webstract::ScreenshotBackend.basic_auth) ua = opts.fetch(:user_agent, Webstract::ScreenshotBackend.user_agent) @user_agent = Webstract::ScreenshotBackend::USER_AGENTS[ua] if ua.is_a?(Symbol) # Browser settings page.driver.resize(@width, @height) page.driver.headers = { "User-Agent" => @user_agent, 'Accept-Language' => @accept_language } page.driver.(basic_auth[:username], basic_auth[:password]) if basic_auth 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 |
#basic_auth ⇒ Object (readonly)
Returns the value of attribute basic_auth.
7 8 9 |
# File 'lib/webstract/screen_capture.rb', line 7 def basic_auth @basic_auth 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.
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 |
# File 'lib/webstract/screen_capture.rb', line 37 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 .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::Errors::PageError.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}") end rescue ::Poltergeist::BrowserError, ::Poltergeist::DeadClient, ::Poltergeist::TimeoutError, Errno::EPIPE => e # TODO: Handle Errno::EPIPE and Errno::ECONNRESET raise Webstract::Errors::CaptureError.new("Capybara error: #{e.message.inspect}") end end |
#start_session(&block) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/webstract/screen_capture.rb', line 29 def start_session(&block) .reset_sessions! .current_session.instance_eval(&block) if block_given? @session_started = true self end |