Class: WebFont::Downloader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDownloader

Returns a new instance of Downloader.



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

def initialize
  @finder = Finder.new
end

Instance Attribute Details

#finderObject (readonly)

Returns the value of attribute finder.



4
5
6
# File 'lib/web_font/downloader.rb', line 4

def finder
  @finder
end

Instance Method Details

#download(font_family, destination_path, from_cache = true) ⇒ Object

Download font from Google and save it locally

Returns nothing



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/web_font/downloader.rb', line 13

def download(font_family, destination_path, from_cache = true)
  downloaded_fonts = []
  item             = finder.find(font_family)
  return downloaded_fonts if item.empty?

  font_family      = item['family'].gsub(/\s/, '-')
  item['files'].each do |variant, url|
    filename  = "#{font_family}-#{variant}#{File.extname(url)}"
    font_path = File.join(destination_path, filename)

    if from_cache && LocalCache.enable? && cache_path = LocalCache.path(filename)
      FileUtils.copy(cache_path, destination_path)
      downloaded_fonts << font_path
    else
      unless File.exist?(font_path)
        HTTP.get(url, font_path)
        LocalCache.save(font_path)
        downloaded_fonts << font_path
      end
    end
  end

  downloaded_fonts
end