grim 0.2.1 → 0.2.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.
data/lib/grim.rb CHANGED
@@ -6,10 +6,10 @@ module Grim
6
6
  WIDTH = 1024
7
7
 
8
8
  # Default image quality, 1 to 100
9
- QUALITY = 90
9
+ QUALITY = 75
10
10
 
11
11
  # Default density, any positive integer
12
- DENSITY = 300
12
+ DENSITY = 150
13
13
 
14
14
  # Default exception class for Grim.
15
15
  class Exception < ::StandardError
@@ -23,6 +23,10 @@ module Grim
23
23
  class PageNotFound < Grim::Exception
24
24
  end
25
25
 
26
+ # Exception that is raised if an empty path is passed to Grim::Page#save
27
+ class PathMissing < Grim::Exception
28
+ end
29
+
26
30
  # Creates and returns a new instance of Grim::Pdf
27
31
  #
28
32
  # path - a path string or object
data/lib/grim/page.rb CHANGED
@@ -19,6 +19,8 @@ module Grim
19
19
  # Tested on png and jpeg.
20
20
  #
21
21
  # path - String of the path to save to
22
+ # options - Hash of options to customize the saving of the image
23
+ # (ie: width, density, and quality)
22
24
  #
23
25
  # For example:
24
26
  #
@@ -27,11 +29,18 @@ module Grim
27
29
  #
28
30
  # Returns a File.
29
31
  #
30
- def save(path)
31
- SafeShell.execute("convert", "-resize", Grim::WIDTH, "-antialias", "-render",
32
- "-quality", Grim::QUALITY, "-colorspace", "RGB",
33
- "-interlace", "none", "-density", Grim::DENSITY,
32
+ def save(path, options={})
33
+ raise PathMissing if path.nil? || path !~ /\S/
34
+
35
+ width = options.fetch(:width, Grim::WIDTH)
36
+ density = options.fetch(:density, Grim::DENSITY)
37
+ quality = options.fetch(:quality, Grim::QUALITY)
38
+
39
+ SafeShell.execute("convert", "-resize", width, "-antialias", "-render",
40
+ "-quality", quality, "-colorspace", "RGB",
41
+ "-interlace", "none", "-density", density,
34
42
  "#{@pdf.path}[#{@index}]", path)
43
+
35
44
  File.exists?(path)
36
45
  end
37
46
 
data/lib/grim/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module Grim
3
- VERSION = "0.2.1" unless defined?(::Grim::VERSION)
3
+ VERSION = "0.2.2" unless defined?(::Grim::VERSION)
4
4
  end
@@ -13,20 +13,78 @@ describe Grim::Page do
13
13
 
14
14
  describe "#save" do
15
15
  before(:all) do
16
- pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
17
- pdf[0].save(tmp_path("to_png_spec.png"))
18
- @file = File.open(tmp_path("to_png_spec.png"))
16
+ @path = tmp_path("to_png_spec.png")
17
+ pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
18
+
19
+ pdf[0].save(@path)
19
20
  end
20
21
 
21
22
  it "should create the file" do
22
- File.exist?(tmp_path("to_png_spec.png")).should be_true
23
+ File.exist?(@path).should be_true
24
+ end
25
+
26
+ it "should use default width of 1024" do
27
+ width, height = dimensions_for_path(@path)
28
+ width.should == 1024
29
+ end
30
+ end
31
+
32
+ describe "#save with empty path" do
33
+ before(:each) do
34
+ @path = tmp_path("to_png_spec.png")
35
+ @pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
36
+ end
37
+
38
+ it "raises an exception" do
39
+ lambda { @pdf[0].save(nil) }.should raise_error(Grim::PathMissing)
40
+ lambda { @pdf[0].save(' ') }.should raise_error(Grim::PathMissing)
41
+ end
42
+ end
43
+
44
+ describe "#save with width option" do
45
+ before(:each) do
46
+ @path = tmp_path("to_png_spec.png")
47
+ pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
48
+
49
+ pdf[0].save(@path, :width => 20)
23
50
  end
24
51
 
25
- it "should have the right file size" do
26
- @file.stat.size.should == 188515
52
+ it "should set width" do
53
+ width, height = dimensions_for_path(@path)
54
+ width.should == 20
27
55
  end
28
56
  end
29
57
 
58
+ describe "#save with quality option" do
59
+ before(:each) do
60
+ @path = tmp_path("to_png_spec.jpg")
61
+ @pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
62
+ end
63
+
64
+ it "should use quality" do
65
+ @pdf[0].save(@path, :quality => 20)
66
+ lower_size = File.size(@path)
67
+
68
+ @pdf[0].save(@path, :quality => 90)
69
+ higher_size = File.size(@path)
70
+
71
+ (lower_size < higher_size).should be_true
72
+ end
73
+ end
74
+
75
+ describe "#save with density option" do
76
+ before(:each) do
77
+ @path = tmp_path("to_png_spec.jpg")
78
+ @pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
79
+ end
80
+
81
+ it "should use density" do
82
+ lower_time = Benchmark.realtime { @pdf[0].save(@path, :density => 20) }
83
+ higher_time = Benchmark.realtime { @pdf[0].save(@path, :density => 300) }
84
+
85
+ (lower_time < higher_time).should be_true
86
+ end
87
+ end
30
88
 
31
89
  describe "#text" do
32
90
  it "should return the text from the selected page" do
@@ -11,11 +11,11 @@ describe Grim do
11
11
  end
12
12
 
13
13
  it "should have QUALITY constant set to 90" do
14
- Grim::QUALITY.should == 90
14
+ Grim::QUALITY.should == 75
15
15
  end
16
16
 
17
17
  it "should have DENSITY constant set to 300" do
18
- Grim::DENSITY.should == 300
18
+ Grim::DENSITY.should == 150
19
19
  end
20
20
 
21
21
  describe "#new" do
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  # encoding: UTF-8
2
+ require 'benchmark'
2
3
  require 'rubygems'
3
4
  require 'bundler/setup'
4
5
 
5
6
  require 'grim'
6
7
 
7
- RSpec.configure do |config|
8
+ module FileHelpers
9
+ def dimensions_for_path(path)
10
+ width, height = `identify -format '%wx%h' #{path}`.strip.split('x').map(&:to_f)
11
+ end
12
+
8
13
  def fixture_path(name)
9
14
  path = File.expand_path("./spec/fixtures/")
10
15
  File.join(path, name)
@@ -19,4 +24,8 @@ RSpec.configure do |config|
19
24
  def tmp_path(name)
20
25
  File.join(tmp_dir, name)
21
26
  end
27
+ end
28
+
29
+ RSpec.configure do |config|
30
+ config.include(FileHelpers)
22
31
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grim
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jonathan Hoyt