websnap 0.1.2 → 0.1.3
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/Rakefile +5 -2
- data/lib/websnap/source.rb +1 -1
- data/lib/websnap/websnap.rb +72 -70
- data/spec/websnap_spec.rb +12 -12
- data/websnap.gemspec +1 -1
- metadata +3 -3
data/Rakefile
CHANGED
@@ -2,9 +2,9 @@ require 'rubygems'
|
|
2
2
|
require 'echoe'
|
3
3
|
|
4
4
|
require 'rake'
|
5
|
-
|
5
|
+
require 'rspec/core/rake_task'
|
6
6
|
|
7
|
-
Echoe.new("websnap", "0.1.
|
7
|
+
Echoe.new("websnap", "0.1.3") do |p|
|
8
8
|
p.author = "Francis Chong"
|
9
9
|
p.description = "Create snapshot of webpage"
|
10
10
|
p.url = "http://github.com/siuying/websnap"
|
@@ -12,3 +12,6 @@ Echoe.new("websnap", "0.1.2") do |p|
|
|
12
12
|
p.development_dependencies = ["rspec", "echoe", "mocha"]
|
13
13
|
p.runtime_dependencies = []
|
14
14
|
end
|
15
|
+
|
16
|
+
RSpec::Core::RakeTask.new('spec') do |t|
|
17
|
+
end
|
data/lib/websnap/source.rb
CHANGED
data/lib/websnap/websnap.rb
CHANGED
@@ -1,93 +1,95 @@
|
|
1
|
-
|
1
|
+
module WebSnap
|
2
|
+
class Snapper
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
class NoExecutableError < StandardError
|
5
|
+
def initialize
|
6
|
+
super('Could not locate wkhtmltoimage-proxy executable')
|
7
|
+
end
|
6
8
|
end
|
7
|
-
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
class ImproperSourceError < StandardError
|
11
|
+
def initialize(msg)
|
12
|
+
super("Improper Source: #{msg}")
|
13
|
+
end
|
12
14
|
end
|
13
|
-
end
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
attr_accessor :source, :stylesheets
|
17
|
+
attr_reader :options
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
def initialize(url_file_or_html, options={})
|
20
|
+
@source = Source.new(url_file_or_html)
|
20
21
|
|
21
|
-
|
22
|
+
@stylesheets = []
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
default_options = {
|
25
|
+
:'crop-h' => '768',
|
26
|
+
:'crop-w' => '1024',
|
27
|
+
:'crop-x' => '0',
|
28
|
+
:'crop-y' => '0',
|
29
|
+
:'format' => 'jpg'
|
30
|
+
}
|
31
|
+
@options = normalize_options(default_options.merge(options))
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
raise NoExecutableError.new if wkhtmltoimage.nil? || wkhtmltoimage == ''
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
def command
|
37
|
+
args = [wkhtmltoimage]
|
38
|
+
args += @options.to_a.flatten.compact
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
if @source.html?
|
41
|
+
args << '-' # Get HTML from stdin
|
42
|
+
else
|
43
|
+
args << @source.to_s
|
44
|
+
end
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
args << '-' # Read PDF from stdout
|
47
|
+
args.join(' ')
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
50
|
+
def to_bytes
|
51
|
+
img = IO.popen(command, "w+")
|
52
|
+
img.puts(@source.to_s) if @source.html?
|
53
|
+
img.close_write
|
54
|
+
result = img.gets(nil)
|
55
|
+
img.close_read
|
56
|
+
return result
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
def to_file(path)
|
60
|
+
File.open(path,'w') {|file| file << self.to_bytes}
|
61
|
+
end
|
61
62
|
|
62
|
-
|
63
|
+
protected
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
65
|
+
def wkhtmltoimage
|
66
|
+
@wkhtmltoimage ||= `which wkhtmltoimage-proxy`.chomp
|
67
|
+
end
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
69
|
+
def normalize_options(options)
|
70
|
+
normalized_options = {}
|
71
|
+
options.each do |key, value|
|
72
|
+
next if !value
|
73
|
+
normalized_key = "--#{normalize_arg key}"
|
74
|
+
normalized_options[normalized_key] = normalize_value(value)
|
75
|
+
end
|
76
|
+
normalized_options
|
74
77
|
end
|
75
|
-
normalized_options
|
76
|
-
end
|
77
78
|
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
def normalize_arg(arg)
|
80
|
+
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
|
81
|
+
end
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
83
|
+
def normalize_value(value)
|
84
|
+
case value
|
85
|
+
when TrueClass
|
86
|
+
nil
|
87
|
+
when String
|
88
|
+
value.match(/\s/) ? "\"#{value}\"" : value
|
89
|
+
else
|
90
|
+
value
|
91
|
+
end
|
90
92
|
end
|
91
|
-
end
|
92
93
|
|
94
|
+
end
|
93
95
|
end
|
data/spec/websnap_spec.rb
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe WebSnap do
|
3
|
+
describe WebSnap::Snapper do
|
4
4
|
|
5
5
|
context "initialization" do
|
6
6
|
it "should accept HTML as the source" do
|
7
|
-
websnap = WebSnap.new('<h1>Oh Hai</h1>')
|
7
|
+
websnap = WebSnap::Snapper.new('<h1>Oh Hai</h1>')
|
8
8
|
websnap.source.should be_html
|
9
9
|
websnap.source.to_s.should == '<h1>Oh Hai</h1>'
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should accept a URL as the source" do
|
13
|
-
websnap = WebSnap.new('http://google.com')
|
13
|
+
websnap = WebSnap::Snapper.new('http://google.com')
|
14
14
|
websnap.source.should be_url
|
15
15
|
websnap.source.to_s.should == 'http://google.com'
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should accept a File as the source" do
|
19
19
|
file_path = File.join(SPEC_ROOT,'fixtures','google.html')
|
20
|
-
websnap = WebSnap.new(File.new(file_path))
|
20
|
+
websnap = WebSnap::Snapper.new(File.new(file_path))
|
21
21
|
websnap.source.should be_file
|
22
22
|
websnap.source.to_s.should == file_path
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should parse the options into a cmd line friedly format" do
|
26
|
-
websnap = WebSnap.new('html', :'crop-w' => '800', :'crop-h' => '600')
|
26
|
+
websnap = WebSnap::Snapper.new('html', :'crop-w' => '800', :'crop-h' => '600')
|
27
27
|
websnap.options.should have_key('--crop-w')
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should provide default options" do
|
31
|
-
websnap = WebSnap.new('<h1>Oh Hai</h1>')
|
31
|
+
websnap = WebSnap::Snapper.new('<h1>Oh Hai</h1>')
|
32
32
|
['--crop-w', '--crop-h', '--scale-w', '--scale-h', '--format'].each do |option|
|
33
33
|
websnap.options.should have_key(option)
|
34
34
|
end
|
@@ -37,30 +37,30 @@ describe WebSnap do
|
|
37
37
|
|
38
38
|
context "command" do
|
39
39
|
it "should contstruct the correct command" do
|
40
|
-
websnap = WebSnap.new('html', :'encoding' => 'Big5')
|
40
|
+
websnap = WebSnap::Snapper.new('html', :'encoding' => 'Big5')
|
41
41
|
websnap.command.should include('--encoding Big5')
|
42
42
|
end
|
43
43
|
|
44
44
|
it "read the source from stdin if it is html" do
|
45
|
-
websnap = WebSnap.new('html')
|
45
|
+
websnap = WebSnap::Snapper.new('html')
|
46
46
|
websnap.command.should match(/ - -$/)
|
47
47
|
end
|
48
48
|
|
49
49
|
it "specify the URL to the source if it is a url" do
|
50
|
-
websnap = WebSnap.new('http://google.com')
|
50
|
+
websnap = WebSnap::Snapper.new('http://google.com')
|
51
51
|
websnap.command.should match(/ http:\/\/google\.com -$/)
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should specify the path to the source if it is a file" do
|
55
55
|
file_path = File.join(SPEC_ROOT,'fixtures','google.html')
|
56
|
-
websnap = WebSnap.new(File.new(file_path))
|
56
|
+
websnap = WebSnap::Snapper.new(File.new(file_path))
|
57
57
|
websnap.command.should match(/ #{file_path} -$/)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
context "#to_bytes" do
|
62
62
|
it "should generate a PDF of the HTML" do
|
63
|
-
websnap = WebSnap.new('html')
|
63
|
+
websnap = WebSnap::Snapper.new('html')
|
64
64
|
websnap.expects(:to_bytes).returns('PNG')
|
65
65
|
png = websnap.to_bytes
|
66
66
|
png.should match(/PNG/)
|
@@ -78,7 +78,7 @@ describe WebSnap do
|
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should create a file with the PNG as content" do
|
81
|
-
websnap = WebSnap.new('html')
|
81
|
+
websnap = WebSnap::Snapper.new('html')
|
82
82
|
websnap.expects(:to_bytes).returns('PNG')
|
83
83
|
|
84
84
|
file = websnap.to_file(@file_path)
|
data/websnap.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: websnap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Francis Chong
|