webshot 0.0.9 → 0.0.10

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
2
  SHA1:
3
- metadata.gz: 62a9727397129f1b5a062f88bd46a48c1b4dd5db
4
- data.tar.gz: b8d66c2cad1e39bb66c55b245549486d1e8e52bb
3
+ metadata.gz: 41f7943a283536330672cd680f8ce992a0d4723e
4
+ data.tar.gz: 3d3f6dfe184a6b1d22ed2e6347b9722dfaa7c45e
5
5
  SHA512:
6
- metadata.gz: 6576d08d90ffe8fc2f129f143cf7f13327af5375e7ceef002646fa9df7f55baa459ebe7d8bfaed128e164359dadd15f3da8722d7f95f5d75903c155f857b807e
7
- data.tar.gz: 4e76de6199a9a96ba5feb068645733dcc857bfefd667a30c7c43cce9d2e929f4c73580897b5c2f23c6f1263cb7be2e2ba82cc01257ed525a002650b8a2cf9868
6
+ metadata.gz: c48722fa56971be69bb4e7865613c0c5110e649b8d8676885a32153704bbc0bcff10473d4272485bcde84aae39efa65469fbec45e2a8de20310eb9b1c716b7b1
7
+ data.tar.gz: df2cf5b137f23928c91a57ad30981cbd287e08b7d917476424e7873eef65a90928fe7931e49c1009aab7ba1771f7428af791a16756f59bda0f245b1e66aa6b84
@@ -3,12 +3,8 @@ cache: bundler
3
3
  before_install:
4
4
  - gem install bundler
5
5
  rvm:
6
+ - 2.2
6
7
  - 2.1
7
8
  - 2.0
8
- - 1.9.3
9
- - jruby-19mode # JRuby in 1.9 mode
10
- - rbx-2
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 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|
@@ -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
- 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)
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
- # 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
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
- # Save thumbnail
71
- thumb.write path
72
- thumb
73
- ensure
74
- tmp.unlink
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
- else
77
- raise WebshotError.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}")
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
@@ -1,3 +1,3 @@
1
1
  module Webshot
2
- VERSION = '0.0.9'
2
+ VERSION = '0.0.10'
3
3
  end
@@ -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.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)
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.9
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-09-14 00:00:00.000000000 Z
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler