Class: Webstract::ScreenCapture

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/webstract/screen_capture.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.capybara_setup!
  @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_languageObject (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

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/webstract/screen_capture.rb', line 7

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/webstract/screen_capture.rb', line 7

def path
  @path
end

#user_agentObject (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

#widthObject (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.message.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