webshot 0.0.3 → 0.0.4

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/CONTRIBUTORS ADDED
@@ -0,0 +1 @@
1
+ Alex Avoyants
data/README.md CHANGED
@@ -19,27 +19,49 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- # Setup Capybara
23
- Webshot.capybara_setup!
24
- webshot = Webshot::Screenshot.new
25
-
26
- # Capture Google's home page
27
- webshot.capture "http://www.google.com/", "google.png"
28
-
29
- # Customize thumbnail
30
- webshot.capture "http://www.google.com/", "google.png", width: 100, height: 90, quality: 85
31
-
32
- # Customize thumbnail generation (MiniMagick)
33
- # see: https://github.com/minimagick/minimagick
34
- webshot.capture("http://www.google.com/", "google.png") do |magick|
35
- magick.combine_options do |c|
36
- c.thumbnail "100x"
37
- c.background "white"
38
- c.extent "100x90"
39
- c.gravity "north"
40
- c.quality 85
41
- end
42
- end
22
+ ```rb
23
+ # Setup Capybara
24
+ ws = Webshot::Screenshot.instance
25
+
26
+ # Capture Google's home page
27
+ ws.capture "http://www.google.com/", "google.png"
28
+
29
+ # Customize thumbnail
30
+ ws.capture "http://www.google.com/", "google.png", width: 100, height: 90, quality: 85
31
+
32
+ # Customize thumbnail generation (MiniMagick)
33
+ # see: https://github.com/minimagick/minimagick
34
+ ws.capture("http://www.google.com/", "google.png") do |magick|
35
+ magick.combine_options do |c|
36
+ c.thumbnail "100x"
37
+ c.background "white"
38
+ c.extent "100x90"
39
+ c.gravity "north"
40
+ c.quality 85
41
+ end
42
+ end
43
+
44
+ ```
45
+
46
+ You can wait some time after visit page before capturing screenshot.
47
+
48
+ ```rb
49
+ # Timeout in seconds
50
+ ws.capture 'http://www.google.com/', 'google.png', timeout: 2
51
+
52
+ ```
53
+
54
+ You can login before capturing screenshot.
55
+
56
+ ```rb
57
+ ws.start_session do
58
+ visit 'https://github.com/login'
59
+ fill_in 'Username or Email', :with => 'user@example.com'
60
+ fill_in 'Password', :with => 'password'
61
+ click_button 'Sign in'
62
+ end.capture 'https://github.com/username/', 'github.png'
63
+
64
+ ```
43
65
 
44
66
  ## Contributing
45
67
 
@@ -1,8 +1,12 @@
1
+ require "singleton"
2
+
1
3
  module Webshot
2
4
  class Screenshot
3
5
  include Capybara::DSL
6
+ include Singleton
4
7
 
5
8
  def initialize(opts = {})
9
+ Webshot.capybara_setup!
6
10
  width = opts.fetch(:width, Webshot.width)
7
11
  height = opts.fetch(:height, Webshot.height)
8
12
  user_agent = opts.fetch(:user_agent, Webshot.user_agent)
@@ -14,44 +18,67 @@ module Webshot
14
18
  }
15
19
  end
16
20
 
21
+ def start_session(&block)
22
+ Capybara.reset_sessions!
23
+ Capybara.current_session.instance_eval(&block) if block_given?
24
+ @session_started = true
25
+ self
26
+ end
27
+
17
28
  # Captures a screenshot of +url+ saving it to +path+.
18
29
  def capture(url, path, opts = {})
19
- # Default settings
20
- width = opts.fetch(:width, 120)
21
- height = opts.fetch(:height, 90)
22
- gravity = opts.fetch(:gravity, "north")
23
- quality = opts.fetch(:quality, 85)
30
+ begin
31
+ # Default settings
32
+ width = opts.fetch(:width, 120)
33
+ height = opts.fetch(:height, 90)
34
+ gravity = opts.fetch(:gravity, "north")
35
+ quality = opts.fetch(:quality, 85)
24
36
 
25
- # Reset session before visiting url
26
- Capybara.reset_sessions!
37
+ # Reset session before visiting url
38
+ Capybara.reset_sessions! unless @session_started
39
+ @session_started = false
27
40
 
28
- # Open page
29
- visit url
41
+ # Open page
42
+ visit url
30
43
 
31
- # Check response code
32
- if page.driver.status_code == 200
33
- # Save screenshot to file
34
- page.driver.save_screenshot(path, :full => true)
44
+ # Timeout
45
+ sleep opts[:timeout] if opts[:timeout]
35
46
 
