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 +4 -4
- data/README.md +2 -2
- data/lib/web_font/downloader.rb +11 -7
- data/lib/web_font/finder.rb +1 -3
- data/lib/web_font/{data.rb → index.rb} +1 -4
- data/lib/web_font/local_cache.rb +21 -0
- data/lib/web_font/version.rb +1 -1
- data/lib/web_font.rb +7 -1
- data/test/data/fonts/Open-Sans-300.ttf +0 -0
- data/test/data/fonts/Open-Sans-300italic.ttf +0 -0
- data/test/data/fonts/Open-Sans-600.ttf +0 -0
- data/test/data/fonts/Open-Sans-600italic.ttf +0 -0
- data/test/data/fonts/Open-Sans-700.ttf +0 -0
- data/test/data/fonts/Open-Sans-700italic.ttf +0 -0
- data/test/data/fonts/Open-Sans-800.ttf +0 -0
- data/test/data/fonts/Open-Sans-800italic.ttf +0 -0
- data/test/data/fonts/Open-Sans-italic.ttf +0 -0
- data/test/data/fonts/Open-Sans-regular.ttf +0 -0
- data/test/data/fonts/suwannaphum.ttf +0 -0
- data/test/data/google/fonts.json +865 -172
- data/test/data/google/index.json +1 -1
- data/test/lib/web_font/downloader_test.rb +24 -4
- data/test/lib/web_font/{data_test.rb → index_test.rb} +5 -5
- data/test/lib/web_font/local_cache_test.rb +64 -0
- data/test/local_cache/suwannaphum.ttf +0 -0
- data/test/test_helper.rb +4 -1
- metadata +32 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a482019fbce41a01da44c56c1a1bf9f47f5f9cd
|
4
|
+
data.tar.gz: 803ef687fe868e65c5349372e4850d1f0d60b66b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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::
|
36
|
+
WebFont::Index.path = '/some/where/in/your/disk'
|
37
37
|
|
38
38
|
# First you need to have font index
|
39
|
-
WebFont::
|
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
|
data/lib/web_font/downloader.rb
CHANGED
@@ -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
|
22
|
-
|
23
|
-
font = "#{filename}#{extname}"
|
19
|
+
filename = "#{font_family}-#{variant}#{File.extname(url)}"
|
20
|
+
font_path = File.join(destination_path, filename)
|
24
21
|
|
25
|
-
|
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
|
data/lib/web_font/finder.rb
CHANGED
@@ -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::
|
49
|
+
path = File.join(WebFont::Index.path, filename)
|
52
50
|
File.open(path) { |file| JSON.parse(file.read) }
|
53
51
|
end
|
54
52
|
|
@@ -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
|
data/lib/web_font/version.rb
CHANGED
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/
|
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
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|