paperclip-compression 0.3.16 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b4944af7c4aa3cfa0a0b3b3c46f9ec47653f5c7
4
- data.tar.gz: e6e9d67da008304ec80183edde840479ddc18662
3
+ metadata.gz: 02a81c110df8dafcfc26ab4001e1cae33e0726d5
4
+ data.tar.gz: 0c02d5b5ac7fb8b114a9bb63f9bc9be88faf52b5
5
5
  SHA512:
6
- metadata.gz: c15645c4729be903286c3d6d4e8b838e5c2fd89b548546850de84e845c0b77153a186d47c16a15ef4d81fb974fadd2d94266b8d9f886fb0ac10965f4c13f89e6
7
- data.tar.gz: cdae34b6fad210d4acb4f2a43428bcca2b5fe523ed36af3beec4e004d24422f06c129801a205ca28ead6dbc77aff7114dc2acc63679d2cec716a6da9fbb1d62c
6
+ metadata.gz: '089b87628388b3da6db1c48bd5feb72ecaef07e98689238ef405c685778df26461ab15600c8ee18a96de96f53f8b078f9bc92c9d67be8c851e16c95e3513a47e'
7
+ data.tar.gz: 1da2739aa067ceca413c040f99f2e56a59ea02071949c09b2e49ef1b09ca7dc0da30ae6e8c22c27a80fc30b762dc7db56efb47d450db56a38eafd025031f2608
@@ -2,6 +2,7 @@ require 'paperclip'
2
2
  require 'os'
3
3
  require 'paperclip-compression/paperclip/compression'
4
4
  require 'paperclip-compression/base'
5
+ require 'paperclip-compression/config'
5
6
  require 'paperclip-compression/jpeg'
6
7
  require 'paperclip-compression/png'
7
8
 
@@ -1,10 +1,8 @@
1
1
  module PaperclipCompression
2
2
  class Base
3
3
 
4
- def initialize(file, first_processor, options = {})
4
+ def initialize(file, first_processor)
5
5
  @file = file
6
- @options = options
7
- @whiny = options.has_key?(:whiny) ? options[:whiny] : true
8
6
  current_extension = File.extname(file.path)
9
7
  @basename = File.basename(file.path, current_extension)
10
8
  @dst = Paperclip::TempfileFactory.new.generate("#{@basename}.png")
@@ -45,13 +43,6 @@ module PaperclipCompression
45
43
  first_processor? ? @dst : @file
46
44
  end
47
45
 
48
- def init_cli_opts(type, default_opts)
49
- # use default options in the papeclip config if exists, otherwise use gem defaults.
50
- default_opts = init_default_opts(Paperclip::Attachment.default_options, type, default_opts)
51
- # use processor_options if exists, otherwise use defaults.
52
- init_default_opts(@options[:processor_options], type, default_opts)
53
- end
54
-
55
46
  def command_path(command)
56
47
  folder = if OS.osx?
57
48
  'osx'
@@ -75,22 +66,6 @@ module PaperclipCompression
75
66
  @first_processor
76
67
  end
77
68
 
78
- def init_default_opts(opts, type, default_opts)
79
- if opts && (compression_opts = opts[:compression])
80
- if compression_opts.has_key?(type)
81
- if (type_opts = compression_opts[type])
82
- type_opts.kind_of?(String) ? type_opts : default_opts
83
- else
84
- false
85
- end
86
- else
87
- default_opts
88
- end
89
- else
90
- default_opts
91
- end
92
- end
93
-
94
69
  def copy_to_tempfile
95
70
  FileUtils.cp(@src_path, @dst_path)
96
71
  end
