Class: Dimension::Scanner
- Inherits:
-
Object
- Object
- Dimension::Scanner
- Defined in:
- lib/dimension/scanner.rb
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Scanner
constructor
A new instance of Scanner.
- #process(content) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Scanner
Returns a new instance of Scanner.
9 10 11 12 13 |
# File 'lib/dimension/scanner.rb', line 9 def initialize(opts = {}) raise "Search root path required." unless opts[:root] @root = File.(opts.delete(:root)) @log_output = opts.delete(:log_output) || STDOUT end |
Instance Method Details
#process(content) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dimension/scanner.rb', line 15 def process(content) images = content.scan(/([a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif))/i) return [] if images.empty? paths = images.map { |img| URI.parse(img[0]).path }.uniq paths.map do |path| full = File.join(@root, path) if File.exist?(full) logger.info "Image exists in #{@root}: #{path}" next end logger.info "Image not found! #{path}" no_extension = File.basename(path, File.extname(path)) if geometry = no_extension[/((\d+)?x(\d+)?.?)$/i, 1] original = File.join(@root, path.sub(/-?#{geometry}/, '')) if File.exist?(original) if resize_image(original, geometry, full) logger.warn "Generated thumbnail for #{geometry} version or #{original}." full else logger.error "Unable to generate #{geometry} thumbnail for #{original}" nil end else logger.warn "Geometry #{geometry} was requested, but #{original} does not exist." nil end end end.compact end |