Class: CapIt::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/capit/capture.rb

Overview

This class provides the screen capture functionality.

Examples:

capit = CapIt::Capture.new("http://mdvlrb.com", :filename => "mdvlrb.png")
capit.max_wait = 5000
capit.folder = "/home/user/screenshots"
capit.capture
capit.output = "/home/user/screenshots/mdvlrb.png"

Constant Summary collapse

EXTENSIONS =

All extensions CutyCapt can use to infer format of output

/\A[\w\-\.]+\.(svg|ps|pdf|itext|html|rtree|png|jpeg|jpg|mng|tiff|gif|bmp|ppm|xvm|xpm)\z/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Object

Initialize a new Capture

Parameters:

  • url (String)

    The URL we want to capture.

  • options (Hash) (defaults to: {})

    Various options we can set.

Raises:

  • CutyCaptError

  • InvalidExtensionError

See Also:



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/capit/capture.rb', line 47

def initialize url, options = {}
  @url            = url              
  @folder         = options[:folder] || Dir.pwd
  @filename       = options[:filename] || "capit.jpeg"
  @user_agent     = options[:user_agent] || "CapIt! [http://github.com/meadvillerb/capit]"
  @max_wait       = options[:max_wait] || 15000
  @delay          = options[:delay]
  @cutycapt_path  = options[:cutycapt_path] || determine_cutycapt_path
  @min_width      = options[:min_width] || 1024
  @min_height     = options[:min_height] || 768
        
  valid_extension?(@filename)
end

Instance Attribute Details

#cutycapt_pathObject

Returns the value of attribute cutycapt_path.



33
34
35
# File 'lib/capit/capture.rb', line 33

def cutycapt_path
  @cutycapt_path
end

#delayObject

Returns the value of attribute delay.



33
34
35
# File 'lib/capit/capture.rb', line 33

def delay
  @delay
end

#filenameObject

Returns the value of attribute filename.



33
34
35
# File 'lib/capit/capture.rb', line 33

def filename
  @filename
end

#folderObject

Returns the value of attribute folder.



33
34
35
# File 'lib/capit/capture.rb', line 33

def folder
  @folder
end

#max_waitObject

Returns the value of attribute max_wait.



33
34
35
# File 'lib/capit/capture.rb', line 33

def max_wait
  @max_wait
end

#min_heightObject

Returns the value of attribute min_height.



33
34
35
# File 'lib/capit/capture.rb', line 33

def min_height
  @min_height
end

#min_widthObject

Returns the value of attribute min_width.



33
34
35
# File 'lib/capit/capture.rb', line 33

def min_width
  @min_width
end

#outputObject

Returns the value of attribute output.



33
34
35
# File 'lib/capit/capture.rb', line 33

def output
  @output
end

#urlObject (readonly)

The URL of the page to be captured



31
32
33
# File 'lib/capit/capture.rb', line 31

def url
  @url
end

#user_agentObject

Returns the value of attribute user_agent.



33
34
35
# File 'lib/capit/capture.rb', line 33

def user_agent
  @user_agent
end

Instance Method Details

#captureString, false

Performs the page capture.

Examples:

capit = CapIt::Capture.new("http://mdvlrb.com", :filename => "mdvlrb.png", :folder => "/home/user/screenshots")
capit.capture

Returns:

  • (String, false)

See Also:



71
72
73
74
# File 'lib/capit/capture.rb', line 71

def capture      
  `#{capture_command}`
  successful?
end

#capture_commandString

Produces the command used to run CutyCapt.

Returns:

  • (String)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/capit/capture.rb', line 110

def capture_command        
  cmd = "#{@cutycapt_path} --url='#{@url}'"
  cmd += " --out='#{@folder}/#{@filename}'"
  cmd += " --max-wait=#{@max_wait}"
  cmd += " --delay=#{@delay}" if @delay
  cmd += " --user-agent='#{@user_agent}'"
  cmd += " --min-width='#{@min_width}'"
  cmd += " --min-height='#{@min_height}'"

  
  if determine_os == :linux and check_xvfb
    xvfb = 'xvfb-run --server-args="-screen 99, 1024x768x24" '
    xvfb.concat(cmd)
  else
    cmd
  end        
end

#check_xvfbObject



128
129
130
# File 'lib/capit/capture.rb', line 128

def check_xvfb
  !(`which xvfb-run`.empty?)
end

#determine_cutycapt_pathObject



132
133
134
# File 'lib/capit/capture.rb', line 132

def determine_cutycapt_path
  `which CutyCapt`.strip or `which cutycapt`.strip
end

#determine_osSymbol

Uses RUBY_PLATFORM to determine the operating system. Not foolproof, but good enough for the time being.

Returns:

  • (Symbol)

Raises:



142
143
144
145
146
147
148
# File 'lib/capit/capture.rb', line 142

def determine_os
  case RUBY_PLATFORM
    when /darwin/i then :mac
    when /linux/i then :linux
    else raise InvalidOSError, "CapIt currently only works on the Mac and Linux platforms"
  end
end

#successful?String, false

Determines whether the capture was successful by checking for the existence of the output file. Sets CapIt::Capture#output if true.

Returns:

  • (String, false)


100
101
102
103
104
# File 'lib/capit/capture.rb', line 100

def successful?
  if FileTest.exists?("#{@folder}/#{@filename}")
    @output = "#{@folder}/#{@filename}"
  end  
end

#valid_extension?(filename) ⇒ Boolean

Compares filename against EXTENSIONS. Raises InvalidExtensionError if the extension is invalid.

Parameters:

  • filename (String)

Returns:

  • (Boolean)

Raises:



90
91
92
# File 'lib/capit/capture.rb', line 90

def valid_extension?(filename)
  raise InvalidExtensionError, "You must supply a valid extension!" if filename[EXTENSIONS].nil?
end