@@ -0,0 +1,85 @@
1
+ module PaperclipCompression
2
+ class Config
3
+
4
+ PROCESSOR_OPTIONS_KEY = :processor_options
5
+ KEY = :compression
6
+
7
+ def self.create_with_fallbacks(style_options, type_key, gem_defaults)
8
+ gem_config = new({ KEY => { type_key => gem_defaults } }, type_key, nil, nil)
9
+ defaults_config = new(Paperclip::Attachment.default_options, type_key, gem_config, Paperclip::Attachment.default_options[:whiny])
10
+ new(style_options[PROCESSOR_OPTIONS_KEY], type_key, defaults_config, style_options[:whiny])
11
+ end
12
+
13
+ def initialize(options, type_key, fallback, whiny)
14
+ @whiny = whiny
15
+ @fallback = fallback
16
+ parse_options(options, type_key)
17
+ end
18
+
19
+ def process_file?
20
+ if defined?(@process_file)
21
+ @process_file
22
+ elsif @fallback
23
+ @fallback.process_file?
24
+ else
25
+ raise('options or fallback should have attributes')
26
+ end
27
+ end
28
+
29
+ def command
30
+ if defined?(@command)
31
+ @command
32
+ elsif @fallback
33
+ @fallback.command
34
+ else
35
+ raise("options or fallback should have 'command'")
36
+ end
37
+ end
38
+
39
+ def options
40
+ if defined?(@options)
41
+ @options
42
+ elsif @fallback
43
+ @fallback.options
44
+ else
45
+ raise("options or fallback should have 'options'")
46
+ end
47
+ end
48
+
49
+ def whiny
50
+ @whiny
51
+ end
52
+
53
+ private
54
+
55
+ def parse_options(options, type_key)
56
+ if options && options.has_key?(KEY)
57
+ compression_opts = options[KEY]
58
+
59
+ if compression_opts.eql?(true)
60
+ @process_file = true
61
+ elsif compression_opts.eql?(false) || compression_opts.eql?(nil)
62
+ @process_file = false
63
+ elsif compression_opts.is_a?(Hash) && compression_opts.has_key?(type_key)
64
+ parse_type_options(compression_opts[type_key])
65
+ end
66
+ end
67
+ end
68
+
69
+ def parse_type_options(type_opts)
70
+ if type_opts
71
+ @process_file = true
72
+
73
+ if type_opts.is_a?(String)
74
+ @options = type_opts
75
+ elsif type_opts.is_a?(Hash)
76
+ @command = type_opts[:command] if type_opts.has_key?(:command)
77
+ @options = type_opts[:options] if type_opts.has_key?(:options)
78
+ end
79
+ else
80
+ @process_file = false
81
+ end
82
+ end
83
+
84
+ end
85
+ end
@@ -1,18 +1,19 @@
1
1
  module PaperclipCompression
2
2
  class Jpeg < Base
3
3
 
4
+ KEY = :jpeg
4
5
  JPEGTRAN_DEFAULT_OPTS = '-copy none -optimize -perfect'
5
6
 
6
7
  def initialize(file, first_processor, options = {})
7
- super(file, first_processor, options)
8
- @cli_opts = init_cli_opts(:jpeg, default_opts)
8
+ super(file, first_processor)
9
+ @config = PaperclipCompression::Config.create_with_fallbacks(options, KEY, gem_defaults)
9
10
  end
10
11
 
11
12
  def make
12
13
  begin
13
- process_file? ? process_file : unprocessed_tempfile
14
+ @config.process_file? ? process_file : unprocessed_tempfile
14
15
  rescue Cocaine::ExitStatusError => e
15
- raise Paperclip::Error, "JPEGTRAN : There was an error processing #{@basename}" if @whiny
16
+ raise Paperclip::Error, "JPEGTRAN : There was an error processing #{@basename}" if @config.whiny
16
17
  rescue Cocaine::CommandNotFoundError => e
17
18
  raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'jpegtran'. Please install jpegtran.")
18
19
  end
@@ -20,14 +21,19 @@ module PaperclipCompression
20
21
 
21
22
  private
22
23
 
23
- def default_opts
24
- JPEGTRAN_DEFAULT_OPTS
24
+ def gem_defaults
25
+ {
26
+ command: command_path('jpegtran'),
27
+ options: JPEGTRAN_DEFAULT_OPTS
28
+ }
25
29
  end
