Permalink
Browse files

Merge pull request #1 from phlipper/protocol-relative

Sweeping improvements to the library (thanks, @philipper!)
  • Loading branch information...
2 parents 1b1b64e + 3d24932 commit 57b340f9c056132ab54efef5d474c70b1e76d524 Mattt Thompson committed Apr 9, 2012
Showing with 109 additions and 49 deletions.
  1. +3 −1 .gitignore
  2. +2 −5 Gemfile
  3. +22 −0 README.md
  4. +0 −22 README.rdoc
  5. +13 −14 lib/rack/typekit.rb
  6. +15 −7 rack-typekit.gemspec
  7. +10 −0 spec/fixtures/test.html
  8. +44 −0 spec/rack_typekit_spec.rb
View
@@ -1,2 +1,4 @@
*.gem
-Gemfile.lock
+Gemfile.lock
+.rvmrc
+
View
@@ -1,7 +1,4 @@
source :rubygems
-gem "rack"
-
-group :development do
- gem "rake"
-end
+# Specify your gem's dependencies in rack-typekit.gemspec
+gemspec
View
@@ -0,0 +1,22 @@
+# rack-typekit
+
+Rack middleware to automatically include your Typekit JavaScript files
+
+## Example
+
+```ruby
+require "rack/typekit"
+
+use Rack::Typekit, :kit => "lng5bpe"
+```
+
+Including this in the `config.ru` file of your Rack application will automatically inject the corresponding JavaScript into the `<head>` of your HTML:
+
+```html
+<script src="//use.typekit.com/lng5bpe.js"></script>
+<script>try{Typekit.load();}catch(e){}</script>
+```
+
+## License
+
+rack-typekit is available under the MIT license. See the LICENSE file for more info.
View
@@ -1,22 +0,0 @@
-= rack-typekit
-
-Rack middleware to automatically include your Typekit JavaScript files
-
-== Example
-
- require 'rack/typekit'
-
- use Rack::Typekit, :kit => 'lng5bpe'
-
-Including this in the <tt>config.ru</tt> file of your Rack application will automatically inject the corresponding JavaScript into the <tt><head></tt> of your HTML:
-
- <script src="http://use.typekit.com/lng5bpe.js" type="text/javascript"></script><script type="text/javascript">
- //<![CDATA[
- try{Typekit.load();}catch(e){}
- //]]>
- </script>
- <script type="text/javascript">
-
-== License
-
-rack-typekit is available under the MIT license. See the LICENSE file for more info.
View
@@ -1,36 +1,35 @@
-require 'rack'
-
module Rack #:nodoc:
class Typekit
-
+
VERSION = "0.1.0"
def initialize(app, options = {})
- raise ArgumentError, "Typekit Kit ID Required" unless options[:kit] and !options[:kit].empty?
+ raise ArgumentError, "Typekit Kit ID Required" unless options[:kit] &&
+ !options[:kit].empty?
@app, @options = app, options
end
- def call(env); dup._call(env); end
-
- def _call(env)
+ 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) }
+ if @response.respond_to?(:to_ary)
+ @response.each { |fragment| response.write inject_typekit(fragment) }
+ end
response.finish
end
- private
+ private
- def html?; @headers['Content-Type'] =~ /html/; end
+ def html?; @headers["Content-Type"] =~ /html/; end
- def inject(response)
+ def inject_typekit(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>
+<script src="//use.typekit.com/#{@options[:kit]}.js"></script>
+<script>try{Typekit.load();}catch(e){}</script>
EOF
response.gsub(%r{</head>}, script + "</head>")
end
end
-end
+end
View
@@ -1,4 +1,6 @@
-require File.expand_path("../lib/rack/typekit", __FILE__)
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "rack/typekit"
Gem::Specification.new do |s|
s.name = "rack-typekit"
@@ -9,9 +11,15 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.summary = "rack-typekit"
s.description = "Rack middleware to automatically include your Typekit JavaScript files"
-
- s.required_rubygems_version = ">= 1.3.6"
-
- s.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md", "*.rdoc"]
- s.require_path = 'lib'
-end
+
+ s.add_runtime_dependency "rack", ">= 1.2.0", "<= 2.0.0"
+
+ s.add_development_dependency "minitest", "~> 2.11.4"
+ s.add_development_dependency "rack-test", "~> 0.6.1"
+ s.add_development_dependency "rake", "~> 0.9.2"
+
+ s.files = Dir["./**/*"].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
View
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Rack::Typekit Test</title>
+</head>
+<body>
+ <h1>Rack::Typekit</h1>
+ <p>Test file.</p>
+</body>
+</html>
View
@@ -0,0 +1,44 @@
+require "minitest/autorun"
+require "minitest/pride"
+require "rack"
+require "rack/lint"
+require "rack/mock"
+require "rack/typekit"
+
+describe Rack::Typekit do
+ let(:html_file) do
+ File.read(File.expand_path("../fixtures/test.html", __FILE__))
+ end
+ let(:html_app) { proc{[200,{"Content-Type"=>"text/html"},[html_file]]} }
+ let(:text_app) { proc{[200,{"Content-Type"=>"text/plain"},["FOO"]]} }
+ let(:typekit_html) { Rack::Typekit.new(html_app, :kit => "123") }
+ let(:typekit_text) { Rack::Typekit.new(text_app, :kit => "123") }
+
+ it { proc{ Rack::Typekit.new(nil) }.must_raise(ArgumentError) }
+
+ it { typekit_html.must_be_kind_of(Rack::Typekit) }
+ it { typekit_text.must_be_kind_of(Rack::Typekit) }
+
+ describe "script injection" do
+ describe "html" do
+ let(:request) { Rack::MockRequest.new(Rack::Lint.new(typekit_html)) }
+ let(:response) { request.get("/") }
+
+ it { response.status.must_equal 200 }
+ it { response.body.must_match %(<script src="//use.typekit.com/123.js"></script>) }
+ it { response.body.must_match "<script>try{Typekit.load();}catch(e){}</script>" }
+ it { response.body.must_match "<title>Rack::Typekit Test</title>" }
+ it { response.body.must_match "<p>Test file.</p>" }
+ end
+
+ describe "text" do
+ let(:request) { Rack::MockRequest.new(Rack::Lint.new(typekit_text)) }
+ let(:response) { request.get("/") }
+
+ it { response.status.must_equal 200 }
+ it { response.body.wont_match %(<script src="//use.typekit.com/123.js"></script>) }
+ it { response.body.wont_match "<script>try{Typekit.load();}catch(e){}</script>" }
+ it { response.body.must_match "FOO" }
+ end
+ end
+end

0 comments on commit 57b340f

Please sign in to comment.