webshot 0.0.9 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 62a9727397129f1b5a062f88bd46a48c1b4dd5db
4
- data.tar.gz: b8d66c2cad1e39bb66c55b245549486d1e8e52bb
2
+ SHA256:
3
+ metadata.gz: a59b9cc6a133d67974cba34f4493c8a478501667c32aac1e098ba38774779eaa
4
+ data.tar.gz: a879235931047261bf8fa43dc20a99bfd3e65f8c2600de19a26aa03a80826d43
5
5
  SHA512:
6
- metadata.gz: 6576d08d90ffe8fc2f129f143cf7f13327af5375e7ceef002646fa9df7f55baa459ebe7d8bfaed128e164359dadd15f3da8722d7f95f5d75903c155f857b807e
7
- data.tar.gz: 4e76de6199a9a96ba5feb068645733dcc857bfefd667a30c7c43cce9d2e929f4c73580897b5c2f23c6f1263cb7be2e2ba82cc01257ed525a002650b8a2cf9868
6
+ metadata.gz: a003e10846290ac9717ed85fb2859cfe7d93f704f2a109dc31d5aea7a03e6be0ac514887e4e6f8154621558f8dbee1a8e350fff663b88d4c93967b113654505a
7
+ data.tar.gz: 0dcf0f7ad5cc31ed044bd253b8d6d31ae81191e616d2d401f5dd8010a05b51fa2fa3a799738744ecb25726aa5c346636a5fc7a61877e4669d600b6da90eed1c5
@@ -3,12 +3,8 @@ cache: bundler
3
3
  before_install:
4
4
  - gem install bundler
5
5
  rvm:
6
- - 2.1
7
- - 2.0
8
- - 1.9.3
9
- - jruby-19mode # JRuby in 1.9 mode
10
- - rbx-2
6
+ - 2.5
7
+ - 2.6
8
+ - 2.7
11
9
  matrix:
12
- allow_failures:
13
- - rvm: rbx-2
14
10
  fast_finish: true
@@ -1 +1,4 @@
1
1
  Alex Avoyants
2
+ Nick Kezhaya
3
+ Rafa García
4
+ Sage Ross
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 2.X,
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|
@@ -82,7 +89,7 @@ Recommended setup:
82
89
 
83
90
 
84
91
  The application triggers screenshot requests which are queued and
85
- then processed by a background worker (Resque, Sidkiq, etc).
92
+ then processed by a background worker ([Resque](https://github.com/resque/resque), [Sidekiq](https://github.com/mperham/sidekiq), etc).
86
93
  The worker uploads the images to S3 which are served through
87
94
  CloudFront. The CDN should be configured to serve a default
88
95
  image (404 handler) with a low TTL to handle screenshot are not
@@ -27,7 +27,7 @@ module Webshot
27
27
  yield self
28
28
  end
29
29
 
30
- # Capibara setup
30
+ # Capybara setup
31
31
  def self.capybara_setup!
32
32
  # By default Capybara will try to boot a rack application
33
33
  # automatically. You might want to switch off Capybara's
@@ -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,9 @@ module Webshot
33
40
  height = opts.fetch(:height, 90)
34
41
  gravity = opts.fetch(:gravity, "north")
35
42
  quality = opts.fetch(:quality, 85)
43
+ full = opts.fetch(:full, true)
44
+ selector = opts.fetch(:selector, nil)
45
+ allowed_status_codes = opts.fetch(:allowed_status_codes, [])
36
46
 
37
47
  # Reset session before visiting url
38
48
  Capybara.reset_sessions! unless @session_started
@@ -45,36 +55,40 @@ module Webshot
45
55
  sleep opts[:timeout] if opts[:timeout]
46
56
 
47
57
  # Check response code
48
- if page.driver.status_code.to_i == 200 || page.driver.status_code.to_i / 100 == 3
49
- tmp = Tempfile.new(["webshot", ".png"])
50
- tmp.close
51
- begin
52
- # Save screenshot to file
53
- page.driver.save_screenshot(tmp.path, :full => true)
58
+ status_code = page.driver.status_code.to_i
59
+ unless valid_status_code?(status_code, allowed_status_codes)
60
+ fail WebshotError, "Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}"
61
+ end
54
62
 
55
- # Resize screenshot
56
- thumb = MiniMagick::Image.open(tmp.path)
57
- if block_given?
58
- # Customize MiniMagick options
59
- yield thumb
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
63
+ tmp = Tempfile.new(["webshot", ".png"])
64
+ tmp.close
65
+ begin
66
+ screenshot_opts = { full: full }
67
+ screenshot_opts = screenshot_opts.merge({ selector: selector }) if selector
69
68
 
70
- # Save thumbnail
71
- thumb.write path
72
- thumb
73
- ensure
74
- tmp.unlink
69
+ # Save screenshot to file
70
+ page.driver.save_screenshot(tmp.path, screenshot_opts)
71
+
72
+ # Resize screenshot
73
+ thumb = MiniMagick::Image.open(tmp.path)
74
+ if block_given?
75
+ # Customize MiniMagick options
76
+ yield thumb
77
+ else
78
+ thumb.combine_options do |c|
79
+ c.thumbnail "#{width}x"
80
+ c.background "white"
81
+ c.extent "#{width}x#{height}"
82
+ c.gravity gravity
83
+ c.quality quality
84
+ end
75
85
  end
76
- else
77
- raise WebshotError.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}")
86
+
87
+ # Save thumbnail
88
+ thumb.write path
89
+ thumb
90
+ ensure
91
+ tmp.unlink
78
92
  end
79
93
  rescue Capybara::Poltergeist::StatusFailError, Capybara::Poltergeist::BrowserError, Capybara::Poltergeist::DeadClient, Capybara::Poltergeist::TimeoutError, Errno::EPIPE => e
80
94
  # TODO: Handle Errno::EPIPE and Errno::ECONNRESET
@@ -1,3 +1,3 @@
1
1
  module Webshot
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require "test_helper"
2
2
 
3
- class WebshotTest < MiniTest::Unit::TestCase
4
-
3
+ class WebshotTest < Minitest::Test
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.exists? output
14
+ File.delete output if File.exist? output
16
15
  @webshot.capture "http://#{name}/", output
17
- assert File.exists? output
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.exists? output
23
+ File.delete output if File.exist? output
25
24
  @webshot.capture "https://#{name}/", output
26
- assert File.exists? output
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.exists? output
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.exists? output
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)
@@ -18,13 +18,13 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "minitest"
24
- spec.add_development_dependency "gem-release"
21
+ spec.add_development_dependency "rake", "~> 13"
22
+ spec.add_development_dependency "minitest", "~> 5.14"
23
+ spec.add_development_dependency "gem-release", "~> 2.1"
24
+ spec.add_development_dependency "bump", "~> 0.9"
25
25
 
