Skip to content

Instantly share code, notes, and snippets.

@kurtsson
Last active January 1, 2016 18:09
Show Gist options
  • Save kurtsson/8182141 to your computer and use it in GitHub Desktop.
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.
<%
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