web_font 0.0.7 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a65e98054069cb5a64f371a802a508112c17848
4
- data.tar.gz: df8c26b928c9c3cbf043d08d6476cb9ae4bb2836
3
+ metadata.gz: 2a482019fbce41a01da44c56c1a1bf9f47f5f9cd
4
+ data.tar.gz: 803ef687fe868e65c5349372e4850d1f0d60b66b
5
5
  SHA512:
6
- metadata.gz: bb6f2f62b2f78c74b3a1ccdb8f086d56622caa495f780c522ef9d71b057211d8d0b4b9149d8f894195d1dbdba42f5639445b3c85ce26b2de36f4296116e40885
7
- data.tar.gz: cdef8e8049135364074b9d3d51baa488e6dc1f48ee0a3b9de1208f81f71e67f986c7027e1217f516149ee4e8bd0d311c49cc0447430f94521bef5ea98931e067
6
+ metadata.gz: d037243c8047af105ac1fb1ef08b74f515d8d858488991c5fd15d7fd6af7d0fdd11395098e3da84d5f175fb4960b1cf72f2867e2396c072a2a9f6758c6560622
7
+ data.tar.gz: ccd9bddc563acf33f002777c30a15ebdb08ccd4cbe85a22de8cc12fa9b275c104d752082f6cf377b1bc95cb4150827ce6e6d692633dbd657414861de5e65230c
data/README.md CHANGED
@@ -33,10 +33,10 @@ To download font from Google, you need to have [Google API Key](https://www.goog
33
33
  require 'web_font'
34
34
 
35
35
  ENV['GOOGLE_API_KEY'] = '...'
36
- WebFont::Data.path = '/some/where/in/your/disk'
36
+ WebFont::Index.path = '/some/where/in/your/disk'
37
37
 
38
38
  # First you need to have font index
39
- WebFont::Data.download
39
+ WebFont::Index.download
40
40
 
41
41
  # Then you can download font 'Open Sans' and store it in /users/foo
42
42
  downloader = WebFont::Downloader.new
@@ -10,19 +10,23 @@ module WebFont
10
10
  # Download font from Google and save it locally
11
11
  #
12
12
  # Returns nothing
13
- def download(font_family, destination_path)
13
+ def download(font_family, destination_path, from_cache = true)
14
14
  item = finder.find(font_family)
15
-
16
15
  return if item.empty?
17
16
 
18
17
  font_family = item['family'].gsub(/\s/, '-')
19
-
20
18
  item['files'].each do |variant, url|
21
- filename = File.join(destination_path, "#{font_family}-#{variant}")
22
- extname = File.extname(url)
23
- font = "#{filename}#{extname}"
19
+ filename = "#{font_family}-#{variant}#{File.extname(url)}"
20
+ font_path = File.join(destination_path, filename)
24
21
 
25
- WebFont::Command.wget(url, font) unless File.exist?(font)
22
+ if from_cache && LocalCache.enable? && cache_path = LocalCache.path(filename)
23
+ FileUtils.copy(cache_path, destination_path)
24
+ else
25
+ unless File.exist?(font_path)
26
+ WebFont::Command.wget(url, font_path)
27
+ LocalCache.save(font_path)
28
+ end
29
+ end
26
30
  end
27
31
  end
28
32
  end
@@ -1,5 +1,3 @@
1
- require 'json'
2
-
3
1
  module WebFont
4
2
  class Finder
5
3
 
@@ -48,7 +46,7 @@ module WebFont
48
46
  end
49
47
 
50
48
  def read_json(filename)
51
- path = File.join(WebFont::Data.path, filename)
49
+ path = File.join(WebFont::Index.path, filename)
52
50
  File.open(path) { |file| JSON.parse(file.read) }
53
51
  end
54
52
 
@@ -1,8 +1,5 @@
1
- require 'json'
2
- require 'fileutils'
3
-
4
1
  module WebFont
5
- module Data
2
+ module Index
6
3
 
7
4
  class << self
8
5
  attr_accessor :path
@@ -0,0 +1,21 @@
1
+ module WebFont
2
+ module LocalCache
3
+ class << self
4
+ attr_accessor :cache, :cache_path
5
+
6
+ def path(filename)
7
+ path = File.join(cache_path, filename)
8
+ File.exist?(path) ? path : nil
9
+ end
10
+
11
+ def save(font)
12
+ filename = File.basename(font)
13
+ FileUtils.copy(font, File.join(cache_path, filename))
14
+ end
15
+
16
+ def enable?
17
+ cache && cache_path && Dir.exist?(cache_path)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module WebFont
2
- VERSION = '0.0.7'
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/web_font.rb CHANGED
@@ -1,8 +1,12 @@
1
+ require 'json'
2
+ require 'fileutils'
3
+
1
4
  require 'web_font/version'
2
5
  require 'web_font/command'
3
6
  require 'web_font/downloader'
4
- require 'web_font/data'
7
+ require 'web_font/index'
5
8
  require 'web_font/finder'
9
+ require 'web_font/local_cache'
6
10
 
7
11
  module WebFont
8
12
  def self.root
@@ -13,3 +17,5 @@ module WebFont
13
17
  File.expand_path('../../test', __FILE__)
14
18
  end
15
19
  end
20
+
21
+ WebFont::LocalCache.cache = true
Binary file
Binary file
Binary file
Binary file
Binary file