Last active
January 1, 2016 18:09
-
-
Save kurtsson/8182141 to your computer and use it in GitHub Desktop.
Ruby method that creates and imports a web font. Preferably used in an .css.scss.erb file to download the css for a Google Font statically. This saves an http request during page load.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
require 'net/http' | |
def require_web_font(o={}) | |
o = {:family=>"Open Sans",:weights=>["500","500italic"],:url=>"http://fonts.googleapis.com/css"}.merge(o) | |
url = URI.parse(URI::encode("#{o[:url]}?family=#{o[:family]}:#{o[:weights] * ','}")) | |
Net::HTTP.start(url.host,url.port){|http| http.request(Net::HTTP::Get.new(url.to_s))}.body | |
end | |
%> | |
<%= require_web_font({ :family => "Droid Sans", | |
:weights => [ | |
"300", | |
"300italic", | |
"400", | |
"400italic", | |
"500", | |
"500italic", | |
"700", | |
"700italic" | |
]}) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment