web_font 0.0.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 +7 -0
- data/.gitignore +21 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/lib/web_font/data.rb +52 -0
- data/lib/web_font/downloader.rb +28 -0
- data/lib/web_font/finder.rb +52 -0
- data/lib/web_font/version.rb +3 -0
- data/lib/web_font.rb +14 -0
- data/test/data/google/fonts.json +11356 -0
- data/test/data/google/index.json +1 -0
- data/test/lib/web_font/data_test.rb +28 -0
- data/test/lib/web_font/downloader_test.rb +17 -0
- data/test/lib/web_font/finder_test.rb +21 -0
- data/test/test_helper.rb +5 -0
- data/web_font.gemspec +24 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3f9a92d63c598139f9a00c29f8f610befe4eca19
|
4
|
+
data.tar.gz: cb056753a367c7338c29c55f77061dc5d9c2a54d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1d2a4784faab9af68b8c43321e1f8ffd4a6a018b22eb5a54262f90be2c17b6eb009fbfa1f970ac5de5e2bc12766de6677d95c97cd493cea202078f47869fea5a
|
7
|
+
data.tar.gz: d25068638ca054c4eea04c7c7b50ccc5ac84688010b66791921b51b984b6de0167e9aacb3e570bf6f380268408bea04ce6b8e54f9d84d0f64a1c6b42ab7fbce2
|
data/.gitignore
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
.DS_Store
|
7
|
+
.ruby-version
|
8
|
+
.ruby-gemset
|
9
|
+
test/output/*
|
10
|
+
Gemfile.lock
|
11
|
+
InstalledFiles
|
12
|
+
_yardoc
|
13
|
+
coverage
|
14
|
+
doc/
|
15
|
+
lib/bundler/man
|
16
|
+
pkg
|
17
|
+
rdoc
|
18
|
+
spec/reports
|
19
|
+
test/tmp
|
20
|
+
test/version_tmp
|
21
|
+
tmp
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Sophy Eung
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# WebFont
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'web_font'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install web_font
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module WebFont
|
5
|
+
module Data
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :path
|
9
|
+
end
|
10
|
+
|
11
|
+
# Download fonts data from Google and index it
|
12
|
+
#
|
13
|
+
# Returns nothing
|
14
|
+
def self.download(destination = nil)
|
15
|
+
raise ArgumentError, "ENV['GOOGLE_API_KEY'] is nil" unless ENV['GOOGLE_API_KEY']
|
16
|
+
|
17
|
+
destination ||= path
|
18
|
+
FileUtils.mkdir_p(destination) unless Dir.exist?(destination)
|
19
|
+
|
20
|
+
url = "https://www.googleapis.com/webfonts/v1/webfonts?key=#{ENV['GOOGLE_API_KEY']}"
|
21
|
+
system("wget -O #{destination}/fonts.json #{url}")
|
22
|
+
|
23
|
+
index(destination)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# Index fonts data so it will speed things up when search
|
29
|
+
#
|
30
|
+
# Returns nothing
|
31
|
+
def self.index(destination)
|
32
|
+
indices = {}
|
33
|
+
alphabet = 'a'
|
34
|
+
indices[alphabet] = { 'start' => 0 }
|
35
|
+
|
36
|
+
hash = JSON.parse(File.open("#{destination}/fonts.json").read)
|
37
|
+
hash['items'].each_with_index do |item, index|
|
38
|
+
family = item['family']
|
39
|
+
|
40
|
+
if family[0].downcase != alphabet
|
41
|
+
indices[alphabet]['end'] = index - 1
|
42
|
+
|
43
|
+
alphabet = family[0].downcase
|
44
|
+
indices[alphabet] = { 'start' => index }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
indices[alphabet]['end'] = hash['items'].size - 1
|
48
|
+
|
49
|
+
File.open("#{destination}/index.json", 'w') { |file| file.write(indices.to_json) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module WebFont
|
2
|
+
class Downloader
|
3
|
+
|
4
|
+
attr_reader :finder
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@finder = WebFont::Finder.new
|
8
|
+
end
|
9
|
+
|
10
|
+
# Download font from Google and save it locally
|
11
|
+
#
|
12
|
+
# Returns nothing
|
13
|
+
def download(font_family, destination_path)
|
14
|
+
item = finder.find(font_family)
|
15
|
+
|
16
|
+
return if item.empty?
|
17
|
+
|
18
|
+
font_family = item['family'].gsub(/\s/, '-')
|
19
|
+
|
20
|
+
item['files'].each do |variant, url|
|
21
|
+
filename = File.join(destination_path, "#{font_family}-#{variant}")
|
22
|
+
extname = File.extname(url)
|
23
|
+
|
24
|
+
system("wget -O #{filename}#{extname} #{url}") unless File.exist?("#{filename}#{extname}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module WebFont
|
4
|
+
class Finder
|
5
|
+
|
6
|
+
attr_reader :indices, :items
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@indices = read_indices
|
10
|
+
@items = read_fonts['items']
|
11
|
+
end
|
12
|
+
|
13
|
+
# Find font in the index
|
14
|
+
#
|
15
|
+
# Returns hash
|
16
|
+
def find(font_family)
|
17
|
+
font_family = font_family.downcase
|
18
|
+
alphabet = font_family[0]
|
19
|
+
hash = indices[alphabet]
|
20
|
+
start = hash['start']
|
21
|
+
|
22
|
+
item = while start <= hash['end']
|
23
|
+
break items[start] if match?(font_family, items[start]['family'])
|
24
|
+
start += 1
|
25
|
+
end
|
26
|
+
|
27
|
+
item || {}
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def match?(font_family1, font_family2)
|
33
|
+
font_family2 = font_family2.downcase
|
34
|
+
font_family1 == font_family2 || font_family1 == font_family2.gsub(/\s/, '')
|
35
|
+
end
|
36
|
+
|
37
|
+
def read_json(filename)
|
38
|
+
file = File.open(File.join(WebFont::Data.path, filename))
|
39
|
+
JSON.parse(file.read)
|
40
|
+
ensure
|
41
|
+
file.close
|
42
|
+
end
|
43
|
+
|
44
|
+
def read_fonts
|
45
|
+
read_json('fonts.json')
|
46
|
+
end
|
47
|
+
|
48
|
+
def read_indices
|
49
|
+
read_json('index.json')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/web_font.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'web_font/version'
|
2
|
+
require 'web_font/downloader'
|
3
|
+
require 'web_font/data'
|
4
|
+
require 'web_font/finder'
|
5
|
+
|
6
|
+
module WebFont
|
7
|
+
def self.root
|
8
|
+
File.expand_path('..', __FILE__)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.test_root
|
12
|
+
File.expand_path('../../test', __FILE__)
|
13
|
+
end
|
14
|
+
end
|