Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit fe636b9eb96487c7ee6f9adfc326ab2cf3972ac1 @mattt committed Aug 6, 2011
Showing with 34 additions and 0 deletions.
  1. +34 −0 lib/rack/typekit.rb
@@ -0,0 +1,34 @@
+require 'rack'
+
+module Rack #:nodoc:
+ class TypeKit
+
+ def initialize(app, options = {})
+ raise ArgumentError, "TypeKit Kit ID Required" unless options[:kit] and !options[:kit].empty?
+ @app, @options = app, options
+ end
+
+ def call(env); dup._call(env); end
+
+ def _call(env)
+ @status, @headers, @response = @app.call(env)
+ return [@status, @headers, @response] unless html?
+ response = Rack::Response.new([], @status, @headers)
+ @response.each { |fragment| response.write inject(fragment) }
+ response.finish
+ end
+
+ private
+
+ def html?; @headers['Content-Type'] =~ /html/; end
+
+ def inject(response)
+ script = <<-EOF
+<script type="text/javascript" src="http://use.typekit.com/#{@options[:kit]}.js"></script>
+<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
+ EOF
+
+ response.gsub(%r{</head>}, script + "</head>")
+ end
+ end
+end

0 comments on commit fe636b9

Please sign in to comment.