rack-fontserve 0.1.0
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.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +53 -0
- data/Rakefile +11 -0
- data/lib/rack-fontserve.rb +66 -0
- data/lib/rack-fontserve/font.rb +69 -0
- data/lib/rack-fontserve/version.rb +5 -0
- data/lib/rack-fontserve/views/demo.erb +35 -0
- data/lib/rack-fontserve/views/stylesheet.erb +14 -0
- data/rack-fontserve.gemspec +27 -0
- data/test/fixtures/CustomCSS.css +3 -0
- data/test/fixtures/LicenseFont.css +14 -0
- data/test/fixtures/SampleFont.css +14 -0
- data/test/fixtures/fonts/AnotherFont/AnotherFont.otf +0 -0
- data/test/fixtures/fonts/AnotherFont/AnotherFont.svg +0 -0
- data/test/fixtures/fonts/AnotherFont/AnotherFont.ttf +0 -0
- data/test/fixtures/fonts/AnotherFont/AnotherFont.woff +0 -0
- data/test/fixtures/fonts/CustomCSS/CustomCSS.css +3 -0
- data/test/fixtures/fonts/CustomCSS/CustomCSS.ttf +0 -0
- data/test/fixtures/fonts/FileFont +0 -0
- data/test/fixtures/fonts/InvalidFont/WeirdNaming.otf +0 -0
- data/test/fixtures/fonts/LicenseFont/LICENSE +1 -0
- data/test/fixtures/fonts/LicenseFont/LicenseFont.eot +0 -0
- data/test/fixtures/fonts/LicenseFont/LicenseFont.ttf +0 -0
- data/test/fixtures/fonts/SampleFont/SampleFont.eot +0 -0
- data/test/fixtures/fonts/SampleFont/SampleFont.otf +0 -0
- data/test/fixtures/fonts/SampleFont/SampleFont.svg +0 -0
- data/test/fixtures/fonts/SampleFont/SampleFont.ttf +0 -0
- data/test/fixtures/fonts/SampleFont/SampleFont.woff +0 -0
- data/test/fixtures/fonts/SimpleFont/SimpleFont.ttf +0 -0
- data/test/helper.rb +102 -0
- data/test/test_font.rb +95 -0
- data/test/test_web.rb +63 -0
- metadata +155 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Christoph Olszowka
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
= Rack::Fontserve
|
2
|
+
|
3
|
+
A simple Sinatra-based font hosting rack application that utilizes HTTP caching and Allow-Access-Control-Origin properly
|
4
|
+
and generates the corresponding @font-face-CSS file on the fly.
|
5
|
+
|
6
|
+
== Running standalone
|
7
|
+
|
8
|
+
Create a Gemfile:
|
9
|
+
|
10
|
+
source :rubygems
|
11
|
+
gem 'rack-fontserve'
|
12
|
+
|
13
|
+
Create a config.ru:
|
14
|
+
|
15
|
+
require 'bundler/setup'
|
16
|
+
require 'rack-fontserve'
|
17
|
+
Rack::Fontserve.set :fonts_path, './fonts'
|
18
|
+
run Rack::Fontserve
|
19
|
+
|
20
|
+
Create a directory 'fonts', place your fonts inside there in subfolders, where each font format file has the same name
|
21
|
+
as the directory:
|
22
|
+
|
23
|
+
fonts/iconic_stroke/
|
24
|
+
+ iconic_stroke.eot
|
25
|
+
+ iconic_stroke.ttf
|
26
|
+
+ iconic_stroke.svg
|
27
|
+
|
28
|
+
That's it! Run with `rackup`, push to heroku, or mount it in an existing Rails 3 app by adding rack-fontserve to your gemfile,
|
29
|
+
setting the fonts path and mounting the app in your routes.rb file:
|
30
|
+
|
31
|
+
mount Rack::Fontserve, :at => "/fonts"
|
32
|
+
|
33
|
+
You can see a demo of all fonts in the fonts_path at /demo
|
34
|
+
|
35
|
+
== TODO
|
36
|
+
|
37
|
+
* Improve the CSS syntax (kill unneccessary blank lines)
|
38
|
+
* Make the scope selector for svg fonts configurable
|
39
|
+
* Add an "all.css" action that aggregates all fonts into one file
|
40
|
+
|
41
|
+
== Note on Patches/Pull Requests
|
42
|
+
|
43
|
+
* Fork the project.
|
44
|
+
* Make your feature addition or bug fix.
|
45
|
+
* Add tests for it. This is important so I don't break it in a
|
46
|
+
future version unintentionally.
|
47
|
+
* Commit, do not mess with rakefile, version, or history.
|
48
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
49
|
+
* Send me a pull request. Bonus points for topic branches.
|
50
|
+
|
51
|
+
== Copyright
|
52
|
+
|
53
|
+
Copyright (c) 2011 Christoph Olszowka. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class Fontserve < Sinatra::Base
|
5
|
+
class InvalidFontError < StandardError; end;
|
6
|
+
class InvalidFormatError < StandardError; end;
|
7
|
+
autoload :Font, 'rack-fontserve/font'
|
8
|
+
|
9
|
+
CONTENT_TYPES = {'ttf' => 'font/truetype',
|
10
|
+
'otf' => 'font/opentype',
|
11
|
+
'woff' => 'font/woff',
|
12
|
+
'eot' => 'application/vnd.ms-fontobject',
|
13
|
+
'svg' => 'image/svg+xml',
|
14
|
+
'css' => 'text/css;charset=utf-8'}
|
15
|
+
|
16
|
+
set :max_age, 365 * 24 * 60 * 60
|
17
|
+
set :views, ::File.join(::File.dirname(__FILE__), 'rack-fontserve/views')
|
18
|
+
|
19
|
+
not_found do
|
20
|
+
[404, '']
|
21
|
+
end
|
22
|
+
|
23
|
+
error(InvalidFontError) { not_found }
|
24
|
+
error(InvalidFormatError) { not_found }
|
25
|
+
|
26
|
+
helpers do
|
27
|
+
def prepare_headers(format)
|
28
|
+
set_content_type(format)
|
29
|
+
set_cache
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_content_type(format)
|
33
|
+
headers['Content-Type'] = CONTENT_TYPES[format.to_s]
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_cache
|
37
|
+
headers 'Cache-Control' => "public, max-age=#{Rack::Fontserve.max_age}",
|
38
|
+
'Expires' => (Time.now + Rack::Fontserve.max_age).httpdate,
|
39
|
+
'Access-Control-Allow-Origin' => '*'
|
40
|
+
end
|
41
|
+
|
42
|
+
def format?(f)
|
43
|
+
@font.formats.include?(f.to_s)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
get '/demo' do
|
48
|
+
erb :demo
|
49
|
+
end
|
50
|
+
|
51
|
+
# Render the generated or custom css for given font with caching
|
52
|
+
get '/:font_name.css' do
|
53
|
+
@font = Font.new(params[:font_name])
|
54
|
+
prepare_headers :css
|
55
|
+
@font.custom_css? ? @font.custom_css : erb(:stylesheet)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Render the font file for given font and format with caching
|
59
|
+
get '/:font_name.:format' do
|
60
|
+
@font = Font.new(params[:font_name])
|
61
|
+
@data = open(@font.format_path(params[:format]))
|
62
|
+
prepare_headers params[:format]
|
63
|
+
@data.read
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Representation of fonts in the Rack::Fontserve.fonts_path, including various
|
2
|
+
# helpers to check and retrieve formats, license and custom css
|
3
|
+
class Rack::Fontserve::Font
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
# Returns an array of all present and valid fonts
|
7
|
+
def self.all
|
8
|
+
Dir[File.join(Rack::Fontserve.fonts_path.to_s, '*')].map do |path|
|
9
|
+
begin
|
10
|
+
new(File.basename(path))
|
11
|
+
rescue Rack::Fontserve::InvalidFontError
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end.compact
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(name)
|
18
|
+
@name = File.basename(name) # Ensure we remove any path sections that might get burned into here
|
19
|
+
raise Rack::Fontserve::InvalidFontError unless valid?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns the base path of this font
|
23
|
+
def path
|
24
|
+
@path ||= File.join(Rack::Fontserve.fonts_path, name)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Retruns a list of available formats
|
28
|
+
def formats
|
29
|
+
@formats ||= Dir[File.join(path, "#{name}.{otf,svg,ttf,woff,eot}")].map {|file| File.extname(file)[1..-1] }.sort
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the path to the font file in given format. Raises InvalidFormatError if the font is not available in this format
|
33
|
+
def format_path(format)
|
34
|
+
raise Rack::Fontserve::InvalidFormatError unless formats.include?(format)
|
35
|
+
file_path "#{name}.#{format}"
|
36
|
+
end
|
37
|
+
|
38
|
+
# custom css file exists?
|
39
|
+
def custom_css?
|
40
|
+
File.exist? file_path("#{name}.css")
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns the content of the custom CSS file if present, nil otherwise
|
44
|
+
def custom_css
|
45
|
+
File.read file_path("#{name}.css") if custom_css?
|
46
|
+
end
|
47
|
+
|
48
|
+
# LICENSE file exists?
|
49
|
+
def license?
|
50
|
+
File.exist? file_path('LICENSE')
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns the content of the LICENSE file if present, nil otherwise
|
54
|
+
def license
|
55
|
+
File.read file_path('LICENSE') if license?
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Checks whether this font is valid - which is the case when the font has any formats present
|
61
|
+
def valid?
|
62
|
+
formats.count > 0
|
63
|
+
end
|
64
|
+
|
65
|
+
# Helper for paths to files in the font's home dir
|
66
|
+
def file_path(filename)
|
67
|
+
File.join(path, filename)
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,5 @@
|
|
1
|
+
# Define it as a plain constant instead of Bundler best-practice of
|
2
|
+
# Rack::Fontserve::VERSION since Fontserve is a class that inherits from Sinatra::Base
|
3
|
+
# and we'd be getting Superclass mismatch errors here since Sinatra is
|
4
|
+
# unavailable when evaluating this file standalone, i.e. in Rakefile
|
5
|
+
FONTSERVE_VERSION = '0.1.0'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<title>rack-fontserve demo</title>
|
4
|
+
<% Font.all.each do |font| %>
|
5
|
+
<link rel="stylesheet" href="/<%= font.name %>.css?nocache=<%= rand(500000000) * rand(5000)/(rand(2000)+3)+5 %>"/>
|
6
|
+
<% end %>
|
7
|
+
<style type="text/css">
|
8
|
+
body {
|
9
|
+
text-align: center;
|
10
|
+
font-size: 180%;
|
11
|
+
background: #ddd;
|
12
|
+
padding-top: 35px;
|
13
|
+
font-family: Helvetica, "Helvetica Neue", Arial, sans-serif;
|
14
|
+
}
|
15
|
+
h1,h2,h3 {
|
16
|
+
text-shadow: white 1px 1px 1px;
|
17
|
+
}
|
18
|
+
|
19
|
+
<% Font.all.each do |font| %>
|
20
|
+
.<%= font.name %> { font-family: '<%= font.name %>', Helvetica, Arial, sans-serif; }
|
21
|
+
<% end %>
|
22
|
+
</style>
|
23
|
+
</head>
|
24
|
+
|
25
|
+
<body>
|
26
|
+
<h1>rack-fontserve</h1>
|
27
|
+
<% Font.all.each do |font| %>
|
28
|
+
<div class="<%= font.name %>">
|
29
|
+
<h2><%= font.name %></h2>
|
30
|
+
<p>The quick brown fox jumps over the lazy dog (possibly)</p>
|
31
|
+
</div>
|
32
|
+
<% end %>
|
33
|
+
</body>
|
34
|
+
</html>
|
35
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<% if @font.license? %>/* LICENSE: <%= @font.license %> */<% end %>
|
2
|
+
@font-face {
|
3
|
+
font-family: '<%= @font.name %>';
|
4
|
+
<% if format?('eot') %>src: url('<%= "./#{@font.name}.eot" %>');<% end %>
|
5
|
+
src: local('☺'),
|
6
|
+
<% if format?('eot') %>url('<%= "./#{@font.name}.eot?iefix" %>') format('eot'), <% end %>
|
7
|
+
<% if format?('woff') %>url('<%= "./#{@font.name}.woff" %>') format('woff'), <% end %>
|
8
|
+
<% if format?('ttf') %>url('<%= "./#{@font.name}.ttf" %>') format('truetype'), <% end %>
|
9
|
+
<% if format?('otf') %>url('<%= "./#{@font.name}.otf" %>') format('opentype'), <% end %>
|
10
|
+
<% if format?('svg') %>url('<%= "./#{@font.name}.svg#webfont" %>') format('svg'), <% end %>
|
11
|
+
local('☺');
|
12
|
+
font-weight: normal;
|
13
|
+
font-style: normal;
|
14
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rack-fontserve/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rack-fontserve"
|
7
|
+
s.version = FONTSERVE_VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Christoph Olszowka"]
|
10
|
+
s.email = ["christoph (at) olszowka de"]
|
11
|
+
s.homepage = "https://github.com/colszowka/rack-fontserve"
|
12
|
+
s.summary = %q{Sinatra app for serving web fonts easily with proper caching and access-control headers}
|
13
|
+
s.description = %q{Sinatra app for serving web fonts easily with proper caching and access-control headers}
|
14
|
+
|
15
|
+
s.rubyforge_project = "rack-fontserve"
|
16
|
+
|
17
|
+
s.add_dependency 'sinatra', "~> 1.2.1"
|
18
|
+
|
19
|
+
s.add_development_dependency 'rack-test', "~> 0.5.7"
|
20
|
+
s.add_development_dependency 'shoulda', "~> 2.11.3"
|
21
|
+
s.add_development_dependency 'simplecov', '>= 0.4.0'
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
/* LICENSE: A super secret license */
|
2
|
+
@font-face {
|
3
|
+
font-family: 'LicenseFont';
|
4
|
+
src: url('./LicenseFont.eot');
|
5
|
+
src: local('☺'),
|
6
|
+
url('./LicenseFont.eot?iefix') format('eot'),
|
7
|
+
|
8
|
+
url('./LicenseFont.ttf') format('truetype'),
|
9
|
+
|
10
|
+
|
11
|
+
local('☺');
|
12
|
+
font-weight: normal;
|
13
|
+
font-style: normal;
|
14
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
@font-face {
|
3
|
+
font-family: 'SampleFont';
|
4
|
+
src: url('./SampleFont.eot');
|
5
|
+
src: local('☺'),
|
6
|
+
url('./SampleFont.eot?iefix') format('eot'),
|
7
|
+
url('./SampleFont.woff') format('woff'),
|
8
|
+
url('./SampleFont.ttf') format('truetype'),
|
9
|
+
url('./SampleFont.otf') format('opentype'),
|
10
|
+
url('./SampleFont.svg#webfont') format('svg'),
|
11
|
+
local('☺');
|
12
|
+
font-weight: normal;
|
13
|
+
font-style: normal;
|
14
|
+
}
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
A super secret license
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/test/helper.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'rack-fontserve'
|
7
|
+
|
8
|
+
require 'test/unit'
|
9
|
+
require 'shoulda'
|
10
|
+
require 'rack/test'
|
11
|
+
|
12
|
+
Rack::Fontserve.set :fonts_path, File.join(File.dirname(__FILE__), 'fixtures/fonts')
|
13
|
+
|
14
|
+
class Test::Unit::TestCase
|
15
|
+
include Rack::Test::Methods
|
16
|
+
|
17
|
+
def app
|
18
|
+
Rack::Fontserve
|
19
|
+
end
|
20
|
+
|
21
|
+
# Shortcut for making requests
|
22
|
+
def self.request(http_method, path)
|
23
|
+
raise ArgumentError, "Must be get, post, put or delete" unless %w(get post put delete).include?(http_method)
|
24
|
+
|
25
|
+
context "#{http_method} '#{path}'" do
|
26
|
+
setup { self.send(http_method, path) }
|
27
|
+
yield
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.get(path, &blk)
|
32
|
+
request('get', path, &blk)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.post(path, &blk)
|
36
|
+
request('post', path, &blk)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.should_respond_with(status)
|
40
|
+
should("respond with #{status}") { assert_equal status, last_response.status }
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.should_set_content_type_for(format)
|
44
|
+
formats = {'ttf' => 'font/truetype',
|
45
|
+
'otf' => 'font/opentype',
|
46
|
+
'woff' => 'font/woff',
|
47
|
+
'eot' => 'application/vnd.ms-fontobject',
|
48
|
+
'svg' => 'image/svg+xml',
|
49
|
+
'css' => 'text/css'
|
50
|
+
}
|
51
|
+
should_set_header 'Content-Type', formats[format.to_s]
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.should_set_header(name, value)
|
55
|
+
should "set header '#{name}' to '#{value}'" do
|
56
|
+
assert_equal value, last_response.headers[name]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.should_set_caching
|
61
|
+
should_set_header 'Cache-Control', 'public, max-age=31536000'
|
62
|
+
|
63
|
+
should "have set 'Expires' to '#{(Time.now + Rack::Fontserve.max_age).httpdate}'" do
|
64
|
+
assert_equal((Time.now + Rack::Fontserve.max_age).httpdate, last_response.headers['Expires'])
|
65
|
+
end
|
66
|
+
|
67
|
+
should "have set 'Access-Control-Allow-Origin' to '*'" do
|
68
|
+
assert_equal '*', last_response.headers['Access-Control-Allow-Origin']
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.should_not_set_caching
|
73
|
+
should "not have set 'Cache-Control' header" do
|
74
|
+
assert_nil last_response.headers['Cache-Control']
|
75
|
+
end
|
76
|
+
|
77
|
+
should "not have set 'Expires' header" do
|
78
|
+
assert_nil last_response.headers['Expires']
|
79
|
+
end
|
80
|
+
|
81
|
+
should "not have set 'Access-Control-Allow-Originl' header" do
|
82
|
+
assert_nil last_response.headers['Access-Control-Allow-Origin']
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.should_have_rendered_css(filename)
|
87
|
+
should "have rendered the body as expected in fixtures/#{filename}.css" do
|
88
|
+
expected_css = File.read(File.join(File.dirname(__FILE__), 'fixtures', "#{filename}.css"))
|
89
|
+
|
90
|
+
unless expected_css == last_response.body
|
91
|
+
puts last_response.body
|
92
|
+
begin
|
93
|
+
File.open("tmp.css", "w+") { |f| f.print last_response.body }
|
94
|
+
diff = `diff #{File.join(File.dirname(__FILE__), 'fixtures', "#{filename}.css")} tmp.css`
|
95
|
+
assert false, "Diff: #{diff}"
|
96
|
+
ensure
|
97
|
+
File.unlink('tmp.css')
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/test/test_font.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class FontTest < Test::Unit::TestCase
|
4
|
+
should "have a fonts path set" do
|
5
|
+
assert Rack::Fontserve.fonts_path =~ /fixtures\/fonts$/
|
6
|
+
end
|
7
|
+
|
8
|
+
context "Rack::Fontserve::Font.all" do
|
9
|
+
setup { @all = Rack::Fontserve::Font.all }
|
10
|
+
should("return 3 fonts") { assert_equal 5, @all.count }
|
11
|
+
should("have font 'SampleFont'") { assert @all.map(&:name).include?('SampleFont') }
|
12
|
+
should("have font 'AnotherFont'") { assert @all.map(&:name).include?('AnotherFont') }
|
13
|
+
should("have font 'SimpleFont'") { assert @all.map(&:name).include?('SimpleFont') }
|
14
|
+
should("have font 'CustomCSS'") { assert @all.map(&:name).include?('CustomCSS') }
|
15
|
+
should("have font 'LicenseFont'") { assert @all.map(&:name).include?('LicenseFont') }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "the font 'AnotherFont'" do
|
19
|
+
setup { @font = Rack::Fontserve::Font.new('AnotherFont') }
|
20
|
+
should("have 4 formats") { assert_equal 4, @font.formats.count }
|
21
|
+
should("have formats otf, svg, ttf, woff") { assert_equal %w(otf svg ttf woff), @font.formats }
|
22
|
+
|
23
|
+
should "return fonts_path/AnotherFont/AnotherFont.otf for format_path('otf')" do
|
24
|
+
assert_equal File.join(Rack::Fontserve.fonts_path, 'AnotherFont/AnotherFont.otf'), @font.format_path('otf')
|
25
|
+
end
|
26
|
+
|
27
|
+
should "Rack::Fontserve::InvalidFontError for format_path('eot')" do
|
28
|
+
assert_raise Rack::Fontserve::InvalidFormatError do
|
29
|
+
@font.format_path('eot')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
should "not have custom_css?" do
|
34
|
+
assert !@font.custom_css?
|
35
|
+
end
|
36
|
+
should "have nil for custom_css" do
|
37
|
+
assert_nil @font.custom_css
|
38
|
+
end
|
39
|
+
|
40
|
+
should "not have license?" do
|
41
|
+
assert !@font.license?
|
42
|
+
end
|
43
|
+
should "have nil for license" do
|
44
|
+
assert_nil @font.license
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "the font 'CustomCSS'" do
|
49
|
+
setup { @font = Rack::Fontserve::Font.new('CustomCSS') }
|
50
|
+
should("have 1 format") { assert_equal 1, @font.formats.count }
|
51
|
+
should("have format ttf") { assert_equal 'ttf', @font.formats.first }
|
52
|
+
|
53
|
+
should "have custom css" do
|
54
|
+
assert @font.custom_css?
|
55
|
+
end
|
56
|
+
should "return custom_css" do
|
57
|
+
assert_equal @font.custom_css, File.read(File.join(File.dirname(__FILE__), 'fixtures', "CustomCSS.css"))
|
58
|
+
end
|
59
|
+
|
60
|
+
should "not have license?" do
|
61
|
+
assert !@font.license?
|
62
|
+
end
|
63
|
+
should "have nil for license" do
|
64
|
+
assert_nil @font.license
|
65
|
+
end
|
66
|
+
|
67
|
+
should "return fonts_path/CustomCSS/CustomCSS.ttf for format_path('ttf')" do
|
68
|
+
assert_equal File.join(Rack::Fontserve.fonts_path, 'CustomCSS/CustomCSS.ttf'), @font.format_path('ttf')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
should "raise Rack::Fontserve::InvalidFontError when trying to load 'InvalidFont'" do
|
73
|
+
assert_raise Rack::Fontserve::InvalidFontError do
|
74
|
+
Rack::Fontserve::Font.new('InvalidFont')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
should "raise Rack::Fontserve::InvalidFontError when trying to load 'MissingFont'" do
|
79
|
+
assert_raise Rack::Fontserve::InvalidFontError do
|
80
|
+
Rack::Fontserve::Font.new('MissingFont')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
should "raise Rack::Fontserve::InvalidFontError when trying to load 'UnexistingFont'" do
|
85
|
+
assert_raise Rack::Fontserve::InvalidFontError do
|
86
|
+
Rack::Fontserve::Font.new('UnexistingFont')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
should "raise Rack::Fontserve::InvalidFontError when trying to load 'FileFont'" do
|
91
|
+
assert_raise Rack::Fontserve::InvalidFontError do
|
92
|
+
Rack::Fontserve::Font.new('FileFont')
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/test/test_web.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class WebTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
get '/' do
|
6
|
+
should_respond_with 404
|
7
|
+
should("render an empty string") { assert_equal '', last_response.body }
|
8
|
+
end
|
9
|
+
|
10
|
+
get '/SampleFont.css' do
|
11
|
+
should_respond_with 200
|
12
|
+
should_set_header 'Content-Type', 'text/css;charset=utf-8'
|
13
|
+
should_set_caching
|
14
|
+
should_have_rendered_css 'SampleFont'
|
15
|
+
end
|
16
|
+
|
17
|
+
get '/CustomCSS.css' do
|
18
|
+
should_respond_with 200
|
19
|
+
should_set_header 'Content-Type', 'text/css;charset=utf-8'
|
20
|
+
should_set_caching
|
21
|
+
should_have_rendered_css 'CustomCSS'
|
22
|
+
end
|
23
|
+
|
24
|
+
get '/LicenseFont.css' do
|
25
|
+
should_respond_with 200
|
26
|
+
should_set_header 'Content-Type', 'text/css;charset=utf-8'
|
27
|
+
should_set_caching
|
28
|
+
should_have_rendered_css 'LicenseFont'
|
29
|
+
end
|
30
|
+
|
31
|
+
%w(eot otf svg ttf woff).each do |format|
|
32
|
+
get "/SampleFont.#{format}" do
|
33
|
+
should_respond_with 200
|
34
|
+
should_set_content_type_for format
|
35
|
+
should_set_caching
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
%w(otf svg ttf woff).each do |format|
|
40
|
+
get "/AnotherFont.#{format}" do
|
41
|
+
should_respond_with 200
|
42
|
+
should_set_content_type_for format
|
43
|
+
should_set_caching
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
get '/AnotherFont.eot' do
|
48
|
+
should_respond_with 404
|
49
|
+
should_not_set_caching
|
50
|
+
end
|
51
|
+
|
52
|
+
%w(eot otf svg woff).each do |format|
|
53
|
+
get "/SimpleFont.#{format}" do
|
54
|
+
should_respond_with 404
|
55
|
+
should_not_set_caching
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
get '/demo' do
|
60
|
+
should_respond_with 200
|
61
|
+
should_set_header 'Content-Type', 'text/html;charset=utf-8'
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-fontserve
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christoph Olszowka
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-28 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: sinatra
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.2.1
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack-test
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 0.5.7
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: shoulda
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.11.3
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: simplecov
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 0.4.0
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
description: Sinatra app for serving web fonts easily with proper caching and access-control headers
|
61
|
+
email:
|
62
|
+
- christoph (at) olszowka de
|
63
|
+
executables: []
|
64
|
+
|
65
|
+
extensions: []
|
66
|
+
|
67
|
+
extra_rdoc_files: []
|
68
|
+
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.rdoc
|
74
|
+
- Rakefile
|
75
|
+
- lib/rack-fontserve.rb
|
76
|
+
- lib/rack-fontserve/font.rb
|
77
|
+
- lib/rack-fontserve/version.rb
|
78
|
+
- lib/rack-fontserve/views/demo.erb
|
79
|
+
- lib/rack-fontserve/views/stylesheet.erb
|
80
|
+
- rack-fontserve.gemspec
|
81
|
+
- test/fixtures/CustomCSS.css
|
82
|
+
- test/fixtures/LicenseFont.css
|
83
|
+
- test/fixtures/SampleFont.css
|
84
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.otf
|
85
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.svg
|
86
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.ttf
|
87
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.woff
|
88
|
+
- test/fixtures/fonts/CustomCSS/CustomCSS.css
|
89
|
+
- test/fixtures/fonts/CustomCSS/CustomCSS.ttf
|
90
|
+
- test/fixtures/fonts/FileFont
|
91
|
+
- test/fixtures/fonts/InvalidFont/WeirdNaming.otf
|
92
|
+
- test/fixtures/fonts/LicenseFont/LICENSE
|
93
|
+
- test/fixtures/fonts/LicenseFont/LicenseFont.eot
|
94
|
+
- test/fixtures/fonts/LicenseFont/LicenseFont.ttf
|
95
|
+
- test/fixtures/fonts/SampleFont/SampleFont.eot
|
96
|
+
- test/fixtures/fonts/SampleFont/SampleFont.otf
|
97
|
+
- test/fixtures/fonts/SampleFont/SampleFont.svg
|
98
|
+
- test/fixtures/fonts/SampleFont/SampleFont.ttf
|
99
|
+
- test/fixtures/fonts/SampleFont/SampleFont.woff
|
100
|
+
- test/fixtures/fonts/SimpleFont/SimpleFont.ttf
|
101
|
+
- test/helper.rb
|
102
|
+
- test/test_font.rb
|
103
|
+
- test/test_web.rb
|
104
|
+
has_rdoc: true
|
105
|
+
homepage: https://github.com/colszowka/rack-fontserve
|
106
|
+
licenses: []
|
107
|
+
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: "0"
|
125
|
+
requirements: []
|
126
|
+
|
127
|
+
rubyforge_project: rack-fontserve
|
128
|
+
rubygems_version: 1.5.3
|
129
|
+
signing_key:
|
130
|
+
specification_version: 3
|
131
|
+
summary: Sinatra app for serving web fonts easily with proper caching and access-control headers
|
132
|
+
test_files:
|
133
|
+
- test/fixtures/CustomCSS.css
|
134
|
+
- test/fixtures/LicenseFont.css
|
135
|
+
- test/fixtures/SampleFont.css
|
136
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.otf
|
137
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.svg
|
138
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.ttf
|
139
|
+
- test/fixtures/fonts/AnotherFont/AnotherFont.woff
|
140
|
+
- test/fixtures/fonts/CustomCSS/CustomCSS.css
|
141
|
+
- test/fixtures/fonts/CustomCSS/CustomCSS.ttf
|
142
|
+
- test/fixtures/fonts/FileFont
|
143
|
+
- test/fixtures/fonts/InvalidFont/WeirdNaming.otf
|
144
|
+
- test/fixtures/fonts/LicenseFont/LICENSE
|
145
|
+
- test/fixtures/fonts/LicenseFont/LicenseFont.eot
|
146
|
+
- test/fixtures/fonts/LicenseFont/LicenseFont.ttf
|
147
|
+
- test/fixtures/fonts/SampleFont/SampleFont.eot
|
148
|
+
- test/fixtures/fonts/SampleFont/SampleFont.otf
|
149
|
+
- test/fixtures/fonts/SampleFont/SampleFont.svg
|
150
|
+
- test/fixtures/fonts/SampleFont/SampleFont.ttf
|
151
|
+
- test/fixtures/fonts/SampleFont/SampleFont.woff
|
152
|
+
- test/fixtures/fonts/SimpleFont/SimpleFont.ttf
|
153
|
+
- test/helper.rb
|
154
|
+
- test/test_font.rb
|
155
|
+
- test/test_web.rb
|