imgkit 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ test.sh
data/Gemfile CHANGED
@@ -1,9 +1,10 @@
1
1
  source :rubygems
2
2
 
3
- group :development do
4
- gem "rspec", "~> 2.0.0.beta.8"
5
- gem "rspec-core", "~> 2.0.0.beta.8"
3
+ group :test do
4
+ gem "rspec", "~> 2"
5
+ gem "rspec-core", "~> 2"
6
6
  gem "mocha"
7
- gem "jeweler"
8
7
  gem "rack"
9
- end
8
+ gem "ruby-debug19", :platforms => :mri_19
9
+ gem "ruby-debug", :platforms => :mri_18
10
+ end
@@ -1,35 +1,45 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ archive-tar-minitar (0.5.2)
5
+ columnize (0.3.2)
4
6
  diff-lcs (1.1.2)
5
- gemcutter (0.6.1)
6
- git (1.2.5)
7
- jeweler (1.4.0)
8
- gemcutter (>= 0.1.0)
9
- git (>= 1.2.5)
10
- rubyforge (>= 2.0.0)
11
- json_pure (1.4.3)
12
- mocha (0.9.8)
13
- rake
14
- rack (1.2.1)
15
- rake (0.8.7)
16
- rspec (2.0.0.beta.19)
17
- rspec-core (= 2.0.0.beta.19)
18
- rspec-expectations (= 2.0.0.beta.19)
19
- rspec-mocks (= 2.0.0.beta.19)
20
- rspec-core (2.0.0.beta.19)
21
- rspec-expectations (2.0.0.beta.19)
22
- diff-lcs (>= 1.1.2)
23
- rspec-mocks (2.0.0.beta.19)
24
- rubyforge (2.0.4)
25
- json_pure (>= 1.1.7)
7
+ linecache (0.45)
8
+ linecache19 (0.5.12)
9
+ ruby_core_source (>= 0.1.4)
10
+ mocha (0.9.12)
11
+ rack (1.3.0)
12
+ rspec (2.6.0)
13
+ rspec-core (~> 2.6.0)
14
+ rspec-expectations (~> 2.6.0)
15
+ rspec-mocks (~> 2.6.0)
16
+ rspec-core (2.6.4)
17
+ rspec-expectations (2.6.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.6.0)
20
+ ruby-debug (0.10.4)
21
+ columnize (>= 0.1)
22
+ ruby-debug-base (~> 0.10.4.0)
23
+ ruby-debug-base (0.10.4)
24
+ linecache (>= 0.3)
25
+ ruby-debug-base19 (0.11.25)
26
+ columnize (>= 0.3.1)
27
+ linecache19 (>= 0.5.11)
28
+ ruby_core_source (>= 0.1.4)
29
+ ruby-debug19 (0.11.6)
30
+ columnize (>= 0.3.1)
31
+ linecache19 (>= 0.5.11)
32
+ ruby-debug-base19 (>= 0.11.19)
33
+ ruby_core_source (0.1.5)
34
+ archive-tar-minitar (>= 0.5.2)
26
35
 
27
36
  PLATFORMS
28
37
  ruby
29
38
 
30
39
  DEPENDENCIES
31
- jeweler
32
40
  mocha
33
41
  rack
