Class: Hexapic::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapic/downloader.rb

Class Method Summary collapse

Class Method Details

.generate_file_path(filename = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/hexapic/downloader.rb', line 16

def self.generate_file_path(filename = nil)
  filename ||= (0...8).map { (65 + rand(26)).chr }.join + '.jpg'
  pictures_dir = File.join(Dir.home, 'Pictures', 'hexapic')
  FileUtils.mkdir_p(pictures_dir) unless Dir.exists? pictures_dir

  File.join(pictures_dir, filename)
end

.get(picture) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/hexapic/downloader.rb', line 3

def self.get(picture)
  file_path = self.generate_file_path(picture.filename)
  
  puts "Downloading #{picture.url}"
  File.open(file_path, 'w') do |f|
    f.puts Net::HTTP.get_response(URI.parse(picture.url)).body
  end

  res = picture.clone
  res.path = File.absolute_path(file_path)
  res
end