Class: BackgroundImages::Downloader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(find_image_of = "Brad Pitt's Ghost", save_to_file = "") ⇒ Downloader

Returns a new instance of Downloader.



5
6
7
8
# File 'lib/background_images/downloader.rb', line 5

def initialize(find_image_of="Brad Pitt's Ghost", save_to_file="")
  @find_image_of = find_image_of
  @save_to_file  = save_to_file
end

Instance Attribute Details

#download_url=(value) ⇒ Object

Sets the attribute download_url

Parameters:

  • value

    the value to set the attribute download_url to.



3
4
5
# File 'lib/background_images/downloader.rb', line 3

def download_url=(value)
  @download_url = value
end

#file_extension=(value) ⇒ Object

Sets the attribute file_extension

Parameters:

  • value

    the value to set the attribute file_extension to.



3
4
5
# File 'lib/background_images/downloader.rb', line 3

def file_extension=(value)
  @file_extension = value
end

#find_image_ofObject

Returns the value of attribute find_image_of.



3
4
5
# File 'lib/background_images/downloader.rb', line 3

def find_image_of
  @find_image_of
end

#save_to_fileObject

Returns the value of attribute save_to_file.



3
4
5
# File 'lib/background_images/downloader.rb', line 3

def save_to_file
  @save_to_file
end

Instance Method Details

#cleanObject



34
35
36
# File 'lib/background_images/downloader.rb', line 34

def clean
  `find . -type f -name "*.png" -exec convert {} -strip {} \;`
end

#downloadObject



10
11
12
13
# File 'lib/background_images/downloader.rb', line 10

def download
  `curl "#{download_url}" > "#{local_file}.#{file_extension}"`
  self
end

#standardize(selfie_image) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/background_images/downloader.rb', line 15

def standardize(selfie_image)
  dimensions = "#{selfie_image.page.width}x#{selfie_image.page.height}"

  `convert #{local_file}.#{file_extension} -resize '#{dimensions}^' -gravity 'center' -crop '#{dimensions}+0+0' #{local_file}.#{destination_extension}`

  begin
    image = Magick::Image.read("#{local_file}.#{destination_extension}")
  rescue
  end

  if command_failed? || image.nil?
    get_new_background_image
    download
    standardize(selfie_image)
  end

  self
end