Class: ImageOptimTolerance
- Inherits:
-
Object
- Object
- ImageOptimTolerance
- Defined in:
- lib/image_optim_tolerance/image_optim_tolerance.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ImageOptimTolerance
constructor
A new instance of ImageOptimTolerance.
- #is_valid_image(path) ⇒ Object
- #log(msg, type = nil) ⇒ Object
- #offenders(paths) ⇒ Object
- #verify(paths) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ImageOptimTolerance
Returns a new instance of ImageOptimTolerance.
10 11 12 |
# File 'lib/image_optim_tolerance/image_optim_tolerance.rb', line 10 def initialize( = {}) self. = DEFAULTS.merge() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/image_optim_tolerance/image_optim_tolerance.rb', line 8 def @options end |
Instance Method Details
#is_valid_image(path) ⇒ Object
14 15 16 17 18 |
# File 'lib/image_optim_tolerance/image_optim_tolerance.rb', line 14 def is_valid_image(path) return false if path.nil? or path.empty? valid_pattern = /.*\.(gif|png|jpeg|jpg|svg)$/ return ((valid_pattern =~ path.to_s.strip) && File.exist?(path)) end |
#log(msg, type = nil) ⇒ Object
20 21 22 23 24 |
# File 'lib/image_optim_tolerance/image_optim_tolerance.rb', line 20 def log(msg, type = nil) if self.[:verbose] puts (type && MARKS[type]) ? " #{MARKS[type]} #{msg}" : msg end end |
#offenders(paths) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/image_optim_tolerance/image_optim_tolerance.rb', line 26 def offenders(paths) offenders = { :terrible => [], :acceptable => [] } paths = [paths] if paths.is_a?(String) paths.each do |path| if self.is_valid_image(path) self.log "checking optimization of #{path}..." optimized = image_optim.optimize_image(path) if optimized size = {} size[:original] = File.size(path) size[:optimized] = File.size?(optimized) || size[:original] size[:saved] = size[:original] - size[:optimized] size[:percentage] = size[:saved].to_f / size[:original].to_f * 100 if size[:saved] > 0 self.log "could have saved #{size[:saved]} bytes (#{size[:percentage].ceil}%)!", :sad if size[:percentage] > self.[:tolerance] self.log "exceeds tolerance threshold by #{(size[:percentage] - self.[:tolerance]).ceil}%", :bad offenders[:terrible] << path else offenders[:acceptable] << path self.log "within acceptable tolerance", :good end next end else self.log "nothing to see here", :happy end end end # if there are terrible offenders that are above the threshold if offenders[:terrible].count > 0 # return both the terrible and acceptable, if we're being aggressive return offenders[:terrible] + offenders[:acceptable] if self.[:aggressive] return offenders[:terrible] end return nil end |
#verify(paths) ⇒ Object
70 71 72 |
# File 'lib/image_optim_tolerance/image_optim_tolerance.rb', line 70 def verify(paths) return self.offenders(paths).nil? end |