Module: Birdy::ImageDownload

Included in:
Base
Defined in:
lib/birdy/image_download.rb

Constant Summary collapse

IMAGES =
"#{ENV['HOME']}/.config/birdy/images/"
ONE_DAY =
24 * 60 * 60

Instance Method Summary collapse

Instance Method Details

#download(image_url) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/birdy/image_download.rb', line 7

def download image_url
  FileUtils.mkdir_p IMAGES

  uri = URI.parse(image_url)
  Net::HTTP.start(uri.host) do |http|
    response = http.get(uri.path)
    @filename = IMAGES + File.split(uri.path)[1]
    yesterday = Time.now - ONE_DAY

    unless File.exists?(@filename) && File.mtime(@filename) > yesterday
      open(@filename, "wb") do |file|
        file.write(response.body)
      end
    end
  end

  @filename
end