Class: CapIt::Capture
- Inherits:
-
Object
- Object
- CapIt::Capture
- Defined in:
- lib/capit/capture.rb
Overview
This class provides the screen capture functionality.
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
-
#cutycapt_path ⇒ Object
Returns the value of attribute cutycapt_path.
-
#delay ⇒ Object
Returns the value of attribute delay.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#folder ⇒ Object
Returns the value of attribute folder.
-
#max_wait ⇒ Object
Returns the value of attribute max_wait.
-
#min_height ⇒ Object
Returns the value of attribute min_height.
-
#min_width ⇒ Object
Returns the value of attribute min_width.
-
#output ⇒ Object
Returns the value of attribute output.
-
#url ⇒ Object
readonly
The URL of the page to be captured.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#capture ⇒ String, false
Performs the page capture.
-
#capture_command ⇒ String
Produces the command used to run CutyCapt.
- #check_xvfb ⇒ Object
- #determine_cutycapt_path ⇒ Object
-
#determine_os ⇒ Symbol
Uses RUBY_PLATFORM to determine the operating system.
-
#initialize(url, options = {}) ⇒ Object
constructor
Initialize a new Capture.
-
#successful? ⇒ String, false
Determines whether the capture was successful by checking for the existence of the output file.
-
#valid_extension?(filename) ⇒ Boolean
Compares filename against EXTENSIONS.
Constructor Details
#initialize(url, options = {}) ⇒ Object
Initialize a new Capture
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/capit/capture.rb', line 47 def initialize url, = {} @url = url @folder = [:folder] || Dir.pwd @filename = [:filename] || "capit.jpeg" @user_agent = [:user_agent] || "CapIt! [http://github.com/meadvillerb/capit]" @max_wait = [:max_wait] || 15000 @delay = [:delay] @cutycapt_path = [:cutycapt_path] || determine_cutycapt_path @min_width = [:min_width] || 1024 @min_height = [:min_height] || 768 valid_extension?(@filename) end |
Instance Attribute Details
#cutycapt_path ⇒ Object
Returns the value of attribute cutycapt_path.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def cutycapt_path @cutycapt_path end |
#delay ⇒ Object
Returns the value of attribute delay.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def delay @delay end |
#filename ⇒ Object
Returns the value of attribute filename.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def filename @filename end |
#folder ⇒ Object
Returns the value of attribute folder.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def folder @folder end |
#max_wait ⇒ Object
Returns the value of attribute max_wait.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def max_wait @max_wait end |
#min_height ⇒ Object
Returns the value of attribute min_height.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def min_height @min_height end |
#min_width ⇒ Object
Returns the value of attribute min_width.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def min_width @min_width end |
#output ⇒ Object
Returns the value of attribute output.
33 34 35 |
# File 'lib/capit/capture.rb', line 33 def output @output end |
#url ⇒ Object (readonly)
The URL of the page to be captured
31 32 33 |
# File 'lib/capit/capture.rb', line 31 def url @url end |
#user_agent ⇒ Object
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
#capture ⇒ String, false
Performs the page capture.
71 72 73 74 |
# File 'lib/capit/capture.rb', line 71 def capture `#{capture_command}` successful? end |
#capture_command ⇒ String
Produces the command used to run CutyCapt.
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_xvfb ⇒ Object
128 129 130 |
# File 'lib/capit/capture.rb', line 128 def check_xvfb !(`which xvfb-run`.empty?) end |
#determine_cutycapt_path ⇒ Object
132 133 134 |
# File 'lib/capit/capture.rb', line 132 def determine_cutycapt_path `which CutyCapt`.strip or `which cutycapt`.strip end |
#determine_os ⇒ Symbol
Uses RUBY_PLATFORM to determine the operating system. Not foolproof, but good enough for the time being.
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.
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.
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 |