webshot 0.0.1

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.
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ test/data/*
20
+
21
+ # Vim
22
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webshot.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vitalie Cherpec
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # Webshot
2
+
3
+ Captures a web page as a screenshot using Poltergeist, Capybara and PhantomJS.
4
+
5
+ ## Installation
6
+
7
+ Download and install [PhantomJS](http://phantomjs.org/) and then add PhantomJS to
8
+ your PATH. Add this line to your application's Gemfile:
9
+
10
+ gem "webshot"
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install webshot
19
+
20
+ ## Usage
21
+
22
+ # Setup Capybara
23
+ Webshot.capybara_setup!
24
+
25
+ webshot = Webshot::Screenshot.new
26
+ webshot.capture "http://www.google.com/", "google.png"
27
+
28
+ # Customize output (MiniMagick settings)
29
+ webshot.capture "http://www.google.com/", "google.png", width: 100, height: 90, quality: 85
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+ require "rdoc/task"
5
+
6
+ desc "Default: run tests."
7
+ task :default => :test
8
+
9
+
10
+ desc "Run webshot unit tests."
11
+ Rake::TestTask.new do |t|
12
+ t.libs << "test"
13
+ t.libs << "lib"
14
+ t.test_files = Dir[ "test/*_test.rb" ]
15
+ t.verbose = true
16
+ end
@@ -0,0 +1,43 @@
1
+ require "mini_magick"
2
+ require "capybara/dsl"
3
+ require "capybara/poltergeist"
4
+ require "active_support/core_ext"
5
+ require "webshot/version"
6
+ require "webshot/errors"
7
+ require "webshot/screenshot"
8
+
9
+ module Webshot
10
+
11
+ ## Browser settings
12
+ # Width
13
+ mattr_accessor :width
14
+ @@width = 1024
15
+
16
+ # Height
17
+ mattr_accessor :height
18
+ @@height = 768
19
+
20
+ # User agent
21
+ mattr_accessor :user_agent
22
+ @@user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31"
23
+
24
+ def self.setup
25
+ yield self
26
+ end
27
+
28
+ def self.capybara_setup!
29
+ # By default Capybara will try to boot a rack application
30
+ # automatically. You might want to switch off Capybara's
31
+ # rack server if you are running against a remote application
32
+ Capybara.run_server = false
33
+ Capybara.register_driver :poltergeist do |app|
34
+ Capybara::Poltergeist::Driver.new(app, {
35
+ # Raise JavaScript errors to Ruby
36
+ js_errors: false,
37
+ # Additional command line options for PhantomJS
38
+ phantomjs_options: ['--ignore-ssl-errors=yes'],
39
+ })
40
+ end
41
+ Capybara.current_driver = :poltergeist
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Webshot
2
+ class WebshotError < RuntimeError; end
3
+ end
@@ -0,0 +1,39 @@
1
+ module Webshot
2
+ class Screenshot
3
+ include Capybara::DSL
4
+
5
+ def initialize(opts = {})
6
+ width = opts.fetch(:width, Webshot.width)
7
+ height = opts.fetch(:height, Webshot.height)
8
+ user_agent = opts.fetch(:user_agent, Webshot.user_agent)
9
+ page.driver.resize(width, height)
10
+ page.driver.headers = {
11
+ "User-Agent" => user_agent,
12
+ }
13
+ end
14
+
15
+ def capture(url, path, opts = {})
16
+ # Default settings
17
+ width = opts.fetch(:width, 120)
18
+ height = opts.fetch(:height, 90)
19
+ gravity = opts.fetch(:gravity, "north")
20
+ quality = opts.fetch(:quality, 85)
21
+
22
+ visit url
23
+ if page.driver.status_code == 200
24
+ page.driver.save_screenshot(path, :full => true)
25
+ thumb = MiniMagick::Image.open(path)
26
+ thumb.combine_options do |c|
27
+ c.thumbnail "#{width}x"
28
+ c.background "white"
29
+ c.extent "#{width}x#{height}"
30
+ c.gravity "north"
31
+ c.quality 85
32
+ end
33
+ thumb.write path
34
+ else
35
+ raise WebshotError.new("Could not fetch page: #{url.inspect}")
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Webshot
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "webshot"
2
+ require "test/unit"
@@ -0,0 +1,47 @@
1
+ require "test_helper"
2
+
3
+ class WebshotTest < Test::Unit::TestCase
4
+
5
+ DATA_DIR = File.expand_path(File.dirname(__FILE__) + "/data")
6
+
7
+ def setup
8
+ Webshot.capybara_setup!
9
+ @webshot = Webshot::Screenshot.new
10
+ end
11
+
12
+ def test_http
13
+ assert_nothing_raised do
14
+ %w(www.google.com www.yahoo.com).each do |name|
15
+ output = thumb(name)
16
+ File.delete output if File.exists? output
17
+ @webshot.capture "http://#{name}/", output
18
+ assert File.exists? output
19
+ end
20
+ end
21
+ end
22
+
23
+ def test_https
24
+ assert_nothing_raised do
25
+ %w(github.com).each do |name|
26
+ output = thumb(name)
27
+ File.delete output if File.exists? output
28
+ @webshot.capture "https://#{name}/", output
29
+ assert File.exists? output
30
+ end
31
+ end
32
+ end
33
+
34
+ def test_
35
+ %w(888.888.888.888).each do |name|
36
+ assert_raise Webshot::WebshotError do
37
+ @webshot.capture "http://#{name}/", thumb(name)
38
+ end
39
+ end
40
+ end
41
+
42
+ protected
43
+
44
+ def thumb(name)
45
+ File.join(DATA_DIR, "#{name}.png")
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webshot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "webshot"
8
+ spec.version = Webshot::VERSION
9
+ spec.authors = ["Vitalie Cherpec"]
10
+ spec.email = ["vitalie@penguin.ro"]
11
+ spec.description = %q{Captures a web page as a screenshot using Poltergeist, Capybara and PhantomJS}
12
+ spec.summary = %q{Captures a web page as a screenshot}
13
+ spec.homepage = "https://github.com/vitalie/webshot"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "activesupport"
25
+ spec.add_dependency "poltergeist", "~> 1.2.0"
26
+ spec.add_dependency "mini_magick", "~> 3.5.0"
27
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vitalie Cherpec
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: poltergeist
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.2.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.2.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: mini_magick
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 3.5.0
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: 3.5.0
94
+ description: Captures a web page as a screenshot using Poltergeist, Capybara and PhantomJS
95
+ email:
96
+ - vitalie@penguin.ro
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/webshot.rb
107
+ - lib/webshot/errors.rb
108
+ - lib/webshot/screenshot.rb
109
+ - lib/webshot/version.rb
110
+ - test/test_helper.rb
111
+ - test/webshot_test.rb
112
+ - webshot.gemspec
113
+ homepage: https://github.com/vitalie/webshot
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.24
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Captures a web page as a screenshot
138
+ test_files:
139
+ - test/test_helper.rb
140
+ - test/webshot_test.rb