26
30
 
27
31
  def compress
28
- Paperclip.run(command_path('jpegtran'),
29
- "#{@cli_opts} :src_path > :dst_path",
30
- src_path: @src_path, dst_path: @dst_path)
32
+ Paperclip.run(
33
+ @config.command,
34
+ "#{@config.options} :src_path > :dst_path",
35
+ src_path: @src_path, dst_path: @dst_path
36
+ )
31
37
  end
32
38
  end
33
39
  end
@@ -1,18 +1,19 @@
1
1
  module PaperclipCompression
2
2
  class Png < Base
3
3
 
4
- OPTIPNG_DEFAULT_OPTS = '-o 5 -quiet'
4
+ KEY = :png
5
+ OPTIPNG_DEFAULT_OPTS = '-o 5 -quiet'
5
6
 
6
7
  def initialize(file, first_processor, options = {})
7
- super(file, first_processor, options)
8
- @cli_opts = init_cli_opts(:png, default_opts)
8
+ super(file, first_processor)
9
+ @config = PaperclipCompression::Config.create_with_fallbacks(options, KEY, gem_defaults)
9
10
  end
10
11
 
11
12
  def make
12
13
  begin
13
- process_file? ? process_file : unprocessed_tempfile
14
+ @config.process_file? ? process_file : unprocessed_tempfile
14
15
  rescue Cocaine::ExitStatusError => e
15
- raise Paperclip::Error, "OPTIPNG : There was an error processing #{@basename}" if @whiny
16
+ raise Paperclip::Error, "OPTIPNG : There was an error processing #{@basename}" if @config.whiny
16
17
  rescue Cocaine::CommandNotFoundError => e
17
18
  raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'optipng'. Please install optipng.")
18
19
  end
@@ -20,14 +21,19 @@ module PaperclipCompression
20
21
 
21
22
  private
22
23
 
23
- def default_opts
24
- OPTIPNG_DEFAULT_OPTS
24
+ def gem_defaults
25
+ {
26
+ command: command_path('optipng'),
27
+ options: OPTIPNG_DEFAULT_OPTS
28
+ }
25
29
  end
26
30
 
27
31
  def compress
28
- Paperclip.run(command_path('optipng'),
29
- "#{@cli_opts} -clobber :src_path -out :dst_path",
30
- src_path: @src_path, dst_path: @dst_path)
32
+ Paperclip.run(
33
+ @config.command,
34
+ "#{@config.options} -clobber :src_path -out :dst_path",
35
+ src_path: @src_path, dst_path: @dst_path
36
+ )
31
37
  end
32
38
  end
33
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-compression
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.16
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - "İ. Emre Kutlu"
7
+ - İ. Emre Kutlu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-27 00:00:00.000000000 Z
11
+ date: 2017-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paperclip
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.9.6
33
+ version: 1.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.9.6
40
+ version: 1.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -70,16 +70,30 @@ dependencies:
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: '2.14'
75
+ version: 3.7.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.7.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 4.7.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
81
95
  - !ruby/object:Gem::Version
82
- version: '2.14'
96
+ version: 4.7.3
83
97
  description: JPEG and PNG compression for Paperclip gem
84
98
  email: emrekutlu@gmail.com
85
99
  executables: []
@@ -99,6 +113,7 @@ files:
99
113
  - bin/win64/libjpeg-62.dll
100
114
  - lib/paperclip-compression.rb
101
115
  - lib/paperclip-compression/base.rb
116
+ - lib/paperclip-compression/config.rb
102
117
  - lib/paperclip-compression/jpeg.rb
103
118
  - lib/paperclip-compression/paperclip/compression.rb
104
119
  - lib/paperclip-compression/png.rb
@@ -122,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
137
  version: '0'
123
138
  requirements: []
124
139
  rubyforge_project:
125
- rubygems_version: 2.6.7
140
+ rubygems_version: 2.6.13
126
141
  signing_key:
127
142
  specification_version: 4
128
143
  summary: Image compression for Paperclip