paperclip-deflater 0.0.1 → 0.0.2

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: b82e5941ea41f9e9d9a7e25a6ffcdd92480cb404
4
- data.tar.gz: b8c20ec4807349555c5a7e592761a2b4f13ff7b1
3
+ metadata.gz: 49bed0decc83cf5c31363c8157336688cad43572
4
+ data.tar.gz: b7422ea22f63a8bf21734bfbfa97093e325d40c3
5
5
  SHA512:
6
- metadata.gz: 6d2f11aa0a288acf3929d72bc01a5b728b714a53f645734742876d38a484ea67c64acf1392ef0894573e5c4e7defccf95d863998c6cd89e794255eee7e7c0ed1
7
- data.tar.gz: 49e7fc02bd3dc030477a1f615f941e68decc74a5bea6a655b78d5ae98f906122f4be4379afff8ba78649edd9339577b60831e0d9f5ee790783fe8e53b5a18ab8
6
+ metadata.gz: 3beb013f2adebd189bf8b5e0e9dc162b83c2a29b0caf228bded75a9c04a6b6d52db4847400fc0ba6cd5b374a319cc056d06084ed76b4b5ddbb18ce543272d90c
7
+ data.tar.gz: 5be6bef8b0bf9b82181195598cc953e3171fd7d950f487270ff4d407a2af4b247f144787f3a8725a35ae3d9c1cae8904efb99f9bc71f64de7d00786c6253538d
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -1,5 +1 @@
1
- require 'paperclip/processors/deflater'
2
-
3
- Paperclip.configure do |c|
4
- c.register_processor :deflater, Paperclip::Processors::Deflater
5
- end
1
+ require 'paperclip/deflater'
@@ -0,0 +1,8 @@
1
+ require 'paperclip'
2
+ require 'paperclip/processors/deflater'
3
+ require 'paperclip/processors/gzip'
4
+
5
+ Paperclip.configure do |c|
6
+ c.register_processor :deflater, Paperclip::Processors::Deflater
7
+ c.register_processor :gzip, Paperclip::Processors::Gzip
8
+ end
@@ -0,0 +1,5 @@
1
+ module Paperclip
2
+ module Deflater
3
+ VERSION = ::File.read(::File.expand_path('../../../../VERSION', __FILE__)).to_s.strip
4
+ end
5
+ end
@@ -1,39 +1,27 @@
1
- require 'active_model'
2
- require 'paperclip/processor'
3
- require 'zlib'
4
- require 'tempfile'
1
+ require_relative 'deflater_base'
5
2
 
6
3
  module Paperclip
7
4
  module Processors
8
- class Deflater < ::Paperclip::Processor
9
- VERSION = ::File.read(::File.expand_path('../../../../VERSION', __FILE__)).to_s.strip
10
-
5
+ class Deflater < DeflaterBase
11
6
  def initialize(file, options = {}, attachment = nil)
12
7
  super
13
- @format = @options[:format]
14
- @deflate_options = @options[:deflate_options] || {}
15
- @current_format = File.extname(@file.path)
16
- @basename = File.basename(@file.path, @current_format)
8
+ @deflate_options = @options[:deflate_options] || {}
17
9
  end
18
10
 
19
- def make
20
- src = @file
21
-
22
- should_deflate = @attachment.instance_read(:deflate)
23
- return src if should_deflate == false
11
+ private
24
12
 
13
+ def make_impl
25
14
  level = @deflate_options[:level]
26
15
  window_bits = @deflate_options[:window_bits]
27
16
  memlevel = @deflate_options[:memlevel]
28
17
  strategy = @deflate_options[:strategy]
29
18
 
30
- dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
31
- dst.binmode
19
+ dst = create_tempfile
32
20
  zd = Zlib::Deflate.new(level, window_bits, memlevel, strategy)
33
- dst.write zd.deflate(src.read)
21
+ dst.write zd.deflate(@file.read)
22
+ zd.close
34
23
  dst.flush
35
24
  dst.rewind
36
- zd.close
37
25
 
38
26
  dst
39
27
  end
@@ -0,0 +1,33 @@
1
+ require 'paperclip/processor'
2
+ require 'zlib'
3
+ require 'tempfile'
4
+
5
+ module Paperclip
6
+ module Processors
7
+ class DeflaterBase < ::Paperclip::Processor
8
+ def initialize(file, options = {}, attachment = nil)
9
+ super
10
+ @format = @options[:format]
11
+ @current_format = File.extname(@file.path)
12
+ @basename = File.basename(@file.path, @current_format)
13
+ end
14
+
15
+ def make
16
+ return @file if @attachment.instance_read(:deflate) == false
17
+ make_impl
18
+ end
19
+
20
+ private
21
+
22
+ def make_impl
23
+ raise NotImplementedError
24
+ end
25
+
26
+ def create_tempfile
27
+ f = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
28
+ f.binmode
29
+ f
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'deflater_base'
2
+
3
+ module Paperclip
4
+ module Processors
5
+ class Gzip < DeflaterBase
6
+ def initialize(file, options = {}, attachment = nil)
7
+ super
8
+ @gzip_options = @options[:gzip_options] || {}
9
+ end
10
+
11
+ private
12
+
13
+ def make_impl
14
+ level = @gzip_options[:level]
15
+ strategy = @gzip_options[:strategy]
16
+
17
+ dst = create_tempfile
18
+ gz = Zlib::GzipWriter.new(dst, level, strategy)
19
+ gz.write(@file.read)
20
+ gz.close
21
+ dst.open
22
+
23
+ dst
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,8 +1,8 @@
1
- require File.expand_path('../lib/paperclip/processors/deflater', __FILE__)
1
+ require File.expand_path('../lib/paperclip/deflater/version', __FILE__)
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "paperclip-deflater"
5
- gem.version = Paperclip::Processors::Deflater::VERSION
5
+ gem.version = Paperclip::Deflater::VERSION
6
6
  gem.platform = Gem::Platform::RUBY
