screencork 0.0.6

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7d5d5dcc9ea3cdf64b4fa76e615d7c5b4c9a6b8
4
+ data.tar.gz: 7202348e60bf1893d4f0e12d8c0da8082c4429dc
5
+ SHA512:
6
+ metadata.gz: e2078779807978237e455759635c42a61df789c613bb8524365064b6d518ea7a6fdb8e555b1f790f38f5b6b3be1412915873f53c820aa382345e1431049ef671
7
+ data.tar.gz: f80308da4c8c41f1ce38afa77142091f3d12195dca0acb86f9460414768543dcbf137aff0732d87b95b45b19883d35044706fe16be81b2a51aa78bbde48d21ed
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ Gemfile.lock
6
+ pkg
7
+ rdoc
8
+ tmp
9
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in screencork.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Maxwell Sechzer
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.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Screencork
2
+
3
+ A gem for grabbing screenshots OR pdfs of webpages. It uses PhantomJS to load and render pages to strings.
4
+
5
+ ## Basic Usage
6
+
7
+ Either pass in the page's url to a new instance of Screencork::Screen, or to the ::screen method.
8
+ Then just call #to_your-format-here to return the string content of the file in that format.
9
+ Supported file formats are bmp, jpg, jpeg, png, ppm, xbm, xpm, and pdf.
10
+
11
+ ```ruby
12
+ require 'screencork'
13
+
14
+ screen = Screencork.screen('http://google.com')
15
+ screenshot = screen.to_png
16
+ ```
17
+
18
+ You can pass in any of the allowed options like so:
19
+
20
+ ```ruby
21
+ screen = Screencork.screen('http://google.com', height: 300, cutoff: 5000)
22
+
23
+ screen.to_pdf
24
+ ```
25
+
26
+ ## Advanced Usage
27
+
28
+ Options with defaults will have their defaults displayed below. All nested arrays/hashes will not have a default,
29
+ but do have default options for when only some of the nested options are supplied. These will be shown with the
30
+ shape of the required array/hash. If there are multiple ways to submit an option they are demonstrated together.
31
+
32
+ ```ruby
33
+ screen = Screencork.screen('google.com',
34
+ height: 300, # the height of the viewport itself
35
+ width: 400, # the width of the viewport itself
36
+ cutoff: 5000, # the amount of time (in ms) allowed before considering the request failed
37
+ cookies: [{ name: 'my-cookie', value: 'my-cookie-val', domain: 'google.com' }], # an array of the cookies to be sent with the request. Must at least have a name, value and valid domain
38
+ paper_size: { width: 400, height: 300, margin: 0 }, # the size of the page when rendering a pdf file. Either width or height must be supplied for this option
39
+ paper_size: { format: 'A4', orientation: 'portrait', margin: 0 }, # either format or orientation must be supplied for this option.
40
+ )
41
+
42
+ screen.to_pdf
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Screencork
2
+ IMAGE_FORMATS = %i(bmp jpg jpeg png ppm xbm xpm pdf)
3
+ end
@@ -0,0 +1,114 @@
1
+ var system = require('system'),
2
+ stdout = system.stdout,
3
+ page = require('webpage').create(),
4
+ url = system.args[1],
5
+ format = system.args[2],
6
+ opts = system.args[3],
7
+ args = { cutoff: 5000 };
8
+ defaultPaperSize = { format: 'A4', orientation: 'portrait' };
9
+
10
+ if( !url || !format ) {
11
+ stdout.write('Error: A URL and a file type must be given');
12
+ phantom.exit(1);
13
+ }
14
+
15
+ function extend() {
16
+ var obj, key, i = 1,
17
+ len = arguments.length,
18
+ baseObj = arguments[0];
19
+
20
+ for (; i < len; i++) {
21
+ obj = arguments[i];
22
+
23
+ for (key in obj) {
24
+ if (obj.hasOwnProperty(key)) {
25
+ baseObj[key] = obj[key];
26
+ }
27
+ }
28
+ }
29
+
30
+ return baseObj;
31
+ }
32
+
33
+ function timeout() {
34
+ stdout.write('Error: Request to ' + url + ' exceeded ' + args.cutoff + 'ms');
35
+ phantom.exit(1);
36
+ }
37
+
38
+ function setCutoff() {
39
+ setTimeout( timeout, args.cutoff );
40
+ }
41
+
42
+ function parseArguments() {
43
+ function camelize( string ) {
44
+ return string.replace(/_([a-z])/g, function (g) { return g[1].toUpperCase(); });
45
+ }
46
+
47
+ var systemArgs = opts ? JSON.parse( opts ) : {};
48
+
49
+ for ( var arg in systemArgs ) {
50
+ if ( systemArgs.hasOwnProperty(arg) ) {
51
+ args[camelize( arg )] = systemArgs[arg];
52
+ }
53
+ }
54
+ }
55
+
56
+ function addCookies() {
57
+ if ( !args.cookies ) { return; }
58
+ args.cookies.forEach(phantom.addCookie);
59
+ }
60
+
61
+ function setViewport() {
62
+ if ( args.height ) {
63
+ page.viewportSize = { height: args.height, width: args.width };
64
+ }
65
+ }
66
+
67
+ function setPaperSize() {
68
+ if ( args.paperSize && format === 'pdf' ) {
69
+ if ( args.paperSize.width || args.paperSize.height ) {
70
+ page.paperSize = { width: args.paperSize.width, height: args.paperSize.height, margin: args.paperSize.margin };
71
+ } else {
72
+ page.paperSize = extend( {}, defaultPaperSize, args.paperSize );
73
+ }
74
+ }
75
+ }
76
+
77
+ function getClipRect() {
78
+ if ( args.el ) {
79
+ page.clipRect = page.evaluate(function( sel ) {
80
+ return document.querySelector( sel ).getBoundingClientRect();
81
+ }, args.el);
82
+ }
83
+ }
84
+
85
+ // either get the base64 encoded file directly if the type is supported, or write the file to stdout
86
+ function render() {
87
+ if ( format === 'pdf' ) {
88
+ page.render( '/dev/stdout', {format: format} );
89
+ } else {
90
+ stdout.write(page.renderBase64( format ));
91
+ }
92
+
93
+ phantom.exit();
94
+ }
95
+
96
+ function openPage() {
97
+ page.open(url, function( status ) {
98
+ if( status !== 'success' ) {
99
+ stdout.write('Error: Unable to load: ' + url);
100
+ phantom.exit(1);
101
+ } else {
102
+ getClipRect();
103
+ render();
104
+ }
105
+ });
106
+
107
+ setCutoff();
108
+ }
109
+
110
+ parseArguments();
111
+ addCookies();
112
+ setViewport();
113
+ setPaperSize();
114
+ openPage();
@@ -0,0 +1,29 @@
1
+ module Screencork
2
+ class Screen
3
+ def initialize(url, opts = {})
4
+ @url = url
5
+ @opts = opts
6
+ end
7
+
8
+ IMAGE_FORMATS.each do |format|
9
+ define_method "to_#{format}" do
10
+ render format
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def render(format)
17
+ Screencork.render(@url, format, proccessed_opts)
18
+ end
19
+
20
+ def proccessed_opts
21
+ if (cookies = @opts[:cookies]) && cookies.is_a?(Hash)
22
+ array_cookies = cookies.map { |name, value| {domain: URI(@url).host, name: name, value: value} }
23
+ @opts.merge(cookies: array_cookies)
24
+ else
25
+ @opts
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Screencork
2
+ VERSION = '0.0.6'
3
+ end
data/lib/screencork.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'screencork/version'
2
+ require 'screencork/image_formats'
3
+ require 'screencork/screen'
4
+ require 'phantomjs'
5
+ require 'json'
6
+
7
+ module Screencork
8
+ class ScreencorkError < StandardError; end;
9
+
10
+ class << self
11
+ def screen(*args)
12
+ Screen.new(*args)
13
+ end
14
+
15
+ def render(url, format, opts = {})
16
+ result = run_phantom(url, format, opts)
17
+ raise_if_error! result
18
+ format == :pdf ? result : Base64.decode64(result)
19
+ end
20
+
21
+ def run_phantom(url, format, opts = {})
22
+ format, opts = format.to_s, opts.to_json
23
+ log_command(url, format, opts)
24
+ Phantomjs.run(render_script_path, url, format, opts)
25
+ end
26
+
27
+ private
28
+
29
+ def log_command(url, format, opts = {})
30
+ puts [Phantomjs.path, render_script_path, url, format, opts.inspect.inspect].join(' ')
31
+ end
32
+
33
+ def raise_if_error!(result)
34
+ return if !result.valid_encoding?
35
+ error_match = /Error: /.match(result)
36
+ return if error_match.nil?
37
+ raise ScreencorkError.new(error_match.post_match)
38
+ end
39
+
40
+ def render_script_path
41
+ @render_script_path ||= File.expand_path('../screencork/render.js', __FILE__)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require File.expand_path('../lib/screencork/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "screencork"
6
+ gem.version = Screencork::VERSION
7
+ gem.authors = ['Maxwell Sechzer']
8
+ gem.email = ['maxwell at sechzer.com']
9
+ gem.description = 'A gem for grabbing screenshots of webpages.'
10
+ gem.summary = 'Uses PhantomJS to load and render pages to strings.'
11
+ gem.homepage = ''
12
+ gem.license = 'MIT'
13
+
14
+ gem.files = `git ls-files`.split($\)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency 'phantomjs'
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'rake'
22
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: screencork
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Maxwell Sechzer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: phantomjs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A gem for grabbing screenshots of webpages.
56
+ email:
57
+ - maxwell at sechzer.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/screencork.rb
69
+ - lib/screencork/image_formats.rb
70
+ - lib/screencork/render.js
71
+ - lib/screencork/screen.rb
72
+ - lib/screencork/version.rb
73
+ - screencork.gemspec
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Uses PhantomJS to load and render pages to strings.
98
+ test_files: []