36
- # Resize screenshot
37
- thumb = MiniMagick::Image.open(path)
38
- if block_given?
39
- # Customize MiniMagick options
40
- yield thumb
41
- else
42
- thumb.combine_options do |c|
43
- c.thumbnail "#{width}x"
44
- c.background "white"
45
- c.extent "#{width}x#{height}"
46
- c.gravity gravity
47
- c.quality quality
47
+ # Check response code
48
+ if page.driver.status_code == 200
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)
54
+
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
69
+
70
+ # Save thumbnail
71
+ thumb.write path
72
+ thumb
73
+ ensure
74
+ tmp.unlink
48
75
  end
76
+ else
77
+ raise WebshotError.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}")
49
78
  end
50
-
51
- # Save thumbnail
52
- thumb.write path
53
- else
54
- raise WebshotError.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}")
79
+ rescue Capybara::Poltergeist::BrowserError, Capybara::Poltergeist::DeadClient, Capybara::Poltergeist::TimeoutError, Errno::EPIPE => e
80
+ # TODO: Handle Errno::EPIPE and Errno::ECONNRESET
81
+ raise WebshotError.new("Capybara error: #{e.message.inspect}")
55
82
  end
56
83
  end
57
84
  end
@@ -1,3 +1,3 @@
1
1
  module Webshot
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/test/webshot_test.rb CHANGED
@@ -5,8 +5,8 @@ class WebshotTest < Test::Unit::TestCase
5
5
  DATA_DIR = File.expand_path(File.dirname(__FILE__) + "/data")
6
6
 
7
7
  def setup
8
- Webshot.capybara_setup!
9
- @webshot = Webshot::Screenshot.new
8
+ FileUtils.mkdir_p(DATA_DIR) unless File.directory?(DATA_DIR)
9
+ @webshot = Webshot::Screenshot.instance
10
10
  end
11
11
 
12
12
  def test_http
@@ -46,12 +46,13 @@ class WebshotTest < Test::Unit::TestCase
46
46
  File.delete output if File.exists? output
47
47
 
48
48
  # Customize MiniMagick options
49
- @webshot.capture("http://#{name}/", output) do |thumb|
49
+ result = @webshot.capture("http://#{name}/", output) do |thumb|
50
50
  thumb.combine_options do |c|
51
51
  c.thumbnail "100x90"
52
52
  end
53
53
  end
54
54
  assert File.exists? output
55
+ assert result.respond_to? :to_blob
55
56
  end
56
57
  end
57
58
  end
data/webshot.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
 
24
24
  spec.add_dependency "activesupport"
25
- spec.add_dependency "poltergeist", "~> 1.2.0"
26
- spec.add_dependency "mini_magick", "~> 3.5.0"
25
+ spec.add_dependency "poltergeist", "~> 1.3.0"
26
+ spec.add_dependency "faye-websocket", "= 0.4.7"
27
+ spec.add_dependency "mini_magick", "~> 3.6.0"
27
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-29 00:00:00.000000000 Z
12
+ date: 2014-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: 1.2.0
69
+ version: 1.3.0
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,23 @@ dependencies:
74
74
  requirements:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: 1.2.0
77
+ version: 1.3.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: faye-websocket
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - '='
84
+ - !ruby/object:Gem::Version
85
+ version: 0.4.7
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - '='
92
+ - !ruby/object:Gem::Version
93
+ version: 0.4.7
78
94
  - !ruby/object:Gem::Dependency
79
95
  name: mini_magick
80
96
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +98,7 @@ dependencies:
82
98
  requirements:
83
99
  - - ~>
84
100
  - !ruby/object:Gem::Version
85
- version: 3.5.0
101
+ version: 3.6.0
86
102
  type: :runtime
87
103
  prerelease: false
88
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -90,7 +106,7 @@ dependencies:
90
106
  requirements:
91
107
  - - ~>
92
108
  - !ruby/object:Gem::Version
93
- version: 3.5.0
109
+ version: 3.6.0
94
110
  description: Captures a web page as a screenshot using Poltergeist, Capybara and PhantomJS
95
111
  email:
96
112
  - vitalie@penguin.ro
@@ -99,6 +115,7 @@ extensions: []
99
115
  extra_rdoc_files: []
100
116
  files:
101
117
  - .gitignore
118
+ - CONTRIBUTORS
102
119
  - Gemfile
103
120
  - LICENSE.txt
104
121
  - README.md