shrimp 0.0.1 → 0.0.2

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,9 @@
1
+ ## ChangeLog
2
+
3
+ 2012-12-18: Version 0.0.2
4
+ Improved Error handling
5
+ Improved Readme
6
+ Minor Bug fixes
7
+
8
+ 2012-12-17: Version 0.0.1
9
+ Initial launch.
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Shrimp
2
-
2
+ [![Build Status](https://travis-ci.org/adeven/shrimp.png?branch=master)](https://travis-ci.org/adeven/shrimp)
3
3
  Creates PDFs from URLs using phantomjs
4
4
 
5
5
  ## Installation
@@ -24,14 +24,16 @@ Or install it yourself as:
24
24
  ## Usage
25
25
 
26
26
  ```
27
+ require 'shrimp'
27
28
  url = 'http://www.google.com'
28
29
  options = { :margin => "1cm"}
29
- Phantomjs::Phantomjs.new(url, options).to_pdf("/output.pdf")
30
+ Shrimp::Phantom.new(url, options).to_pdf("~/output.pdf")
30
31
  ```
31
32
  ## Configuration
32
33
 
33
34
  ```
34
35
  Shrimp.configure do |config|
36
+
35
37
  # The path to the phantomjs executable
36
38
  # defaults to `where phantomjs`
37
39
  # config.phantomjs = '/usr/local/bin/phantomjs'
@@ -42,8 +44,8 @@ Shrimp.configure do |config|
42
44
 
43
45
  # the default margin
44
46
  # config.margin = '1cm'
45
- # the zoom factor
46
47
 
48
+ # the zoom factor
47
49
  # config.zoom = 1
48
50
 
49
51
  # the page orientation 'portrait' or 'landscape'
@@ -52,11 +54,13 @@ Shrimp.configure do |config|
52
54
  # a temporary dir used to store tempfiles
53
55
  # config.tmpdir = Dir.tmpdir
54
56
 
55
- # the timeout for phantomjs rendering process
56
- # rendering_timeout = 90000
57
-
58
- # the default rendering time
57
+ # the default rendering time in ms
58
+ # increase if you need to render very complex pages
59
59
  # config.rendering_time = 1000
60
+
61
+ # the timeout for the phantomjs rendering process in ms
62
+ # this needs always to be higher than rendering_time
63
+ # config.rendering_timeout = 90000
60
64
  end
61
65
  ```
62
66
 
@@ -76,7 +80,7 @@ Shrimp comes with a middleware that allows users to get a PDF view of any page o
76
80
 
77
81
  # in application.rb(Rails3) or environment.rb(Rails2)
78
82
  require 'shrimp'
79
- config.middleware.use PDFKit::Middleware
83
+ config.middleware.use Shrimp::Middleware
80
84
 
81
85
  **With Shrimp options**
82
86
 
@@ -109,9 +113,9 @@ you can setup the polling interval and the polling offset in seconds.
109
113
 
110
114
  ### Caching
111
115
 
112
- To avoid rendering the page on each request you can setup some the cache ttl on seconds
116
+ To avoid rendering the page on each request you can setup some the cache ttl in seconds
113
117
 
114
- config.middleware.use Shrimp::Middleware, :cache_ttl => 3600 # one hour
118
+ config.middleware.use Shrimp::Middleware, :cache_ttl => 3600, :out_path => "my/pdf/store"
115
119
 
116
120
 
117
121
  ### Ajax requests
@@ -126,7 +130,7 @@ To include some fancy Ajax stuff with jquery
126
130
  return window.location.assign(url);
127
131
  },
128
132
  504: function() {
129
- console.log("Shit's beeing wired"
133
+ console.log("Shit's beeing wired")
130
134
  },
131
135
  503: function(jqXHR, textStatus, errorThrown) {
132
136
  var wait;
@@ -1,3 +1,4 @@
1
+ require 'tmpdir'
1
2
  module Shrimp
2
3
  class Configuration
3
4
  attr_accessor :default_options
@@ -1,3 +1,5 @@
1
+ require 'uri'
2
+ require 'json'
1
3
  module Shrimp
2
4
  class NoExecutableError < StandardError
3
5
  def initialize
@@ -13,17 +15,39 @@ module Shrimp
13
15
  end
14
16
  end
15
17
 
18
+ class RenderingError < StandardError
19
+ def initialize(msg = nil)
20
+ super("Rendering Error: #{msg}")
21
+ end
22
+ end
16
23
 
17
24
  class Phantom
18
25
  attr_accessor :source, :configuration, :outfile
19
- attr_reader :options, :cookies
26
+ attr_reader :options, :cookies, :result, :error
20
27
  SCRIPT_FILE = File.expand_path('../rasterize.js', __FILE__)
21
28
 
22
29
  # Public: Runs the phantomjs binary
23
30
  #
24
31
  # Returns the stdout output of phantomjs
25
32
  def run
33
+ @error = nil
34
+ @result = `#{cmd}`
35
+ unless $?.exitstatus == 0
36
+ @error = @result
37
+ @result = nil
38
+ end
39
+ @result
40
+ end
41
+
42
+ def run!
43
+ @error = nil
26
44
  @result = `#{cmd}`
45
+ unless $?.exitstatus == 0
46
+ @error = @result
47
+ @result = nil
48
+ raise RenderingError.new(@error)
49
+ end
50
+ @result
27
51
  end
28
52
 
29
53
  # Public: Returns the phantom rasterize command
@@ -51,7 +75,7 @@ module Shrimp
51
75
  @source = Source.new(url_or_file)
52
76
  @options = Shrimp.configuration.default_options.merge(options)
53
77
  @cookies = cookies
54
- @outfile = outfile
78
+ @outfile = File.expand_path(outfile) if outfile
55
79
  raise NoExecutableError.new unless File.exists?(Shrimp.configuration.phantomjs)
56
80
  end
57
81
 
@@ -60,7 +84,7 @@ module Shrimp
60
84
  #
61
85
  # Returns the path to the pdf file
62
86
  def to_pdf(path=nil)
63
- @outfile = path
87
+ @outfile = File.expand_path(path) if path
64
88
  self.run
65
89
  @outfile
66
90
  end
@@ -79,8 +103,22 @@ module Shrimp
79
103
  #
80
104
  # Returns the binary string of the pdf
81
105
  def to_string(path=nil)
82
- self.to_pdf(path)
83
- File.open(path).read
106
+ File.open(self.to_pdf(path)).read
107
+ end
108
+
109
+ def to_pdf!(path=nil)
110
+ @outfile = File.expand_path(path) if path
111
+ self.run!
112
+ @outfile
113
+ end
114
+
115
+ def to_file!(path=nil)
116
+ self.to_pdf!(path)
117
+ File.new(@outfile)
118
+ end
119
+
120
+ def to_string!(path=nil)
121
+ File.open(self.to_pdf!(path)).read
84
122
  end
85
123
 
86
124
  private
@@ -54,8 +54,14 @@ if (system.args.length < 3 || system.args.length > 10) {
54
54
  if (fs.exists(output)) {
55
55
  fs.remove(output);
56
56
  }
57
- fs.touch(output);
58
- phantom.exit();
57
+ try {
58
+ fs.touch(output);
59
+ }
60
+ catch (e) {
61
+ phantom.exit(1);
62
+ throw e
63
+ }
64
+ phantom.exit(1);
59
65
  } else {
60
66
  window.setTimeout(function () {
61
67
  page.render(output + '_tmp.pdf');
@@ -64,7 +70,13 @@ if (system.args.length < 3 || system.args.length > 10) {
64
70
  fs.remove(output);
65
71
  }
66
72
 
67
- fs.move(output + '_tmp.pdf', output);
73
+ try {
74
+ fs.move(output + '_tmp.pdf', output);
75
+ }
76
+ catch (e) {
77
+ phantom.exit(1);
78
+ throw e
79
+ }
68
80
  console.log('rendered to: ' + output, new Date().getTime());
69
81
  phantom.exit();
70
82
  }, render_time);
@@ -1,3 +1,3 @@
1
1
  module Shrimp
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -12,6 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.summary = %q{a phantomjs based pdf renderer}
13
13
  gem.homepage = "http://github.com/adeven/shrimp"
14
14
  gem.files = `git ls-files`.split($/)
15
+ gem.files.reject! { |fn| fn.include? "script" }
15
16
  gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
16
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
18
  gem.require_paths = %w(lib)
@@ -99,4 +99,37 @@ describe Shrimp::Phantom do
99
99
  valid_pdf(@result)
100
100
  end
101
101
  end
102
+
103
+ context "Error" do
104
+ it "should return result nil" do
105
+ phantom = Shrimp::Phantom.new("file://foo/bar")
106
+ @result = phantom.run
107
+ @result.should be_nil
108
+ end
109
+
110
+ it "should be unable to load the address" do
111
+ phantom = Shrimp::Phantom.new("file:///foo/bar")
112
+ phantom.run
113
+ phantom.error.should include "Unable to load the address"
114
+ end
115
+
116
+ it "should be unable to copy file" do
117
+ phantom = Shrimp::Phantom.new("file://#{testfile}")
118
+ phantom.to_pdf("/foo/bar/")
119
+ phantom.error.should include "Unable to copy file "
120
+ end
121
+ end
122
+
123
+ context "Error Bang!" do
124
+
125
+ it "should be unable to load the address" do
126
+ phantom = Shrimp::Phantom.new("file:///foo/bar")
127
+ expect { phantom.run! }.to raise_error Shrimp::RenderingError
128
+ end
129
+
130
+ it "should be unable to copy file" do
131
+ phantom = Shrimp::Phantom.new("file://#{testfile}")
132
+ expect { phantom.to_pdf!("/foo/bar/") }.to raise_error Shrimp::RenderingError
133
+ end
134
+ end
102
135
  end
@@ -1,10 +1,6 @@
1
- require 'URI'
2
- require 'json'
3
- require 'rack'
4
1
  require 'rack/test'
5
2
  require 'shrimp'
6
3
 
7
-
8
4
  RSpec.configure do |config|
9
5
  include Rack::Test::Methods
10
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrimp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-17 00:00:00.000000000 Z
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70106322524180 !ruby/object:Gem::Requirement
16
+ requirement: &70155488162600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70106322524180
24
+ version_requirements: *70155488162600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70106322523660 !ruby/object:Gem::Requirement
27
+ requirement: &70155488162080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.9.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70106322523660
35
+ version_requirements: *70155488162080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70106322523140 !ruby/object:Gem::Requirement
38
+ requirement: &70155488161560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.2.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70106322523140
46
+ version_requirements: *70155488161560
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rack-test
49
- requirement: &70106322522660 !ruby/object:Gem::Requirement
49
+ requirement: &70155488161080 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 0.5.6
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70106322522660
57
+ version_requirements: *70155488161080
58
58
  description: html to pdf with phantomjs
59
59
  email:
60
60
  - manuel@adeven.com
@@ -64,6 +64,7 @@ extra_rdoc_files: []
64
64
  files:
65
65
  - .gitignore
66
66
  - .travis.yml
67
+ - ChangeLog.md
67
68
  - Gemfile
68
69
  - LICENSE.txt
69
70
  - README.md