shrimp 0.0.4 → 0.0.5

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: 5f3a74b72ab74c2073b97e509e0d71dc3ac6dc90
4
- data.tar.gz: 20c0b3d8b39e38c712cb2e4453bb17f85762de5f
3
+ metadata.gz: 05e0367a03f9aa8546b0b317bdb720d3370bdd36
4
+ data.tar.gz: ccc3e94cd1cd019a2defb5c8f63aa8c768f340c1
5
5
  SHA512:
6
- metadata.gz: 045699658ee607453c27708941711b857c21a98cc0606021a12c12ebbb2cab7652c56b7104894be4162ff45f8b5e2a5acfec82b1c1d420da40c2ee90b657f683
7
- data.tar.gz: 95443979c5f10a47a2026181f168ead99a6d3fc50a0406f713a23035a6334149036eef8ecc6f4deb631db41ce2cec3df9a07d7d71896040a313813a1ba164ec9
6
+ metadata.gz: 197abf62e9ad22f6a7ad54c14b92a401bd86a294a99d961fdcf10fcbed375c7566d10d475fb21b83c0801ba764b36dcdc69c8b6bff75817170ed77c500334783
7
+ data.tar.gz: 684cb4b7d4618630bd0551a6521bf3b369eef9ad28b7022f3b873e199b49b797285ec87d12dfed0c0b014239642b1a34630e866574be7d0e88d4c9e309e283c1
data/.travis.yml CHANGED
@@ -1,10 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
- - 1.9.2
5
- - rbx-19mode
6
- - ruby-head
4
+ - rbx-2
7
5
  - 2.0.0
6
+ - 2.1.1
8
7
  matrix:
9
8
  allow_failures:
10
- - rvm: ruby-head
9
+ - rvm: rbx-2
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Shrimp
2
- [![Build Status](https://travis-ci.org/adeven/shrimp.png?branch=master)](https://travis-ci.org/adeven/shrimp)
2
+ [![Build Status](https://travis-ci.org/adjust/shrimp.png?branch=master)](https://travis-ci.org/adjust/shrimp)
3
3
  Creates PDFs from URLs using phantomjs
4
4
 
5
5
  Read our [blogpost](http://big-elephants.com/2012-12/pdf-rendering-with-phantomjs/) about how it works.
@@ -60,15 +60,20 @@ Shrimp.configure do |config|
60
60
  # increase if you need to render very complex pages
61
61
  # config.rendering_time = 1000
62
62
 
63
- # change the viewport size. If you rendering pages that have
63
+ # change the viewport size. If you rendering pages that have
64
64
  # flexible page width and height then you may need to set this
65
65
  # to enforce a specific size
66
- # config.viewport_width = 600
67
- # config.viewport_height = 600
66
+ # config.viewport_width = 600
67
+ # config.viewport_height = 600
68
68
 
69
69
  # the timeout for the phantomjs rendering process in ms
70
70
  # this needs always to be higher than rendering_time
71
- # config.rendering_timeout = 90000
71
+ # config.rendering_timeout = 90000
72
+
73
+ # maximum number of redirects to follow
74
+ # by default Shrimp does not follow any redirects which means that
75
+ # if the server responds with non HTTP 200 an error will be returned
76
+ # config.max_redirect_count = 0
72
77
 
73
78
  # the path to a json configuration file for command-line options
74
79
  # config.command_config_file = "#{Rails.root.join('config', 'shrimp', 'config.json')}"
@@ -153,7 +158,7 @@ To include some fancy Ajax stuff with jquery
153
158
  return window.location.assign(url);
154
159
  },
155
160
  504: function() {
156
- console.log("Shit's beeing wired")
161
+ console.log("Shit's being wired")
157
162
  },
158
163
  503: function(jqXHR, textStatus, errorThrown) {
159
164
  var wait;
@@ -1,10 +1,11 @@
1
1
  require 'tmpdir'
2
+
2
3
  module Shrimp
3
4
  class Configuration
4
5
  attr_accessor :default_options
5
6
  attr_writer :phantomjs
6
7
 
7
- [:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout, :rendering_time, :command_config_file, :viewport_width, :viewport_height].each do |m|
8
+ [:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout, :rendering_time, :command_config_file, :viewport_width, :viewport_height, :max_redirect_count].each do |m|
8
9
  define_method("#{m}=") do |val|
9
10
  @default_options[m]=val
10
11
  end
@@ -21,7 +22,8 @@ module Shrimp
21
22
  :rendering_time => 1000,
22
23
  :command_config_file => File.expand_path('../config.json', __FILE__),
23
24
  :viewport_width => 600,
24
- :viewport_height => 600
25
+ :viewport_height => 600,
26
+ :max_redirect_count => 0
25
27
  }
26
28
  end
27
29
 
@@ -28,13 +28,6 @@ module Shrimp
28
28
  attr_reader :options, :cookies, :result, :error
29
29
  SCRIPT_FILE = File.expand_path('../rasterize.js', __FILE__)
30
30
 
31
- class << self
32
- def quote_arg(arg)
33
- # "'#{arg.gsub("'", %q(\\\'))}'"
34
- "'#{arg.gsub("'", %q('"'"'))}'"
35
- end
36
- end
37
-
38
31
  # Public: Runs the phantomjs binary
39
32
  #
40
33
  # Returns the stdout output of phantomjs
@@ -65,6 +58,7 @@ module Shrimp
65
58
  format, zoom, margin, orientation = options[:format], options[:zoom], options[:margin], options[:orientation]
66
59
  rendering_time, timeout = options[:rendering_time], options[:rendering_timeout]
67
60
  viewport_width, viewport_height = options[:viewport_width], options[:viewport_height]
61
+ max_redirect_count = options[:max_redirect_count]
68
62
  @outfile ||= "#{options[:tmpdir]}/#{Digest::MD5.hexdigest((Time.now.to_i + rand(9001)).to_s)}.pdf"
69
63
  command_config_file = "--config=#{options[:command_config_file]}"
70
64
  [
@@ -81,7 +75,8 @@ module Shrimp
81
75
  rendering_time,
82
76
  timeout,
83
77
  viewport_width,
84
- viewport_height
78
+ viewport_height,
79
+ max_redirect_count
85
80
  ].join(" ")
86
81
  end
87
82
 
@@ -1,69 +1,83 @@
1
- var page = require('webpage').create(),
2
- fs = require('fs'),
3
- system = require('system'),
4
- margin = system.args[5] || '0cm',
5
- orientation = system.args[6] || 'portrait',
6
- cookie_file = system.args[7] ,
7
- render_time = system.args[8] || 10000 ,
8
- time_out = system.args[9] || 90000 ,
9
- viewport_width = system.args[10] || 600,
10
- viewport_height= system.args[11] || 600,
1
+ var
2
+ webpage = require('webpage'),
3
+ fs = require('fs'),
4
+ system = require('system'),
5
+ margin = system.args[5] || '0cm',
6
+ orientation = system.args[6] || 'portrait',
7
+ cookie_file = system.args[7],
8
+ render_time = system.args[8] || 10000 ,
9
+ time_out = system.args[9] || 90000 ,
10
+ viewport_width = system.args[10] || 600,
11
+ viewport_height = system.args[11] || 600,
12
+ redirects_num = system.args[12] || 0,
11
13
  cookies = {},
12
- address, output, size, statusCode;
14
+ address, output, size;
13
15
 
14
- window.setTimeout(function () {
15
- console.log("Shit's being weird no result within: " + time_out + "ms");
16
+ function error(msg) {
17
+ msg = msg || 'Unknown error';
18
+ console.log(msg);
16
19
  phantom.exit(1);
17
- }, time_out);
18
-
19
- try {
20
- f = fs.open(cookie_file, "r");
21
- cookies = JSON.parse(f.read());
22
- fs.remove(cookie_file)
23
- } catch (e) {
24
- console.log(e);
20
+ throw msg;
25
21
  }
26
- phantom.cookiesEnabled = true;
27
- phantom.cookies = cookies;
28
22
 
29
- if (system.args.length < 3 || system.args.length > 12) {
30
- console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out] [viewport_width] [viewport_height]');
23
+ function print_usage() {
24
+ console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out] [viewport_width] [viewport_height] [max_redirects_count]');
31
25
  console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
32
- phantom.exit(1);
33
- } else {
34
- address = system.args[1];
35
- output = system.args[2];
36
- page.viewportSize = { width: viewport_width, height: viewport_height };
37
- if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
38
- size = system.args[3].split('*');
39
- page.paperSize = size.length === 2 ? { width:size[0], height:size[1], margin:'0px' }
40
- : { format:system.args[3], orientation:orientation, margin:margin };
41
- }
42
- if (system.args.length > 4) {
43
- page.zoomFactor = system.args[4];
26
+ }
27
+
28
+ window.setTimeout(function () {
29
+ error("Shit's being weird no result within: " + time_out + "ms");
30
+ }, time_out);
31
+
32
+ function renderUrl(url, output, options) {
33
+ options = options || {};
34
+
35
+ var statusCode,
36
+ page = webpage.create();
37
+
38
+ for (var k in options) {
39
+ if (options.hasOwnProperty(k)) {
40
+ page[k] = options[k];
41
+ }
44
42
  }
45
43
 
46
44
  // determine the statusCode
47
45
  page.onResourceReceived = function (resource) {
48
- if (resource.url == address) {
46
+ if (resource.url == url) {
49
47
  statusCode = resource.status;
50
48
  }
51
49
  };
52
50
 
53
- page.open(address, function (status) {
51
+ page.onResourceError = function (resourceError) {
52
+ error(resourceError.errorString + ' (URL: ' + resourceError.url + ')');
53
+ };
54
+
55
+ page.onNavigationRequested = function (redirect_url, type, willNavigate, main) {
56
+ if (main) {
57
+ if (redirect_url !== url) {
58
+ page.close();
59
+
60
+ if (redirects_num-- >= 0) {
61
+ renderUrl(redirect_url, output, options);
62
+ } else {
63
+ error(url + ' redirects to ' + redirect_url + ' after maximum number of redirects reached');
64
+ }
65
+ }
66
+ }
67
+ };
68
+
69
+ page.open(url, function (status) {
54
70
  if (status !== 'success' || (statusCode != 200 && statusCode != null)) {
55
- console.log(statusCode, 'Unable to load the address!');
56
71
  if (fs.exists(output)) {
57
72
  fs.remove(output);
58
73
  }
59
74
  try {
60
75
  fs.touch(output);
76
+ } catch (e) {
77
+ console.log(e);
61
78
  }
62
- catch (e) {
63
- phantom.exit(1);
64
- throw e
65
- }
66
- phantom.exit(1);
79
+
80
+ error('Unable to load the URL: ' + url + ' (HTTP ' + statusCode + ')');
67
81
  } else {
68
82
  window.setTimeout(function () {
69
83
  page.render(output + '_tmp.pdf');
@@ -74,15 +88,49 @@ if (system.args.length < 3 || system.args.length > 12) {
74
88
 
75
89
  try {
76
90
  fs.move(output + '_tmp.pdf', output);
91
+ } catch (e) {
92
+ error(e);
77
93
  }
78
- catch (e) {
79
- console.log(e);
80
- phantom.exit(1);
81
- throw e
82
- }
83
- console.log('rendered to: ' + output, new Date().getTime());
84
- phantom.exit();
94
+ console.log('Rendered to: ' + output, new Date().getTime());
95
+ phantom.exit(0);
85
96
  }, render_time);
86
97
  }
87
98
  });
88
99
  }
100
+
101
+ if (cookie_file) {
102
+ try {
103
+ f = fs.open(cookie_file, "r");
104
+ cookies = JSON.parse(f.read());
105
+ fs.remove(cookie_file);
106
+ } catch (e) {
107
+ console.log(e);
108
+ }
109
+ phantom.cookiesEnabled = true;
110
+ phantom.cookies = cookies;
111
+ }
112
+
113
+ if (system.args.length < 3 || system.args.length > 13) {
114
+ print_usage() && phantom.exit(2);
115
+ } else {
116
+ address = system.args[1];
117
+ output = system.args[2];
118
+
119
+ page_options = {
120
+ viewportSize: {
121
+ width: viewport_width,
122
+ height: viewport_height
123
+ }
124
+ };
125
+
126
+ if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
127
+ size = system.args[3].split('*');
128
+ page_options.paperSize = size.length === 2 ? { width:size[0], height:size[1], margin:'0px' }
129
+ : { format:system.args[3], orientation:orientation, margin:margin };
130
+ }
131
+ if (system.args.length > 4) {
132
+ page_options.zoomFactor = system.args[4];
133
+ }
134
+
135
+ renderUrl(address, output, page_options);
136
+ }
@@ -1,3 +1,3 @@
1
1
  module Shrimp
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -58,10 +58,11 @@ describe Shrimp::Phantom do
58
58
  end
59
59
 
60
60
  it "should parse options into a cmd line" do
61
- phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => "2cm" }, { }, "#{Dir.tmpdir}/test.pdf")
61
+ phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => "2cm", :max_redirect_count => 10 }, { }, "#{Dir.tmpdir}/test.pdf")
62
62
  phantom.cmd.should include "test.pdf A4 1 2cm portrait"
63
63
  phantom.cmd.should include "file://#{testfile}"
64
64
  phantom.cmd.should include "lib/shrimp/rasterize.js"
65
+ phantom.cmd.should end_with " 10"
65
66
  end
66
67
 
67
68
  it "should properly escape arguments" do
@@ -130,7 +131,7 @@ describe Shrimp::Phantom do
130
131
  it "should be unable to load the address" do
131
132
  phantom = Shrimp::Phantom.new("file:///foo/bar")
132
133
  phantom.run
133
- phantom.error.should include "Unable to load the address"
134
+ phantom.error.should include "Error opening /foo/bar: No such file or directory (URL: file:///foo/bar)"
134
135
  end
135
136
 
136
137
  it "should be unable to copy file" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrimp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Kniep
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-21 00:00:00.000000000 Z
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json