Class: RedditImageDownloader::Processor

Inherits:
Struct
  • Object
show all
Defined in:
lib/reddit_image_downloader/processor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



2
3
4
# File 'lib/reddit_image_downloader/processor.rb', line 2

def options
  @options
end

Class Method Details

.process!(options) ⇒ Object



3
4
5
# File 'lib/reddit_image_downloader/processor.rb', line 3

def self.process!(options)
  new(options).process!
end

Instance Method Details

#process!Object



7
8
9
10
11
12
13
14
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
# File 'lib/reddit_image_downloader/processor.rb', line 7

def process!
  listings.each do |listing|
    download = agent.get(listing["url"])

    if download.is_a?(Mechanize::Image)
      reader = Dimensions::Reader.new

      reader << download.body

      if reader.width >= options[:min_width] && reader.height >= options[:min_height]
        filename = "#{listing["id"]}.#{reader.type}"
        destination = File.join(options[:destination], filename)

        unless File.exists?(destination)
          download.save(destination)
        end
      end
    end
  end

  if options[:max_age]
    file_query = File.join(options[:destination], "*")

    Dir[file_query].each do |file|
      age = Time.now - File.ctime(file)
      days_old = age / (24 * 3600)

      if days_old > options[:max_age]
        File.delete(file)
      end
    end
  end
end