26
- spec.add_dependency "activesupport"
27
- spec.add_dependency "poltergeist", "~> 1.6.0"
28
- spec.add_dependency "faye-websocket", "~> 0.7.3"
29
- spec.add_dependency "mini_magick", "~> 4.3.3"
26
+ spec.add_dependency "activesupport", "~> 5"
27
+ spec.add_dependency "poltergeist", [">= 1.12.0", "<= 1.18.1"]
28
+ spec.add_dependency "faye-websocket", "~> 0.11.0"
29
+ spec.add_dependency "mini_magick", "~> 4.9"
30
30
  end
metadata CHANGED
@@ -1,127 +1,133 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitalie Cherpec
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-14 00:00:00.000000000 Z
11
+ date: 2020-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '13'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '13'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '5.14'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '5.14'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: gem-release
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '2.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '2.1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: gem-release
56
+ name: bump
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '0.9'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '0.9'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '5'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '5'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: poltergeist
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 1.6.0
89
+ version: 1.12.0
90
+ - - "<="
91
+ - !ruby/object:Gem::Version
92
+ version: 1.18.1
90
93
  type: :runtime
91
94
  prerelease: false
92
95
  version_requirements: !ruby/object:Gem::Requirement
93
96
  requirements:
94
- - - "~>"
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 1.12.0
100
+ - - "<="
95
101
  - !ruby/object:Gem::Version
96
- version: 1.6.0
102
+ version: 1.18.1
97
103
  - !ruby/object:Gem::Dependency
98
104
  name: faye-websocket
99
105
  requirement: !ruby/object:Gem::Requirement
100
106
  requirements:
101
107
  - - "~>"
102
108
  - !ruby/object:Gem::Version
103
- version: 0.7.3
109
+ version: 0.11.0
104
110
  type: :runtime
105
111
  prerelease: false
106
112
  version_requirements: !ruby/object:Gem::Requirement
107
113
  requirements:
108
114
  - - "~>"
109
115
  - !ruby/object:Gem::Version
110
- version: 0.7.3
116
+ version: 0.11.0
111
117
  - !ruby/object:Gem::Dependency
112
118
  name: mini_magick
113
119
  requirement: !ruby/object:Gem::Requirement
114
120
  requirements:
115
121
  - - "~>"
116
122
  - !ruby/object:Gem::Version
117
- version: 4.3.3
123
+ version: '4.9'
118
124
  type: :runtime
119
125
  prerelease: false
120
126
  version_requirements: !ruby/object:Gem::Requirement
121
127
  requirements:
122
128
  - - "~>"
123
129
  - !ruby/object:Gem::Version
124
- version: 4.3.3
130
+ version: '4.9'
125
131
  description: Captures a web page as a screenshot using Poltergeist, Capybara and PhantomJS
126
132
  email:
127
133
  - vitalie@penguin.ro
@@ -147,7 +153,7 @@ homepage: https://github.com/vitalie/webshot
147
153
  licenses:
148
154
  - MIT
149
155
  metadata: {}
150
- post_install_message:
156
+ post_install_message:
151
157
  rdoc_options: []
152
158
  require_paths:
153
159
  - lib
@@ -162,9 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
168
  - !ruby/object:Gem::Version
163
169
  version: '0'
164
170
  requirements: []
165
- rubyforge_project:
166
- rubygems_version: 2.2.2
167
- signing_key:
171
+ rubygems_version: 3.1.2
172
+ signing_key:
168
173
  specification_version: 4
169
174
  summary: Captures a web page as a screenshot
170
175
  test_files: