font_assets 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +34 -0
- data/Rakefile +1 -0
- data/font_assets.gemspec +24 -0
- data/lib/font_assets.rb +7 -0
- data/lib/font_assets/middleware.rb +41 -0
- data/lib/font_assets/railtie.rb +20 -0
- data/lib/font_assets/version.rb +3 -0
- metadata +54 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
Font Assets
|
2
|
+
=============
|
3
|
+
|
4
|
+
This little gem helps serve font-face assets in Rails 3.1. It really does these two things:
|
5
|
+
|
6
|
+
* Registers font Mime Types for woff, eot, tff, and svg font files
|
7
|
+
* Sets Access-Control-Allow-Origin response headers for font assets, which Firefox requires for cross domain fonts
|
8
|
+
|
9
|
+
Install
|
10
|
+
-------
|
11
|
+
|
12
|
+
Add `font_assets` to your Gemfile:
|
13
|
+
|
14
|
+
gem 'font_assets'
|
15
|
+
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
|
20
|
+
Set the origin domain that will get set in the `Access-Control-Allow-Origin` header
|
21
|
+
|
22
|
+
# in config/environments/production.rb
|
23
|
+
|
24
|
+
config.font_assets.origin = 'http://codeschool.com'
|
25
|
+
|
26
|
+
The origin domain must match the domain of the site serving the page that is requesting the font, not the host of the font. For example, if you are using a CDN to serve your assets (like CloudFront), and the full path to the font asset is `http://d3rd6rvl24noqd.cloudfront.net/assets/fonts/Pacifico-webfont-734f1436e605566ae2f1c132905e28b2.woff`, but the URI the user is visiting if `http://coffeescript.codeschool.com/level/1`, you'd want to set the origin head to this:
|
27
|
+
|
28
|
+
config.font_assets.origin = 'http://coffeescript.codeschool.com'
|
29
|
+
|
30
|
+
|
31
|
+
License
|
32
|
+
-------
|
33
|
+
|
34
|
+
Font Assets is released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/font_assets.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "font_assets/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "font_assets"
|
7
|
+
s.version = FontAssets::VERSION
|
8
|
+
s.authors = ["Eric Allam"]
|
9
|
+
s.email = ["rubymaverick@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Improve font serving in Rails 3.1}
|
12
|
+
s.description = %q{Improve font serving in Rails 3.1}
|
13
|
+
|
14
|
+
s.rubyforge_project = "font_assets"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
data/lib/font_assets.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module FontAssets
|
2
|
+
class Middleware
|
3
|
+
|
4
|
+
def initialize(app, origin)
|
5
|
+
@app = app
|
6
|
+
@origin = origin
|
7
|
+
end
|
8
|
+
|
9
|
+
def access_control_headers
|
10
|
+
{
|
11
|
+
"Access-Control-Allow-Origin" => @origin,
|
12
|
+
"Access-Control-Allow-Methods" => "GET",
|
13
|
+
"Access-Control-Allow-Headers" => "x-requested-with",
|
14
|
+
"Access-Control-Max-Age" => "3628800"
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
# intercept the "preflight" request
|
20
|
+
if env["REQUEST_METHOD"] == "OPTIONS"
|
21
|
+
return [200, access_control_headers, []]
|
22
|
+
else
|
23
|
+
code, headers, body = @app.call(env)
|
24
|
+
headers.merge!(access_control_headers) if font_asset?(env["PATH_INFO"])
|
25
|
+
[code, headers, body]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def extension(path)
|
32
|
+
path.split("?").first.split(".").last
|
33
|
+
end
|
34
|
+
|
35
|
+
def font_asset?(path)
|
36
|
+
%w(woff eot tff svg).include? extension(path)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'font_assets/middleware'
|
2
|
+
|
3
|
+
module FontAssets
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
config.font_assets = ActiveSupport::OrderedOptions.new
|
6
|
+
|
7
|
+
initializer "font_assets.configure_rails_initialization" do |app|
|
8
|
+
config.font_assets.origin ||= "*"
|
9
|
+
|
10
|
+
app.middleware.insert_before 'ActionDispatch::Static', FontAssets::Middleware, config.font_assets.origin
|
11
|
+
end
|
12
|
+
|
13
|
+
config.after_initialize do
|
14
|
+
Rack::Mime::MIME_TYPES.merge! '.woff' => 'application/x-font-woff'
|
15
|
+
Rack::Mime::MIME_TYPES.merge! '.ttf' => 'application/x-font-ttf'
|
16
|
+
Rack::Mime::MIME_TYPES.merge! '.eot' => 'application/vnd.ms-fontobject'
|
17
|
+
Rack::Mime::MIME_TYPES.merge! '.svg' => 'image/svg+xml'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: font_assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Eric Allam
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-15 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Improve font serving in Rails 3.1
|
15
|
+
email:
|
16
|
+
- rubymaverick@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- font_assets.gemspec
|
26
|
+
- lib/font_assets.rb
|
27
|
+
- lib/font_assets/middleware.rb
|
28
|
+
- lib/font_assets/railtie.rb
|
29
|
+
- lib/font_assets/version.rb
|
30
|
+
homepage: ''
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project: font_assets
|
50
|
+
rubygems_version: 1.8.10
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Improve font serving in Rails 3.1
|
54
|
+
test_files: []
|