7
7
  gem.authors = ["Daisuke Taniwaki"]
8
8
  gem.email = ["daisuketaniwaki@gmail.com"]
@@ -0,0 +1 @@
1
+ test
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Paperclip::Processors::DeflaterBase do
4
+ let(:attachment) { double }
5
+ let(:should_deflate) { true }
6
+ let(:options) { {} }
7
+ let(:file) { test_file }
8
+ subject { Paperclip::Processors::DeflaterBase.new(file, options, attachment) }
9
+ before do
10
+ allow(attachment).to receive(:instance_read).with(:deflate).and_return(should_deflate)
11
+ end
12
+ describe "#make" do
13
+ context "deflate setting is false" do
14
+ let(:should_deflate) { false }
15
+ it "returns original" do
16
+ expect(subject.make).to eq(file)
17
+ end
18
+ end
19
+ end
20
+ describe "private methods" do
21
+ describe "#make_impl" do
22
+ it "raises an exception" do
23
+ expect{ subject.send(:make_impl) }.to raise_error(NotImplementedError)
24
+ end
25
+ end
26
+ describe "create_tempfile" do
27
+ it "returns a tempfile" do
28
+ f = subject.send(:create_tempfile)
29
+ expect(f).to be_a Tempfile
30
+ expect(f.binmode?).to be true
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,27 +1,22 @@
1
+ # encoding: utf-8
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Paperclip::Processors::Deflater do
4
5
  let(:attachment) { double }
5
6
  let(:should_deflate) { true }
6
7
  let(:options) { {} }
7
- let(:file) { Tempfile.new('paperclip-processors-deflater') }
8
+ let(:file) { test_file }
8
9
  subject { Paperclip::Processors::Deflater.new(file, options, attachment) }
9
10
  before do
10
11
  allow(attachment).to receive(:instance_read).with(:deflate).and_return(should_deflate)
11
- file.write 'test'
12
- file.rewind
13
12
  end
14
- it "deflates the file" do
15
- dst = subject.make
16
- out = File.open(dst.path).read
17
- expect(out).to eq("x\x9C")
18
- end
19
- context "disable deflate" do
20
- let(:should_deflate) { false }
21
- it "does not deflate the file" do
22
- dst = subject.make
23
- out = File.open(dst.path).read
24
- expect(out).to eq("test")
13
+ describe "private methods" do
14
+ describe "#make_impl" do
15
+ it "deflates the file" do
16
+ dst = subject.make
17
+ data = dst.read
18
+ expect(data.unpack('H*').first).to eq("x\x9C".unpack('H*').first)
19
+ end
25
20
  end
26
21
  end
27
22
  end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Paperclip::Processors::Gzip do
5
+ let(:attachment) { double }
6
+ let(:should_deflate) { true }
7
+ let(:options) { {} }
8
+ let(:file) { test_file }
9
+ subject { Paperclip::Processors::Gzip.new(file, options, attachment) }
10
+ before do
11
+ allow(attachment).to receive(:instance_read).with(:deflate).and_return(should_deflate)
12
+ end
13
+ describe "private methods" do
14
+ describe "#make_impl" do
15
+ before do
16
+ allow(file).to receive(:mtime).and_return(Time.new(2000, 1, 2, 3, 4, 5))
17
+ end
18
+ it "deflates the file" do
19
+ dst = subject.make
20
+ data = dst.read
21
+ expect(data[0..3]).to eq("\u001F\x8B\b\u0000")
22
+ # data[4..7] is modified time and it changes
23
+ expect(data[8..-1]).to eq("\u0000\u0003+I-.\xE1\u0002\u0000\xC65\xB9;\u0005\u0000\u0000\u0000")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ def test_file
3
+ File.open(File.expand_path('../../fixtures/files/test.txt', __FILE__), 'r:utf-8')
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-deflater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Taniwaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-02 00:00:00.000000000 Z
11
+ date: 2014-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paperclip
@@ -81,10 +81,18 @@ files:
81
81
  - Rakefile
82
82
  - VERSION
83
83
  - lib/paperclip-deflater.rb
84
+ - lib/paperclip/deflater.rb
85
+ - lib/paperclip/deflater/version.rb
84
86
  - lib/paperclip/processors/deflater.rb
87
+ - lib/paperclip/processors/deflater_base.rb
88
+ - lib/paperclip/processors/gzip.rb
85
89
  - paperclip-deflater.gemspec
90
+ - spec/fixtures/files/test.txt
91
+ - spec/paperclip/processors/deflater_base_spec.rb
86
92
  - spec/paperclip/processors/deflater_spec.rb
93
+ - spec/paperclip/processors/gzip_spec.rb
87
94
  - spec/spec_helper.rb
95
+ - spec/support/files.rb
88
96
  homepage: https://github.com/dtaniwaki/paperclip-deflater
89
97
  licenses:
90
98
  - MIT
@@ -110,5 +118,9 @@ signing_key:
110
118
  specification_version: 4
111
119
  summary: Deflate Processor for Paperclip
112
120
  test_files:
121
+ - spec/fixtures/files/test.txt
122
+ - spec/paperclip/processors/deflater_base_spec.rb
113
123
  - spec/paperclip/processors/deflater_spec.rb
124
+ - spec/paperclip/processors/gzip_spec.rb
114
125
  - spec/spec_helper.rb
126
+ - spec/support/files.rb