imgkit 1.3.3 → 1.3.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/README.md CHANGED
@@ -1,28 +1,3 @@
1
- # Patch Intention
2
-
3
- To overcome the lack of support for --user-style-sheet option by wkhtmltoimage 0.10.0 rc2 as reported here http://code.google.com/p/wkhtmltopdf/issues/detail?id=387
4
-
5
- require 'imgkit'
6
- require 'restclient'
7
- require 'stringio'
8
-
9
- url = 'http://domain/path/to/stylesheet.css'
10
- css = StringIO.new( RestClient.get(url) )
11
-
12
- kit = IMGKit.new(<<EOD)
13
- <!DOCTYPE HTML>
14
- <html lang="en">
15
- <head>
16
- <meta charset="UTF-8">
17
- <title>coolest converter</title>
18
- </head>
19
- <body>
20
- <div class="cool">image kit</div>
21
- </body>
22
- </html>
23
- EOD
24
-
25
- kit.stylesheets << css
26
1
 
27
2
  # IMGKit
28
3
 
@@ -129,6 +104,32 @@ You can respond in a controller with:
129
104
  This allows you to take advantage of rails page caching so you only generate the
130
105
  image when you need to.
131
106
 
107
+ ## --user-style-sheet workaround
108
+ To overcome the lack of support for --user-style-sheet option by wkhtmltoimage 0.10.0 rc2 as reported here http://code.google.com/p/wkhtmltopdf/issues/detail?id=387
109
+
110
+ require 'imgkit'
111
+ require 'restclient'
112
+ require 'stringio'
113
+
114
+ url = 'http://domain/path/to/stylesheet.css'
115
+ css = StringIO.new( RestClient.get(url) )
116
+
117
+ kit = IMGKit.new(<<EOD)
118
+ <!DOCTYPE HTML>
119
+ <html lang="en">
120
+ <head>
121
+ <meta charset="UTF-8">
122
+ <title>coolest converter</title>
123
+ </head>
124
+ <body>
125
+ <div class="cool">image kit</div>
126
+ </body>
127
+ </html>
128
+ EOD
129
+
130
+ kit.stylesheets << css
131
+
132
+
132
133
  ## Note on Patches/Pull Requests
133
134
 
134
135
  * Fork the project.
@@ -67,25 +67,49 @@ class IMGKit
67
67
  default.split('/').last
68
68
  end
69
69
  end
70
+
71
+ if Open3.method_defined? :capture3
72
+ def capture3(*opts)
73
+ Open3.capture3 *opts
74
+ end
75
+ else
76
+ # Lifted from ruby 1.9.2-p290 sources for ruby 1.8 compatibility
77
+ # and modified to work on 1.8
78
+ def capture3(*cmd, &block)
79
+ if Hash === cmd.last
80
+ opts = cmd.pop.dup
81
+ else
82
+ opts = {}
83
+ end
84
+
85
+ stdin_data = opts.delete(:stdin_data) || ''
86
+ binmode = opts.delete(:binmode)
87
+
88
+ Open3.popen3(*cmd) {|i, o, e|
89
+ if binmode
90
+ i.binmode
91
+ o.binmode
92
+ e.binmode
93
+ end
94
+ out_reader = Thread.new { o.read }
95
+ err_reader = Thread.new { e.read }
96
+ i.write stdin_data
97
+ i.close
98
+ [out_reader.value, err_reader.value]
99
+ }
100
+ end
101
+ end
70
102
 
71
103
  def to_img(format = nil)
72
104
  append_stylesheets
73
105
  set_format(format)
74
106
 
75
- result = nil
76
- stderr_output = nil
77
- Open3.popen3(*command) do |stdin,stdout,stderr|
78
- stdin << (@source.to_s) if @source.html?
79
- stdin.close
80
- result = stdout.gets(nil)
81
- result.force_encoding("ASCII-8BIT") if result.respond_to? :force_encoding
82
- stderr_output = stderr.readlines.join
83
- stdout.close
84
- stderr.close
85
- end
107
+ opts = @source.html? ? {:stdin_data => @source.to_s} : {}
108
+ result, stderr = capture3(*(command + [opts]))
109
+ result.force_encoding("ASCII-8BIT") if result.respond_to? :force_encoding
86
110
 
