capit 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/.rvmrc +1 -0
- data/.yardopts +1 -0
- data/Gemfile +2 -0
- data/README.md +0 -5
- data/Rakefile +7 -1
- data/capit.gemspec +1 -1
- data/lib/capit/capture.rb +41 -15
- data/lib/capit/version.rb +1 -1
- data/spec/capit/capture_spec.rb +67 -64
- data/spec/capit_spec.rb +4 -5
- data/spec/spec_helper.rb +5 -1
- data/spec/support/deferred_garbage_collection.rb +26 -0
- data/spec/support/temporary_folder_helper.rb +9 -0
- data/spec/support/with_constants.rb +52 -0
- metadata +10 -2
data/.gitignore
CHANGED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm ruby-1.9.2
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --title "CapIt Documentation" --protected
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -33,10 +33,5 @@ Usage
|
|
33
33
|
require 'capit'
|
34
34
|
capit = CapIt::Capture("http://www.github.com")
|
35
35
|
|
36
|
-
Notes
|
37
|
-
-----
|
38
|
-
Don't use this yet.
|
39
|
-
|
40
36
|
[1]: http://cutycapt.sourceforge.net/ "CutyCapt"
|
41
37
|
[2]: http://daveelkins.com/2009/04/10/setting-up-headless-xserver-and-cutycapt-on-ubuntu/ "Setting up Headless XServer and CutyCapt on Ubuntu"
|
42
|
-
|
data/Rakefile
CHANGED
data/capit.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.authors = ["Ezekiel Templin"]
|
11
11
|
s.email = ["ezkl@me.com"]
|
12
|
-
s.date = %q{2011-03-
|
12
|
+
s.date = %q{2011-03-27}
|
13
13
|
s.homepage = "http://github.com/meadvillerb/capit"
|
14
14
|
s.summary = %q{Easy screen captures with the help of CutyCapt}
|
15
15
|
s.description = %q{CapIt provides a simple Ruby interface to Björn Höhrmann's CutyCapt.}
|
data/lib/capit/capture.rb
CHANGED
@@ -13,6 +13,15 @@ module CapIt
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
# This class provides the screen capture functionality.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# capit = CapIt::Capture.new("http://mdvlrb.com", :filename => "mdvlrb.png")
|
20
|
+
# capit.max_wait = 5000
|
21
|
+
# capit.folder = "/home/user/screenshots"
|
22
|
+
# capit.capture
|
23
|
+
# capit.output = "/home/user/screenshots/mdvlrb.png"
|
24
|
+
#
|
16
25
|
class Capture
|
17
26
|
|
18
27
|
# All extensions CutyCapt can use to infer format of output
|
@@ -21,22 +30,25 @@ module CapIt
|
|
21
30
|
# The URL of the page to be captured
|
22
31
|
attr_reader :url
|
23
32
|
|
24
|
-
|
25
|
-
attr_accessor :folder, :filename, :user_agent, :max_wait,
|
26
|
-
:delay, :output
|
33
|
+
attr_accessor :folder, :filename, :user_agent, :max_wait, :delay, :output
|
27
34
|
|
28
35
|
# Initialize a new Capture
|
36
|
+
# @param [String] url The URL we want to capture.
|
37
|
+
# @param [Hash] options Various options we can set.
|
29
38
|
#
|
30
|
-
# @
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
39
|
+
# @return [Object]
|
40
|
+
#
|
41
|
+
# @see CapIt::Capture#cutycapt_installed?
|
42
|
+
# @see CapIt::Capture#valid_extension?
|
43
|
+
#
|
44
|
+
# @raise CutyCaptError
|
45
|
+
# @raise InvalidExtensionError
|
34
46
|
#
|
35
47
|
def initialize url, options = {}
|
36
48
|
cutycapt_installed?
|
37
49
|
@url = url
|
38
50
|
@folder = options[:folder] || Dir.pwd
|
39
|
-
@filename = options[:filename] || "capit.
|
51
|
+
@filename = options[:filename] || "capit.jpeg"
|
40
52
|
@user_agent = options[:user_agent] || "CapIt! [http://github.com/meadvillerb/capit]"
|
41
53
|
@max_wait = options[:max_wait] || 15000
|
42
54
|
@delay = options[:delay]
|
@@ -52,27 +64,36 @@ module CapIt
|
|
52
64
|
#
|
53
65
|
# @return [String, false]
|
54
66
|
#
|
67
|
+
# @see CapIt::Capture#successful?
|
68
|
+
#
|
55
69
|
def capture
|
56
70
|
`#{capture_command}`
|
57
71
|
successful?
|
58
72
|
end
|
59
73
|
|
74
|
+
# Overloads #filename= to ensure that filenames have valid extensions.
|
75
|
+
#
|
76
|
+
# @param [String] filename
|
77
|
+
# @see CapIt::Capture#valid_extension?
|
78
|
+
#
|
60
79
|
def filename=(filename)
|
61
80
|
valid_extension?(filename)
|
62
81
|
@filename = filename
|
63
82
|
end
|
64
83
|
|
84
|
+
# Compares filename against EXTENSIONS. Raises InvalidExtensionError if the extension is invalid.
|
85
|
+
#
|
86
|
+
# @param [String] filename
|
87
|
+
#
|
65
88
|
def valid_extension?(filename)
|
66
|
-
|
67
|
-
raise InvalidExtensionError, "You must supply a valid extension!"
|
68
|
-
end
|
89
|
+
raise InvalidExtensionError, "You must supply a valid extension!" if filename[EXTENSIONS].nil?
|
69
90
|
end
|
70
91
|
|
71
92
|
# Determines whether the capture was successful
|
72
93
|
# by checking for the existence of the output file.
|
73
|
-
# Sets
|
94
|
+
# Sets CapIt::Capture#output if true.
|
74
95
|
#
|
75
|
-
# @return [
|
96
|
+
# @return [String, false]
|
76
97
|
#
|
77
98
|
def successful?
|
78
99
|
if FileTest.exists?("#{@folder}/#{@filename}")
|
@@ -85,8 +106,7 @@ module CapIt
|
|
85
106
|
# @return [String]
|
86
107
|
#
|
87
108
|
def capture_command
|
88
|
-
cmd = "CutyCapt"
|
89
|
-
cmd += " --url='#{@url}'"
|
109
|
+
cmd = "CutyCapt --url='#{@url}'"
|
90
110
|
cmd += " --out='#{@folder}/#{@filename}'"
|
91
111
|
cmd += " --max-wait=#{@max_wait}"
|
92
112
|
cmd += " --delay=#{@delay}" if @delay
|
@@ -104,6 +124,7 @@ module CapIt
|
|
104
124
|
# Not foolproof, but good enough for the time being.
|
105
125
|
#
|
106
126
|
# @return [Symbol]
|
127
|
+
# @raise [InvalidOSError]
|
107
128
|
#
|
108
129
|
def determine_os
|
109
130
|
case RUBY_PLATFORM
|
@@ -113,6 +134,11 @@ module CapIt
|
|
113
134
|
end
|
114
135
|
end
|
115
136
|
|
137
|
+
# Checks to see if CutyCapt is available in PATH.
|
138
|
+
# Raises CutyCaptError if not.
|
139
|
+
#
|
140
|
+
# @return
|
141
|
+
#
|
116
142
|
def cutycapt_installed?
|
117
143
|
raise CutyCaptError, "CutyCapt must be installed and available on PATH" if `which CutyCapt`.empty?
|
118
144
|
end
|
data/lib/capit/version.rb
CHANGED
data/spec/capit/capture_spec.rb
CHANGED
@@ -1,92 +1,95 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
3
|
|
4
|
-
describe CapIt
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@capit = CapIt::Capture("http://mdvlrb.com/", :filename => 'mdvlrb.png', :folder => @folder).should == @folder + "/mdvlrb.png"
|
10
|
-
`rm -rf #{@folder}`
|
11
|
-
end
|
4
|
+
describe CapIt do
|
5
|
+
it "should have a Convenience Method" do
|
6
|
+
@folder = setup_temporary_folder
|
7
|
+
@capit = CapIt::Capture("http://mdvlrb.com/", :filename => 'mdvlrb.png', :folder => @folder).should == @folder + "/mdvlrb.png"
|
8
|
+
destroy_temporary_folder(@folder)
|
12
9
|
end
|
13
|
-
|
14
|
-
describe
|
15
|
-
|
10
|
+
|
11
|
+
describe CapIt::Capture do
|
12
|
+
before { @capit = CapIt::Capture.new("http://mdvlrb.com/") }
|
13
|
+
subject { @capit }
|
16
14
|
|
17
15
|
describe "#initialize" do
|
18
|
-
it
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
it { should respond_to :url, :folder, :filename, :user_agent, :max_wait, :delay }
|
17
|
+
|
18
|
+
context "defaults" do
|
19
|
+
specify { @capit.folder.should == Dir.pwd }
|
20
|
+
specify { @capit.filename.should == "capit.jpeg" }
|
21
|
+
specify { @capit.user_agent.should == "CapIt! [http://github.com/meadvillerb/capit]" }
|
22
|
+
specify { @capit.max_wait.should == 15000 }
|
23
23
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
end
|
25
|
+
|
26
|
+
describe ":filename" do
|
27
|
+
context "when valid" do
|
28
|
+
before { @capit.filename = "mdvlrb.png" }
|
29
|
+
specify { @capit.filename.should == "mdvlrb.png" }
|
27
30
|
end
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
32
|
+
context "when invalid" do
|
33
|
+
it "should raise an error" do
|
34
|
+
expect { @capit.filename = "capit.foo" }.to raise_error(/valid extension/)
|
35
|
+
end
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
39
|
+
|
36
40
|
describe "#capture" do
|
37
41
|
before(:all) do
|
38
|
-
@folder =
|
39
|
-
@
|
40
|
-
`mkdir #{@folder}`
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should return the full path to the screenshot" do
|
44
|
-
@capit.capture.should == @folder + "/mdvlrb.jpeg"
|
42
|
+
@folder = setup_temporary_folder
|
43
|
+
@capture = CapIt::Capture.new("http://mdvlrb.com", :folder => @folder)
|
45
44
|
end
|
46
|
-
|
47
|
-
|
48
|
-
@
|
45
|
+
|
46
|
+
context "when screen capture is successful" do
|
47
|
+
specify { @capture.capture.should == @folder + "/capit.jpeg" }
|
48
|
+
specify { @capture.output.should == @folder + "/capit.jpeg" }
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
after(:all) do
|
52
|
-
|
52
|
+
destroy_temporary_folder(@folder)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
describe "#capture_command" do
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@capit.capture_command.should match /^CutyCapt/
|
57
|
+
before { @capit = CapIt::Capture.new("http://mdvlrb.com/") }
|
58
|
+
subject { @capit }
|
59
|
+
|
60
|
+
context "when platform is Linux" do
|
61
|
+
it "should add the xvfb prefix" do
|
62
|
+
with_constants :RUBY_PLATFORM => "linux" do
|
63
|
+
@capit.capture_command.should match /^xvfb/
|
64
|
+
end
|
65
|
+
end
|
67
66
|
end
|
68
|
-
|
67
|
+
|
68
|
+
context "when platform is Mac" do
|
69
|
+
it "should not add the xvfb prefix" do
|
70
|
+
with_constants :RUBY_PLATFORM => "darwin" do
|
71
|
+
@capit.capture_command.should match /^CutyCapt/
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
69
75
|
end
|
70
76
|
|
71
|
-
describe "Errors" do
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
+
describe "Fatal Errors" do
|
78
|
+
context "when platform is unknown" do
|
79
|
+
it "should raise an error" do
|
80
|
+
with_constants :RUBY_PLATFORM => "mingw" do
|
81
|
+
expect { @capit.determine_os }.should raise_error(/platforms/)
|
82
|
+
end
|
83
|
+
end
|
77
84
|
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
it "should not accept filenames without a valid extension" do
|
86
|
-
expect { CapIt::Capture.new("http://mdvlrb.com/", :filename => 'capit.foo') }.to raise_error(/valid extension/)
|
87
|
-
expect { subject.filename = "capit.foo" }.to raise_error(/valid extension/)
|
85
|
+
|
86
|
+
context "when CutyCapt executable is unavailable" do
|
87
|
+
it "should raise an error" do
|
88
|
+
with_environment_variable 'PATH' => "" do
|
89
|
+
expect { CapIt::Capture.new("http://mdvlrb.com/") }.to raise_error(/CutyCapt/)
|
90
|
+
end
|
91
|
+
end
|
88
92
|
end
|
89
|
-
end
|
90
|
-
|
93
|
+
end
|
91
94
|
end
|
92
95
|
end
|
data/spec/capit_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -3,8 +3,12 @@ require 'simplecov'
|
|
3
3
|
|
4
4
|
SimpleCov.start do
|
5
5
|
coverage_dir 'coverage'
|
6
|
-
add_filter '
|
6
|
+
add_filter 'spec/'
|
7
7
|
end
|
8
8
|
|
9
9
|
require 'rspec'
|
10
|
+
require 'support/with_constants'
|
11
|
+
require 'support/deferred_garbage_collection' # http://makandra.com/notes/950-speed-up-rspec-by-deferring-garbage-collection
|
12
|
+
require 'support/temporary_folder_helper'
|
13
|
+
|
10
14
|
require 'capit'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class DeferredGarbageCollection
|
2
|
+
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 10.0).to_f
|
3
|
+
@@last_gc_run = Time.now
|
4
|
+
def self.start
|
5
|
+
GC.disable if DEFERRED_GC_THRESHOLD > 0
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.reconsider
|
9
|
+
if DEFERRED_GC_THRESHOLD > 0 && Time.now - @@last_gc_run >= DEFERRED_GC_THRESHOLD
|
10
|
+
GC.enable
|
11
|
+
GC.start
|
12
|
+
GC.disable
|
13
|
+
@@last_gc_run = Time.now
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.before(:all) do
|
20
|
+
DeferredGarbageCollection.start
|
21
|
+
end
|
22
|
+
|
23
|
+
config.after(:all) do
|
24
|
+
DeferredGarbageCollection.reconsider
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Borrowing Rails monkeypatch: http://api.rubyonrails.org/classes/Kernel.html
|
2
|
+
# Make sure we don't redefine it if its already defined.
|
3
|
+
#
|
4
|
+
unless Kernel.method_defined?(:silence_warnings)
|
5
|
+
module Kernel
|
6
|
+
def silence_warnings
|
7
|
+
with_warnings(nil) { yield }
|
8
|
+
end
|
9
|
+
|
10
|
+
def with_warnings(flag)
|
11
|
+
old_verbose, $VERBOSE = $VERBOSE, flag
|
12
|
+
yield
|
13
|
+
ensure
|
14
|
+
$VERBOSE = old_verbose
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# From: http://digitaldumptruck.jotabout.com/?p=551
|
20
|
+
def with_constants(constants, &block)
|
21
|
+
saved_constants = {}
|
22
|
+
|
23
|
+
constants.each do |constant, val|
|
24
|
+
saved_constants[ constant ] = Object.const_get( constant )
|
25
|
+
Kernel::silence_warnings { Object.const_set( constant, val ) }
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
block.call
|
30
|
+
ensure
|
31
|
+
constants.each do |constant, val|
|
32
|
+
Kernel::silence_warnings { Object.const_set( constant, saved_constants[ constant ] ) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def with_environment_variable(variables, &block)
|
38
|
+
saved_variables = {}
|
39
|
+
|
40
|
+
variables.each do |variable, value|
|
41
|
+
saved_variables[variable] = ENV[variable]
|
42
|
+
Kernel::silence_warnings { ENV[variable] = value }
|
43
|
+
end
|
44
|
+
|
45
|
+
begin
|
46
|
+
block.call
|
47
|
+
ensure
|
48
|
+
variables.each do |variable, value|
|
49
|
+
Kernel::silence_warnings { ENV[variable] = saved_variables[variable] }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: capit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ezekiel Templin
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-27 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -92,6 +92,8 @@ extra_rdoc_files:
|
|
92
92
|
files:
|
93
93
|
- .gemtest
|
94
94
|
- .gitignore
|
95
|
+
- .rvmrc
|
96
|
+
- .yardopts
|
95
97
|
- Gemfile
|
96
98
|
- Guardfile
|
97
99
|
- LICENSE
|
@@ -104,6 +106,9 @@ files:
|
|
104
106
|
- spec/capit/capture_spec.rb
|
105
107
|
- spec/capit_spec.rb
|
106
108
|
- spec/spec_helper.rb
|
109
|
+
- spec/support/deferred_garbage_collection.rb
|
110
|
+
- spec/support/temporary_folder_helper.rb
|
111
|
+
- spec/support/with_constants.rb
|
107
112
|
has_rdoc: true
|
108
113
|
homepage: http://github.com/meadvillerb/capit
|
109
114
|
licenses: []
|
@@ -136,3 +141,6 @@ test_files:
|
|
136
141
|
- spec/capit/capture_spec.rb
|
137
142
|
- spec/capit_spec.rb
|
138
143
|
- spec/spec_helper.rb
|
144
|
+
- spec/support/deferred_garbage_collection.rb
|
145
|
+
- spec/support/temporary_folder_helper.rb
|
146
|
+
- spec/support/with_constants.rb
|