webshot 0.0.9 → 0.0.10
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -5
- data/CONTRIBUTORS +3 -0
- data/README.md +8 -1
- data/lib/webshot/screenshot.rb +37 -28
- data/lib/webshot/version.rb +1 -1
- data/test/webshot_test.rb +16 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41f7943a283536330672cd680f8ce992a0d4723e
|
4
|
+
data.tar.gz: 3d3f6dfe184a6b1d22ed2e6347b9722dfaa7c45e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c48722fa56971be69bb4e7865613c0c5110e649b8d8676885a32153704bbc0bcff10473d4272485bcde84aae39efa65469fbec45e2a8de20310eb9b1c716b7b1
|
7
|
+
data.tar.gz: df2cf5b137f23928c91a57ad30981cbd287e08b7d917476424e7873eef65a90928fe7931e49c1009aab7ba1771f7428af791a16756f59bda0f245b1e66aa6b84
|
data/.travis.yml
CHANGED
data/CONTRIBUTORS
CHANGED
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Captures a web page as a screenshot using Poltergeist, Capybara and PhantomJS.
|
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
Download and install [PhantomJS](http://phantomjs.org/),
|
9
|
+
Download and install [PhantomJS](http://phantomjs.org/releases.html) version 1.9,
|
10
10
|
add the directory containing the binary to your PATH.
|
11
11
|
|
12
12
|
Add the `webshot` gem to your Gemfile:
|
@@ -33,6 +33,13 @@ ws.capture "http://www.google.com/", "google.png"
|
|
33
33
|
# Customize thumbnail
|
34
34
|
ws.capture "http://www.google.com/", "google.png", width: 100, height: 90, quality: 85
|
35
35
|
|
36
|
+
# Specify only width, height will be computed according to page's height
|
37
|
+
ws.capture "http://www.google.com/", "google.png", width: 1024
|
38
|
+
|
39
|
+
# Specify an array of additional HTTP status codes to accept,
|
40
|
+
# beyond normal success codes like 200 or 302
|
41
|
+
ws.capture "http://www.google.com/foo", "google_404.png", allowed_status_codes: [404]
|
42
|
+
|
36
43
|
# Customize thumbnail generation (MiniMagick)
|
37
44
|
# see: https://github.com/minimagick/minimagick
|
38
45
|
ws.capture("http://www.google.com/", "google.png") do |magick|
|
data/lib/webshot/screenshot.rb
CHANGED
@@ -14,7 +14,7 @@ module Webshot
|
|
14
14
|
# Browser settings
|
15
15
|
page.driver.resize(width, height)
|
16
16
|
page.driver.headers = {
|
17
|
-
"User-Agent" => user_agent
|
17
|
+
"User-Agent" => user_agent
|
18
18
|
}
|
19
19
|
end
|
20
20
|
|
@@ -25,6 +25,13 @@ module Webshot
|
|
25
25
|
self
|
26
26
|
end
|
27
27
|
|
28
|
+
def valid_status_code?(status_code, allowed_status_codes)
|
29
|
+
return true if status_code == 200
|
30
|
+
return true if status_code / 100 == 3
|
31
|
+
return true if allowed_status_codes.include?(status_code)
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
28
35
|
# Captures a screenshot of +url+ saving it to +path+.
|
29
36
|
def capture(url, path, opts = {})
|
30
37
|
begin
|
@@ -33,6 +40,7 @@ module Webshot
|
|
33
40
|
height = opts.fetch(:height, 90)
|
34
41
|
gravity = opts.fetch(:gravity, "north")
|
35
42
|
quality = opts.fetch(:quality, 85)
|
43
|
+
allowed_status_codes = opts.fetch(:allowed_status_codes, [])
|
36
44
|
|
37
45
|
# Reset session before visiting url
|
38
46
|
Capybara.reset_sessions! unless @session_started
|
@@ -45,36 +53,37 @@ module Webshot
|
|
45
53
|
sleep opts[:timeout] if opts[:timeout]
|
46
54
|
|
47
55
|
# Check response code
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
# Save screenshot to file
|
53
|
-
page.driver.save_screenshot(tmp.path, :full => true)
|
56
|
+
status_code = page.driver.status_code.to_i
|
57
|
+
unless valid_status_code?(status_code, allowed_status_codes)
|
58
|
+
fail WebshotError, "Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}"
|
59
|
+
end
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
else
|
61
|
-
thumb.combine_options do |c|
|
62
|
-
c.thumbnail "#{width}x"
|
63
|
-
c.background "white"
|
64
|
-
c.extent "#{width}x#{height}"
|
65
|
-
c.gravity gravity
|
66
|
-
c.quality quality
|
67
|
-
end
|
68
|
-
end
|
61
|
+
tmp = Tempfile.new(["webshot", ".png"])
|
62
|
+
tmp.close
|
63
|
+
begin
|
64
|
+
# Save screenshot to file
|
65
|
+
page.driver.save_screenshot(tmp.path, :full => true)
|
69
66
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
67
|
+
# Resize screenshot
|
68
|
+
thumb = MiniMagick::Image.open(tmp.path)
|
69
|
+
if block_given?
|
70
|
+
# Customize MiniMagick options
|
71
|
+
yield thumb
|
72
|
+
else
|
73
|
+
thumb.combine_options do |c|
|
74
|
+
c.thumbnail "#{width}x"
|
75
|
+
c.background "white"
|
76
|
+
c.extent "#{width}x#{height}"
|
77
|
+
c.gravity gravity
|
78
|
+
c.quality quality
|
79
|
+
end
|
75
80
|
end
|
76
|
-
|
77
|
-
|
81
|
+
|
82
|
+
# Save thumbnail
|
83
|
+
thumb.write path
|
84
|
+
thumb
|
85
|
+
ensure
|
86
|
+
tmp.unlink
|
78
87
|
end
|
79
88
|
rescue Capybara::Poltergeist::StatusFailError, Capybara::Poltergeist::BrowserError, Capybara::Poltergeist::DeadClient, Capybara::Poltergeist::TimeoutError, Errno::EPIPE => e
|
80
89
|
# TODO: Handle Errno::EPIPE and Errno::ECONNRESET
|
data/lib/webshot/version.rb
CHANGED
data/test/webshot_test.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
3
|
class WebshotTest < MiniTest::Unit::TestCase
|
4
|
-
|
5
4
|
DATA_DIR = File.expand_path(File.dirname(__FILE__) + "/data")
|
6
5
|
|
7
6
|
def setup
|
@@ -12,18 +11,18 @@ class WebshotTest < MiniTest::Unit::TestCase
|
|
12
11
|
def test_http
|
13
12
|
%w(www.yahoo.com).each do |name|
|
14
13
|
output = thumb(name)
|
15
|
-
File.delete output if File.
|
14
|
+
File.delete output if File.exist? output
|
16
15
|
@webshot.capture "http://#{name}/", output
|
17
|
-
assert File.
|
16
|
+
assert File.exist? output
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
21
20
|
def test_https
|
22
21
|
%w(github.com).each do |name|
|
23
22
|
output = thumb(name)
|
24
|
-
File.delete output if File.
|
23
|
+
File.delete output if File.exist? output
|
25
24
|
@webshot.capture "https://#{name}/", output
|
26
|
-
assert File.
|
25
|
+
assert File.exist? output
|
27
26
|
end
|
28
27
|
end
|
29
28
|
|
@@ -38,7 +37,7 @@ class WebshotTest < MiniTest::Unit::TestCase
|
|
38
37
|
def test_mini_magick
|
39
38
|
%w(www.yahoo.com).each do |name|
|
40
39
|
output = thumb(name)
|
41
|
-
File.delete output if File.
|
40
|
+
File.delete output if File.exist? output
|
42
41
|
|
43
42
|
# Customize MiniMagick options
|
44
43
|
result = @webshot.capture("http://#{name}/", output) do |thumb|
|
@@ -46,11 +45,21 @@ class WebshotTest < MiniTest::Unit::TestCase
|
|
46
45
|
c.thumbnail "100x90"
|
47
46
|
end
|
48
47
|
end
|
49
|
-
assert File.
|
48
|
+
assert File.exist? output
|
50
49
|
assert result.respond_to? :to_blob
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
53
|
+
def test_allowed_404_status
|
54
|
+
%w(en.wikipedia.org/wiki/THIS_PAGE_DOES_NOT_EXIST).each do |name|
|
55
|
+
output = thumb('404_example')
|
56
|
+
File.delete output if File.exist? output
|
57
|
+
|
58
|
+
@webshot.capture "https://#{name}/", output, allowed_status_codes: [404]
|
59
|
+
assert File.exist? output
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
54
63
|
protected
|
55
64
|
|
56
65
|
def thumb(name)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webshot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitalie Cherpec
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|