Class: Gastly::Screenshot

Inherits:
Object
  • Object
show all
Defined in:
lib/gastly/screenshot.rb

Constant Summary collapse

SCRIPT_PATH =
File.expand_path('../script.js', __FILE__)
DEFAULT_TIMEOUT =
0
DEFAULT_BROWSER_WIDTH =
1440
DEFAULT_BROWSER_HEIGHT =
900
DEFAULT_FILE_FORMAT =
'.png'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, **kwargs) ⇒ Screenshot

Returns a new instance of Screenshot.

Parameters:

  • url (String)

    The full url to the site



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gastly/screenshot.rb', line 14

def initialize(url, **kwargs)
  hash = Gastly::Utils::Hash.new(kwargs)
  hash.assert_valid_keys(:timeout, :browser_width, :browser_height, :selector, :cookies, :proxy_host, :proxy_port, :phantomjs_options)

  @url = url
  @cookies = kwargs.delete(:cookies)

  @image = MiniMagick::Image.create(DEFAULT_FILE_FORMAT, false) # Disable validation

  kwargs.each { |key, value| instance_variable_set(:"@#{key}", value) }
end

Instance Attribute Details

#browser_height=(value) ⇒ Object (writeonly)

Sets the attribute browser_height

Parameters:

  • value

    the value to set the attribute browser_height to.



10
11
12
# File 'lib/gastly/screenshot.rb', line 10

def browser_height=(value)
  @browser_height = value
end

#browser_width=(value) ⇒ Object (writeonly)

Sets the attribute browser_width

Parameters:

  • value

    the value to set the attribute browser_width to.



10
11
12
# File 'lib/gastly/screenshot.rb', line 10

def browser_width=(value)
  @browser_width = value
end

#cookiesObject

Returns the value of attribute cookies.



11
12
13
# File 'lib/gastly/screenshot.rb', line 11

def cookies
  @cookies
end

#imageObject (readonly)

Returns the value of attribute image.



9
10
11
# File 'lib/gastly/screenshot.rb', line 9

def image
  @image
end

#phantomjs_optionsObject

Returns the value of attribute phantomjs_options.



11
12
13
# File 'lib/gastly/screenshot.rb', line 11

def phantomjs_options
  @phantomjs_options
end

#proxy_hostObject

Returns the value of attribute proxy_host.



11
12
13
# File 'lib/gastly/screenshot.rb', line 11

def proxy_host
  @proxy_host
end

#proxy_portObject

Returns the value of attribute proxy_port.



11
12
13
# File 'lib/gastly/screenshot.rb', line 11

def proxy_port
  @proxy_port
end

#selectorObject

Returns the value of attribute selector.



11
12
13
# File 'lib/gastly/screenshot.rb', line 11

def selector
  @selector
end

#timeout=(value) ⇒ Object (writeonly)

Sets the attribute timeout

Parameters:

  • value

    the value to set the attribute timeout to.



10
11
12
# File 'lib/gastly/screenshot.rb', line 10

def timeout=(value)
  @timeout = value
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/gastly/screenshot.rb', line 11

def url
  @url
end

Instance Method Details

#captureGastly::Image

Capture image via PhantomJS and save to output file

Returns:



29
30
31
32
33
34
35
36
37
38
# File 'lib/gastly/screenshot.rb', line 29

def capture
  # This necessary to install PhantomJS via proxy
  Phantomjs.proxy_host = proxy_host if proxy_host
  Phantomjs.proxy_port = proxy_port if proxy_port

  output = Phantomjs.run(options, SCRIPT_PATH.to_s, *prepared_params)
  handle_output(output)

  Gastly::Image.new(image)
end