87
- raise CommandFailedError.new(command.join(' '), stderr_output) unless result
88
- return result
111
+ raise CommandFailedError.new(command.join(' '), stderr) if result.size == 0
112
+ result
89
113
  end
90
114
 
91
115
  def to_file(path)
@@ -1,3 +1,3 @@
1
1
  class IMGKit
2
- VERSION = "1.3.3"
2
+ VERSION = "1.3.4"
3
3
  end
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+ echo 'I have a problem' > /dev/stderr
3
+ exit 1
@@ -156,13 +156,23 @@ describe IMGKit do
156
156
  lambda { imgkit.to_img }.should raise_error(IMGKit::ImproperSourceError)
157
157
  end
158
158
 
159
+ def set_wkhtmltoimage_binary(binary)
160
+ spec_dir = File.dirname(__FILE__)
161
+ IMGKit.configuration.should_receive(:wkhtmltoimage).at_least(1).times.and_return(File.join(spec_dir, binary))
162
+ end
163
+
159
164
  it "should throw an error if the wkhtmltoimage command fails" do
160
- #stdin #stdout #stderr
161
- Open3.stub(:popen3).and_yield(StringIO.new, StringIO.new, StringIO.new("This failed, dude"))
165
+ set_wkhtmltoimage_binary 'error_binary'
162
166
  imgkit = IMGKit.new('http://www.example.com')
163
167
  lambda { imgkit.to_img }.should raise_error(IMGKit::CommandFailedError)
164
168
  end
165
169
 
170
+ it "should be able to handle lots of error output" do
171
+ set_wkhtmltoimage_binary 'warning_binary'
172
+ imgkit = IMGKit.new("<html><body>Hai!</body></html>")
173
+ imgkit.to_img.should == "result\n"
174
+ end
175
+
166
176
  context "when there is no format" do
167
177
  it "should fallback to jpg" do
168
178
  IMGKit.new("Hello, world").to_img.should be_a(:jpg)
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+
3
+ for i in {1..1000}; do
4
+ echo "Some kind of a long error message so that the operating system buffer gets filled" > /dev/stderr
5
+ done
6
+
7
+ echo "result"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imgkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.3.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: 2011-11-07 00:00:00.000000000Z
12
+ date: 2011-11-15 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Uses wkhtmltoimage to create Images using HTML
15
15
  email: christopher.continanza@gmail.com
@@ -35,11 +35,13 @@ files:
35
35
  - lib/imgkit/imgkit.rb
36
36
  - lib/imgkit/source.rb
37
37
  - lib/imgkit/version.rb
38
+ - spec/error_binary
38
39
  - spec/fixtures/example.css
39
40
  - spec/fixtures/example.html
40
41
  - spec/imgkit_spec.rb
41
42
  - spec/source_spec.rb
42
43
  - spec/spec_helper.rb
44
+ - spec/warning_binary
43
45
  homepage: http://rubygems.org/gems/imgkit
44
46
  licenses: []
45
47
  post_install_message: ! "******************************************************************\n\nNow
@@ -59,7 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
61
  version: '0'
60
62
  segments:
61
63
  - 0
62
- hash: -3563234315577198767
64
+ hash: -1173393730622892843
63
65
  required_rubygems_version: !ruby/object:Gem::Requirement
64
66
  none: false
65
67
  requirements:
@@ -73,8 +75,10 @@ signing_key:
73
75
  specification_version: 3
74
76
  summary: HTML+CSS -> JPG
75
77
  test_files:
78
+ - spec/error_binary
76
79
  - spec/fixtures/example.css
77
80
  - spec/fixtures/example.html
78
81
  - spec/imgkit_spec.rb
79
82
  - spec/source_spec.rb
80
83
  - spec/spec_helper.rb
84
+ - spec/warning_binary