shrimp 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +1 -4
- data/README.md +8 -2
- data/lib/shrimp/configuration.rb +4 -2
- data/lib/shrimp/phantom.rb +27 -1
- data/lib/shrimp/rasterize.js +5 -3
- data/lib/shrimp/version.rb +1 -1
- data/spec/shrimp/phantom_spec.rb +34 -15
- data/spec/shrimp/source_spec.rb +2 -1
- metadata +17 -35
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5f3a74b72ab74c2073b97e509e0d71dc3ac6dc90
|
4
|
+
data.tar.gz: 20c0b3d8b39e38c712cb2e4453bb17f85762de5f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 045699658ee607453c27708941711b857c21a98cc0606021a12c12ebbb2cab7652c56b7104894be4162ff45f8b5e2a5acfec82b1c1d420da40c2ee90b657f683
|
7
|
+
data.tar.gz: 95443979c5f10a47a2026181f168ead99a6d3fc50a0406f713a23035a6334149036eef8ecc6f4deb631db41ce2cec3df9a07d7d71896040a313813a1ba164ec9
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -19,9 +19,9 @@ Or install it yourself as:
|
|
19
19
|
$ gem install shrimp
|
20
20
|
|
21
21
|
|
22
|
-
###
|
22
|
+
### Phantomjs
|
23
23
|
|
24
|
-
See http://phantomjs.org/download.html on how to install
|
24
|
+
See http://phantomjs.org/download.html on how to install phantomjs
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
@@ -60,6 +60,12 @@ Shrimp.configure do |config|
|
|
60
60
|
# increase if you need to render very complex pages
|
61
61
|
# config.rendering_time = 1000
|
62
62
|
|
63
|
+
# change the viewport size. If you rendering pages that have
|
64
|
+
# flexible page width and height then you may need to set this
|
65
|
+
# to enforce a specific size
|
66
|
+
# config.viewport_width = 600
|
67
|
+
# config.viewport_height = 600
|
68
|
+
|
63
69
|
# the timeout for the phantomjs rendering process in ms
|
64
70
|
# this needs always to be higher than rendering_time
|
65
71
|
# config.rendering_timeout = 90000
|
data/lib/shrimp/configuration.rb
CHANGED
@@ -4,7 +4,7 @@ module Shrimp
|
|
4
4
|
attr_accessor :default_options
|
5
5
|
attr_writer :phantomjs
|
6
6
|
|
7
|
-
[:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout, :rendering_time, :command_config_file].each do |m|
|
7
|
+
[:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout, :rendering_time, :command_config_file, :viewport_width, :viewport_height].each do |m|
|
8
8
|
define_method("#{m}=") do |val|
|
9
9
|
@default_options[m]=val
|
10
10
|
end
|
@@ -19,7 +19,9 @@ module Shrimp
|
|
19
19
|
:tmpdir => Dir.tmpdir,
|
20
20
|
:rendering_timeout => 90000,
|
21
21
|
:rendering_time => 1000,
|
22
|
-
:command_config_file => File.expand_path('../config.json', __FILE__)
|
22
|
+
:command_config_file => File.expand_path('../config.json', __FILE__),
|
23
|
+
:viewport_width => 600,
|
24
|
+
:viewport_height => 600
|
23
25
|
}
|
24
26
|
end
|
25
27
|
|
data/lib/shrimp/phantom.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'uri'
|
2
2
|
require 'json'
|
3
|
+
require 'shellwords'
|
4
|
+
|
3
5
|
module Shrimp
|
4
6
|
class NoExecutableError < StandardError
|
5
7
|
def initialize
|
@@ -26,6 +28,13 @@ module Shrimp
|
|
26
28
|
attr_reader :options, :cookies, :result, :error
|
27
29
|
SCRIPT_FILE = File.expand_path('../rasterize.js', __FILE__)
|
28
30
|
|
31
|
+
class << self
|
32
|
+
def quote_arg(arg)
|
33
|
+
# "'#{arg.gsub("'", %q(\\\'))}'"
|
34
|
+
"'#{arg.gsub("'", %q('"'"'))}'"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
29
38
|
# Public: Runs the phantomjs binary
|
30
39
|
#
|
31
40
|
# Returns the stdout output of phantomjs
|
@@ -55,9 +64,25 @@ module Shrimp
|
|
55
64
|
cookie_file = dump_cookies
|
56
65
|
format, zoom, margin, orientation = options[:format], options[:zoom], options[:margin], options[:orientation]
|
57
66
|
rendering_time, timeout = options[:rendering_time], options[:rendering_timeout]
|
67
|
+
viewport_width, viewport_height = options[:viewport_width], options[:viewport_height]
|
58
68
|
@outfile ||= "#{options[:tmpdir]}/#{Digest::MD5.hexdigest((Time.now.to_i + rand(9001)).to_s)}.pdf"
|
59
69
|
command_config_file = "--config=#{options[:command_config_file]}"
|
60
|
-
[
|
70
|
+
[
|
71
|
+
Shrimp.configuration.phantomjs,
|
72
|
+
command_config_file,
|
73
|
+
SCRIPT_FILE,
|
74
|
+
@source.to_s.shellescape,
|
75
|
+
@outfile,
|
76
|
+
format,
|
77
|
+
zoom,
|
78
|
+
margin,
|
79
|
+
orientation,
|
80
|
+
cookie_file,
|
81
|
+
rendering_time,
|
82
|
+
timeout,
|
83
|
+
viewport_width,
|
84
|
+
viewport_height
|
85
|
+
].join(" ")
|
61
86
|
end
|
62
87
|
|
63
88
|
# Public: initializes a new Phantom Object
|
@@ -123,6 +148,7 @@ module Shrimp
|
|
123
148
|
end
|
124
149
|
|
125
150
|
private
|
151
|
+
|
126
152
|
def dump_cookies
|
127
153
|
host = @source.url? ? URI::parse(@source.to_s).host : "/"
|
128
154
|
json = @cookies.inject([]) { |a, (k, v)| a.push({ :name => k, :value => v, :domain => host }); a }.to_json
|
data/lib/shrimp/rasterize.js
CHANGED
@@ -6,6 +6,8 @@ var page = require('webpage').create(),
|
|
6
6
|
cookie_file = system.args[7] ,
|
7
7
|
render_time = system.args[8] || 10000 ,
|
8
8
|
time_out = system.args[9] || 90000 ,
|
9
|
+
viewport_width = system.args[10] || 600,
|
10
|
+
viewport_height= system.args[11] || 600,
|
9
11
|
cookies = {},
|
10
12
|
address, output, size, statusCode;
|
11
13
|
|
@@ -24,14 +26,14 @@ try {
|
|
24
26
|
phantom.cookiesEnabled = true;
|
25
27
|
phantom.cookies = cookies;
|
26
28
|
|
27
|
-
if (system.args.length < 3 || system.args.length >
|
28
|
-
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out]');
|
29
|
+
if (system.args.length < 3 || system.args.length > 12) {
|
30
|
+
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out] [viewport_width] [viewport_height]');
|
29
31
|
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
|
30
32
|
phantom.exit(1);
|
31
33
|
} else {
|
32
34
|
address = system.args[1];
|
33
35
|
output = system.args[2];
|
34
|
-
page.viewportSize = { width:
|
36
|
+
page.viewportSize = { width: viewport_width, height: viewport_height };
|
35
37
|
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
|
36
38
|
size = system.args[3].split('*');
|
37
39
|
page.paperSize = size.length === 2 ? { width:size[0], height:size[1], margin:'0px' }
|
data/lib/shrimp/version.rb
CHANGED
data/spec/shrimp/phantom_spec.rb
CHANGED
@@ -10,21 +10,31 @@ def valid_pdf(io)
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def testfile
|
14
|
-
File.expand_path('../test_file.html', __FILE__)
|
15
|
-
end
|
16
|
-
|
17
|
-
Shrimp.configure do |config|
|
18
|
-
config.rendering_time = 1000
|
19
|
-
end
|
20
|
-
|
21
13
|
describe Shrimp::Phantom do
|
14
|
+
let(:testfile) { File.expand_path('../test_file.html', __FILE__) }
|
15
|
+
|
22
16
|
before do
|
23
|
-
Shrimp.configure
|
24
|
-
config.rendering_time = 1000
|
25
|
-
end
|
17
|
+
Shrimp.configure { |config| config.rendering_time = 1000 }
|
26
18
|
end
|
27
19
|
|
20
|
+
# describe ".quote_arg" do
|
21
|
+
# subject { described_class }
|
22
|
+
|
23
|
+
# let(:arg) { "test" }
|
24
|
+
|
25
|
+
# it "wraps the argument with single quotes" do
|
26
|
+
# subject.quote_arg(arg).should eq "'test'"
|
27
|
+
# end
|
28
|
+
|
29
|
+
# context "when the argument contains single quotes" do
|
30
|
+
# let(:arg) { "'te''st'" }
|
31
|
+
|
32
|
+
# it "escapes them" do
|
33
|
+
# %x(echo #{subject.quote_arg(arg)}).strip.should eq arg
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
|
28
38
|
it "should initialize attributes" do
|
29
39
|
phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => "2cm" }, { }, "#{Dir.tmpdir}/test.pdf")
|
30
40
|
phantom.source.to_s.should eq "file://#{testfile}"
|
@@ -54,8 +64,18 @@ describe Shrimp::Phantom do
|
|
54
64
|
phantom.cmd.should include "lib/shrimp/rasterize.js"
|
55
65
|
end
|
56
66
|
|
67
|
+
it "should properly escape arguments" do
|
68
|
+
malicious_uri = "file:///hello';shutdown"
|
69
|
+
bogus_phantom = Shrimp::Phantom.new(malicious_uri)
|
70
|
+
|
71
|
+
bogus_phantom.cmd.should_not include malicious_uri
|
72
|
+
|
73
|
+
Shrimp.configuration.stub(:phantomjs).and_return "echo"
|
74
|
+
%x(#{bogus_phantom.cmd}).strip.should include malicious_uri
|
75
|
+
end
|
76
|
+
|
57
77
|
context "rendering to a file" do
|
58
|
-
before
|
78
|
+
before do
|
59
79
|
phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => "2cm" }, { }, "#{Dir.tmpdir}/test.pdf")
|
60
80
|
@result = phantom.to_file
|
61
81
|
end
|
@@ -70,7 +90,7 @@ describe Shrimp::Phantom do
|
|
70
90
|
end
|
71
91
|
|
72
92
|
context "rendering to a pdf" do
|
73
|
-
before
|
93
|
+
before do
|
74
94
|
@phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => "2cm" }, { })
|
75
95
|
@result = @phantom.to_pdf("#{Dir.tmpdir}/test.pdf")
|
76
96
|
end
|
@@ -86,7 +106,7 @@ describe Shrimp::Phantom do
|
|
86
106
|
end
|
87
107
|
|
88
108
|
context "rendering to a String" do
|
89
|
-
before
|
109
|
+
before do
|
90
110
|
phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => "2cm" }, { })
|
91
111
|
@result = phantom.to_string("#{Dir.tmpdir}/test.pdf")
|
92
112
|
end
|
@@ -121,7 +141,6 @@ describe Shrimp::Phantom do
|
|
121
141
|
end
|
122
142
|
|
123
143
|
context "Error Bang!" do
|
124
|
-
|
125
144
|
it "should be unable to load the address" do
|
126
145
|
phantom = Shrimp::Phantom.new("file:///foo/bar")
|
127
146
|
expect { phantom.run! }.to raise_error Shrimp::RenderingError
|
data/spec/shrimp/source_spec.rb
CHANGED
@@ -7,8 +7,9 @@ describe Shrimp::Source do
|
|
7
7
|
source = Shrimp::Source.new("file:///test/test.html")
|
8
8
|
source.should be_url
|
9
9
|
end
|
10
|
+
|
10
11
|
it "should match http urls" do
|
11
|
-
source = Shrimp::Source.new("http
|
12
|
+
source = Shrimp::Source.new("http://test/test.html")
|
12
13
|
source.should be_url
|
13
14
|
end
|
14
15
|
end
|
metadata
CHANGED
@@ -1,84 +1,74 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shrimp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Manuel Kniep
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-03-21 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: json
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: 0.9.2
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: 0.9.2
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: 2.2.0
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: 2.2.0
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rack-test
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - ">="
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: 0.5.6
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - ">="
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: 0.5.6
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: rack
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
73
|
- - '='
|
84
74
|
- !ruby/object:Gem::Version
|
@@ -86,7 +76,6 @@ dependencies:
|
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
80
|
- - '='
|
92
81
|
- !ruby/object:Gem::Version
|
@@ -98,8 +87,8 @@ executables: []
|
|
98
87
|
extensions: []
|
99
88
|
extra_rdoc_files: []
|
100
89
|
files:
|
101
|
-
- .gitignore
|
102
|
-
- .travis.yml
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
103
92
|
- ChangeLog.md
|
104
93
|
- Gemfile
|
105
94
|
- LICENSE.txt
|
@@ -121,34 +110,27 @@ files:
|
|
121
110
|
- spec/spec_helper.rb
|
122
111
|
homepage: http://github.com/adeven/shrimp
|
123
112
|
licenses: []
|
113
|
+
metadata: {}
|
124
114
|
post_install_message:
|
125
115
|
rdoc_options: []
|
126
116
|
require_paths:
|
127
117
|
- lib
|
128
118
|
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
119
|
requirements:
|
131
|
-
- -
|
120
|
+
- - ">="
|
132
121
|
- !ruby/object:Gem::Version
|
133
122
|
version: '0'
|
134
|
-
segments:
|
135
|
-
- 0
|
136
|
-
hash: 2078211269578037741
|
137
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
-
none: false
|
139
124
|
requirements:
|
140
|
-
- -
|
125
|
+
- - ">="
|
141
126
|
- !ruby/object:Gem::Version
|
142
127
|
version: '0'
|
143
|
-
segments:
|
144
|
-
- 0
|
145
|
-
hash: 2078211269578037741
|
146
128
|
requirements:
|
147
129
|
- phantomjs, v1.6 or greater
|
148
130
|
rubyforge_project:
|
149
|
-
rubygems_version:
|
131
|
+
rubygems_version: 2.2.2
|
150
132
|
signing_key:
|
151
|
-
specification_version:
|
133
|
+
specification_version: 4
|
152
134
|
summary: a phantomjs based pdf renderer
|
153
135
|
test_files:
|
154
136
|
- spec/shrimp/middleware_spec.rb
|