34
- rspec (~> 2.0.0.beta.8)
35
- rspec-core (~> 2.0.0.beta.8)
42
+ rspec (~> 2)
43
+ rspec-core (~> 2)
44
+ ruby-debug
45
+ ruby-debug19
data/README.md CHANGED
@@ -25,9 +25,17 @@ Heavily based on [PDFKit](http://github.com/jdpace/pdfkit/).
25
25
 
26
26
  # Get the image BLOB
27
27
  img = kit.to_img
28
-
29
- # Save the JPG to a file
28
+
29
+ # New in 1.3!
30
+ img = kit.to_img(:jpg) #default
31
+ img = kit.to_img(:jpeg)
32
+ img = kit.to_img(:png)
33
+ img = kit.to_img(:tif)
34
+ img = kit.to_img(:tiff)
35
+
36
+ # Save the image to a file
30
37
  file = kit.to_file('/path/to/save/file.jpg')
38
+ file = kit.to_file('/path/to/save/file.png')
31
39
 
32
40
  # IMGKit.new can optionally accept a URL or a File.
33
41
  # Stylesheets can not be added when source is provided as a URL of File.
@@ -35,11 +43,18 @@ Heavily based on [PDFKit](http://github.com/jdpace/pdfkit/).
35
43
  kit = IMGKit.new(File.new('/path/to/html'))
36
44
 
37
45
  # Add any kind of option through meta tags
38
- IMGKit.new('<html><head><meta name="imgkit-quality" content="75")
46
+ IMGKit.new('<html><head><meta name="imgkit-quality" content="75"...
47
+
48
+ # Format shortcuts - New in 1.3!
49
+ IMGKit.new("hello").to_jpg
50
+ IMGKit.new("hello").to_jpeg
51
+ IMGKit.new("hello").to_png
52
+ IMGKit.new("hello").to_tif
53
+ IMGKit.new("hello").to_tiff
39
54
 
40
55
  ## Configuration
41
56
 
42
- If you're on Windows or you installed wkhtmltoimage by hand to a location other than /usr/local/bin you will need to tell PDFKit where the binary is. You can configure PDFKit like so:
57
+ If you're on Windows or you installed wkhtmltoimage by hand to a location other than /usr/local/bin you will need to tell IMGKit where the binary is. You can configure IMGKit like so:
43
58
 
44
59
  # config/initializers/imgkit.rb
45
60
  IMGKit.configure do |config|
@@ -47,9 +62,9 @@ If you're on Windows or you installed wkhtmltoimage by hand to a location other
47
62
  config.default_options = {
48
63
  :quality => 60
49
64
  }
65
+ config.default_format = :png
50
66
  end
51
67
 
52
-
53
68
  ## Rails
54
69
 
55
70
  ### Mime Types
@@ -58,11 +73,31 @@ register a .jpg mime type in:
58
73
  #config/initializers/mime_type.rb
59
74
  Mime::Type.register "image/jpeg", :jpg
60
75
 
76
+ register a .png mime type in:
77
+
78
+ #config/initializers/mime_type.rb
79
+ Mime::Type.register "image/png", :png
80
+
61
81
  ### Controller Actions
62
- You can then send JPGs with
82
+ You can respond in a controller with:
83
+
84
+ @kit = IMGKit.new(render_as_string)
63
85
 
64
86
  format.jpg do
65
- send_data(@kit.to_img, :type => "image/jpeg", :disposition => 'inline')
87
+ send_data(@kit.to_jpg, :type => "image/jpeg", :disposition => 'inline')
88
+ end
89
+
90
+ - or -
91
+
92
+ format.png do
93
+ send_data(@kit.to_png, :type => "image/png", :disposition => 'inline')
94
+ end
95
+
96
+ - or -
97
+
98
+ respond_to do |format|
99
+ send_data(@kit.to_img(format.to_sym),
100
+ :type => "image/png", :disposition => 'inline')
66
101
  end
67
102
 
68
103
  This allows you to take advantage of rails page caching so you only generate the
data/Rakefile CHANGED
@@ -1,2 +1,15 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+ Bundler.require
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
8
+
9
+ desc "Run specs"
10
+ RSpec::Core::RakeTask.new do |t|
11
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
12
+ # Put spec opts in a file named .rspec in root
13
+ end
14
+
15
+ task :specs => :spec
@@ -1,11 +1,12 @@
1
1
  class IMGKit
2
2
  class Configuration
3
- attr_accessor :meta_tag_prefix, :wkhtmltoimage, :default_options
3
+ attr_accessor :meta_tag_prefix, :wkhtmltoimage, :default_options, :default_format
4
4
 
5
5
  def initialize
6
6
  @meta_tag_prefix = 'imgkit-'
7
- @wkhtmltoimage = '/usr/local/bin/wkhtmltoimage'
7
+ @wkhtmltoimage = '/usr/local/bin/wkhtmltoimage'
8
8
  @default_options = {}
9
+ @default_format = :jpg
9
10
  end
10
11
  end
11
12
 
@@ -1,4 +1,5 @@
1
1
  class IMGKit
2
+ KNOWN_FORMATS = [:jpg, :jpeg, :png, :tiff, :tif]
2
3
 
3
4
  class NoExecutableError < StandardError
4
5
  def initialize
@@ -22,6 +23,12 @@ class IMGKit
22
23
  super("Command failed: #{command}: #{stderr}")
23
24
  end
24
25
  end
26
+
27
+ class UnknownFormatError < StandardError
28
+ def initialize(format)
29
+ super("Unknown Format: #{format}")
30
+ end
31
+ end
25
32
 
26
33
  attr_accessor :source, :stylesheets
27
34
  attr_reader :options
@@ -33,14 +40,13 @@ class IMGKit
33
40
 
34
41
  @options = IMGKit.configuration.default_options.merge(options)
35
42
  @options.merge! find_options_in_meta(url_file_or_html) unless source.url?
36
- @options = normalize_options(@options)
37
43
 
38
44
  raise NoExecutableError.new unless File.exists?(IMGKit.configuration.wkhtmltoimage)
39
45
  end
40
46
 
41
47
  def command
42
48
  args = [executable]
43
- args += @options.to_a.flatten.compact
49
+ args += normalize_options(@options).to_a.flatten.compact
44
50
 
45
51
  if @source.html?
46
52
  args << '-' # Get HTML from stdin
@@ -62,8 +68,9 @@ class IMGKit
62
68
  end
63
69
  end
64
70
 
65
- def to_img
71
+ def to_img(format = nil)
66
72
  append_stylesheets
73
+ set_format(format)
67
74
 
68
75
  result = nil
69
76
  stderr_output = nil
@@ -71,6 +78,7 @@ class IMGKit
71
78
  stdin << (@source.to_s) if @source.html?
72
79
  stdin.close
73
80
  result = stdout.gets(nil)
81
+ result.force_encoding("ASCII-8BIT") if result.respond_to? :force_encoding
74
82
  stderr_output = stderr.readlines.join
75
83
  stdout.close
76
84
  stderr.close
@@ -81,7 +89,17 @@ class IMGKit
81
89
  end
82
90
 
83
91
  def to_file(path)
84
- File.open(path,'w') {|file| file << self.to_img}
92
+ format = File.extname(path).gsub(/^\./,'').to_sym
93
+ set_format(format)
94
+ File.open(path,'w:ASCII-8BIT') {|file| file << self.to_img}
95
+ end
96
+
97
+ def method_missing(name, *args, &block)
98
+ if(m = name.to_s.match(/^to_(\w+)/))
99
+ self.send(:to_img, m[1].to_sym)
100
+ else
101
+ super
102
+ end
85
103
  end
86
104
 
87
105
  protected
@@ -146,5 +164,10 @@ class IMGKit
146
164
  value.to_s
147
165
  end
148
166
  end
149
-
167
+
168
+ def set_format(format)
169
+ format = IMGKit.configuration.default_format unless format
170
+ @options.merge!(:format => format.to_s) unless @options[:format]
171
+ raise UnknownFormatError.new(format) unless KNOWN_FORMATS.include?(@options[:format].to_sym)
172
+ end
150
173
  end
@@ -1,3 +1,3 @@
1
1
  class IMGKit
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -21,11 +21,6 @@ describe IMGKit do
21
21
  imgkit.source.to_s.should == file_path
22
22
  end
23
23
 
24
- it "should parse the options into a cmd line friedly format" do
25
- imgkit = IMGKit.new('html', :quality => 75)
26
- imgkit.options.should have_key('--quality')
27
- end
28
-
29
24
  it "should provide no default options" do
30
25
  imgkit = IMGKit.new('<h1>Oh Hai</h1>')
31
26
  imgkit.options.should be_empty
@@ -49,13 +44,17 @@ describe IMGKit do
49
44
  imgkit.command[0].should include('wkhtmltoimage')
50
45
  imgkit.command.should include('-')
51
46
  end
47
+
48
+ it "should parse the options into a cmd line friedly format" do
49
+ imgkit = IMGKit.new('html', :quality => 75)
50
+ imgkit.command.should include('--quality')
51
+ end
52
52
 
53
53
  it "will not include default options it is told to omit" do
54
54
  imgkit = IMGKit.new('html')
55
55
  imgkit = IMGKit.new('html', :disable_smart_shrinking => false)
56
56
  imgkit.command.should_not include('--disable-smart-shrinking')
57
57
  end
58
-
59
58
  it "should encapsulate string arguments in quotes" do
60
59
  imgkit = IMGKit.new('html', :header_center => "foo [page]")
61
60
  imgkit.command[imgkit.command.index('--header-center') + 1].should == 'foo [page]'
@@ -98,7 +97,7 @@ describe IMGKit do
98
97
  end
99
98
  end
100
99
 
101
- context "#to_img" do
100
+ context "#to_img(format = nil)" do
102
101
  def filetype_of(img)
103
102
  result = nil
104
103
  tmpfile = Tempfile.new('imgkit')
@@ -150,9 +149,71 @@ describe IMGKit do
150
149
  end
151
150
 
152
151
  it "should throw an error if the wkhtmltoimage command fails" do
153
- imgkit = IMGKit.new('http://www.hopefully.this.site.never.exists.asjdhjkalshgflkahfsglkahfdlg11.com')
152
+ #stdin #stdout #stderr
153
+ Open3.stub(:popen3).and_yield(StringIO.new, StringIO.new, StringIO.new("This failed, dude"))
154
+ imgkit = IMGKit.new('http://www.example.com')
154
155
  lambda { imgkit.to_img }.should raise_error(IMGKit::CommandFailedError)
155
156
  end
157
+
158
+ context "when there is no format" do
159
+ it "should fallback to jpg" do
160
+ IMGKit.new("Hello, world").to_img.should be_a(:jpg)
161
+ end
162
+
163
+ context "when a default_format has been configured" do
164
+ before do
165
+ IMGKit.configure do |config|
166
+ config.default_format = :png
167
+ end
168
+ end
169
+
170
+ after do
171
+ IMGKit.configure do |config|
172
+ config.default_format = :jpg
173
+ end
174
+ end
175
+
176
+ it "should use the configured format" do
177
+ IMGKit.new("Oh hai!").to_img.should be_a(:png)
178
+ end
179
+ end
180
+ end
181
+
182
+ context "when format = :jpg" do
183
+ it "should create a jpg" do
184
+ IMGKit.new("Hello, world").to_img(:jpg).should be_a(:jpg)
185
+ end
186
+ end
187
+
188
+ context "when format is a known format" do
189
+ it "should create an image with that format" do
190
+ IMGKit::KNOWN_FORMATS.each do |format|
191
+ IMGKit.new("Hello, world").to_img(format).should be_a(format)
192
+ end
193
+ end
194
+ end
195
+
196
+ context "when format is unknown" do
197
+ it "should raise an UnknownFormatError" do
198
+ lambda { IMGKit.new("Hello, world").to_img(:blah) }.should raise_error(IMGKit::UnknownFormatError)
199
+ end
200
+ end
201
+ end
202
+
203
+ context "#to_<known_format>" do
204
+ IMGKit::KNOWN_FORMATS.each do |format|
205
+ describe "#to_#{format}" do
206
+ it "should create a #{format}" do
207
+ IMGKit.new("Hello").send("to_#{format}").should be_a(format)
208
+ end
209
+ end
210
+ end
211
+ end
212
+
213
+ context "#to_<unkown_format>" do
214
+ it "should raise and UnknownFormatError" do
215
+ lambda { IMGKit.new("Hello, world").to_blah }.should raise_error(IMGKit::UnknownFormatError)
216
+ end
156
217
  end
157
218
 
158
219
  context "#to_file" do
@@ -162,7 +223,7 @@ describe IMGKit do
162
223
  end
163
224
 
164
225
  after do
165
- File.delete(@file_path)
226
+ File.delete(@file_path) if File.exist?(@file_path)
166
227
  end
167
228
 
168
229
  it "should create a file with the result of :to_img as content" do
@@ -172,6 +233,29 @@ describe IMGKit do
172
233
  file.should be_instance_of(File)
173
234
  File.read(file.path).should == 'CONTENT'
174
235
  end
236
+
237
+ IMGKit::KNOWN_FORMATS.each do |format|
238
+ it "should use the extension #{format} as the format" do
239
+ @file_path = File.join(SPEC_ROOT,'fixtures',"test.#{format}")
240
+ imgkit = IMGKit.new('html', :quality => 50)
241
+ file = imgkit.to_file(@file_path)
242
+ file.should be_instance_of(File)
243
+ File.open(file.path, "r:ASCII-8BIT") { |f| f.read.should be_a(format) }
244
+ end
245
+ end
246
+
247
+ it "should raise UnknownFormatError when format is unknown" do
248
+ kit = IMGKit.new("html")
249
+ lambda {
250
+ kit.to_file("file.bad_format")
251
+ }.should raise_error(IMGKit::UnknownFormatError)
252
+ end
253
+
254
+ it "should not create the file if format is unknown" do
255
+ kit = IMGKit.new("html")
256
+ kit.to_file("file.bad_format") rescue nil
257
+ File.exist?("file.bad_format").should be_false
258
+ end
175
259
  end
176
260
 
177
261
  context "security" do
@@ -9,11 +9,40 @@ require 'rack'
9
9
  require 'tempfile'
10
10
 
11
11
  RSpec.configure do |config|
12
-
13
12
  config.before do
14
13
  IMGKit.any_instance.stubs(:wkhtmltoimage).returns(
15
14
  File.join(SPEC_ROOT,'..','bin','wkhtmltoimage-proxy')
16
15
  )
17
16
  end
18
-
17
+ end
18
+
19
+ module MagicNumber
20
+ extend self
21
+ JPG = "\xFF\xD8\xFF\xE0"
22
+ JPEG = JPG
23
+ PNG = "\x89\x50\x4e\x47"
24
+ TIFF = "\x49\x49\x2a\x00"
25
+ TIF = TIFF
26
+ GIF = "\x47\x49\x46\x38"
27
+
28
+
29
+ if "".respond_to?(:force_encoding)
30
+ constants.each { |c| const_get(c).force_encoding("ASCII-8BIT") }
31
+ end
32
+
33
+ def read(string)
34
+ string[0,4]
35
+ end
36
+ end
37
+
38
+ RSpec::Matchers.define :be_a do |expected|
39
+ match do |actual|
40
+ @expected = MagicNumber.const_get(expected.to_s.upcase)
41
+ MagicNumber.read(actual) == @expected
42
+ end
43
+
44
+ failure_message_for_should do |actual|
45
+ actual = MagicNumber.read(actual)
46
+ "expctected #{actual.inspect},#{actual.encoding} to equal #{@expected.inspect},#{@expected.encoding}"
47
+ end
19
48
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imgkit
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 2
8
- - 1
9
- version: 1.2.1
4
+ prerelease:
5
+ version: 1.3.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - csquared
@@ -14,8 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-05-20 00:00:00 -05:00
18
- default_executable:
13
+ date: 2011-06-21 00:00:00 Z
19
14
  dependencies: []
20
15
 
21
16
  description: Uses wkhtmltoimage to create Images using HTML
@@ -49,7 +44,6 @@ files:
49
44
  - spec/imgkit_spec.rb
50
45
  - spec/source_spec.rb
51
46
  - spec/spec_helper.rb
52
- has_rdoc: true
53
47
  homepage: http://rubygems.org/gems/imgkit
54
48
  licenses: []
55
49
 
@@ -72,6 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
66
  requirements:
73
67
  - - ">="
74
68
  - !ruby/object:Gem::Version
69
+ hash: 2095953912074654249
75
70
  segments:
76
71
  - 0
77
72
  version: "0"
@@ -80,13 +75,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
75
  requirements:
81
76
  - - ">="
82
77
  - !ruby/object:Gem::Version
83
- segments:
84
- - 0
85
78
  version: "0"
86
79
  requirements: []
87
80
 
88
81
  rubyforge_project: imgkit
89
- rubygems_version: 1.3.7
82
+ rubygems_version: 1.8.4
90
83
  signing_key:
91
84
  specification_version: 3
92
85
  summary: HTML+CSS -> JPG