class Rack::Typekit

Constants

VERSION

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/typekit.rb, line 6
def initialize(app, options = {})
  raise ArgumentError, "Typekit Kit ID Required" unless options[:kit] &&
    !options[:kit].empty?
  @app, @options = app, options
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/typekit.rb, line 12
def call(env)
  @status, @headers, @response = @app.call(env)
  return [@status, @headers, @response] unless html?
  response = Rack::Response.new([], @status, @headers)
  if @response.respond_to?(:to_ary)
    @response.each { |fragment| response.write inject_typekit(fragment) }
  end
  response.finish
end

Private Instance Methods

html?() click to toggle source
# File lib/rack/typekit.rb, line 24
def html?; @headers["Content-Type"] =~ /html/; end
inject_typekit(response) click to toggle source
# File lib/rack/typekit.rb, line 26
    def inject_typekit(response)
      script = <<-EOF
<script src="//use.typekit.com/#{@options[:kit]}.js"></script>
<script>try{Typekit.load();}catch(e){}</script>
      EOF

      response.gsub(%r{</head>}, script + "</head>")
    end