Permalink
Browse files

Initial commit

  • Loading branch information...
akahn committed Apr 6, 2010
0 parents commit 65156a22d573d91cac150e1926921a97551cd918
Showing with 58 additions and 0 deletions.
  1. +17 −0 example_app.rb
  2. +13 −0 lib/sinatra/subdomain.rb
  3. +28 −0 test/subdomain_test.rb
@@ -0,0 +1,17 @@
+require 'sinatra'
+require 'sinatra/subdomain'
+
+# This is an example application that uses sinatra-subdomain. Start it using
+# `shotgun -I lib example_app.rb`. Then visit http://smackaho.st:9393 and
+# http://foo.smackaho.st:9393 to use the application.
+
+subdomain :foo do |sub|
+ get '/' do
+ "You are on: #{sub}"
+ end
+
+ get '/' do
+ "Home!"
+ end
+end
+
@@ -0,0 +1,13 @@
+require 'sinatra'
+
+module Sinatra
+ module Subdomain
+ def subdomain(host, &block)
+ subdomain = host.to_s
+ condition { subdomain == request.env['HTTP_HOST'].split('.').first }

This comment has been minimized.

Show comment Hide comment
@rkh

rkh Apr 7, 2010

That way, the subdomain for github.com would be "github".

@rkh

rkh Apr 7, 2010

That way, the subdomain for github.com would be "github".

+ block.call(subdomain)
+ end
+ end
+
+ register Subdomain
+end
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/../lib/sinatra/subdomain'
+require 'test/unit'
+require 'capybara'
+require 'capybara/dsl'
+
+class SubDomainTest < Test::Unit::TestCase
+ include Capybara
+ Capybara.default_driver = :selenium
+
+ Capybara.app = Sinatra.new(Sinatra::Base) do
+ subdomain :foo do
+ get('/') { "foo" }
+ end
+ get('/') { "home" }
+ end
+
+ def test_subdomain
+ visit 'http://foo.smackaho.st:9887/'
+ p page.body
+ assert page.has_content? 'foo'
+ end
+
+ def test_home
+ visit 'http://smackaho.st:9887/'
+ p page.body
+ assert page.has_content? 'home'
+ end
+end

0 comments on commit 65156a2

Please sign in to comment.