Created
January 25, 2012 03:43
-
-
Save khy/1674581 to your computer and use it in GitHub Desktop.
Hacker Please: Initial Work on useless.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Useless | |
class Base | |
def call(env) | |
[200, {'Content-Type' => 'text/plain'}, ['useless.io: The Useless Platform']] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.dirname(__FILE__) + '/lib/useless' | |
run Useless |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'rack' | |
group :test do | |
gem 'rspec' | |
end | |
group :development do | |
gem 'heroku' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.dirname(__FILE__) + '/../lib/useless' | |
require 'rack/mock' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Useless | |
class StreetArt | |
def call(env) | |
[200, {'Content-Type' => 'text/plain'}, ['useless.io: The Useless Platform / Street Art API']] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Useless | |
# `SubdomainMap` is Rack middleware that delegates to other Rack apps based | |
# upon subdomain. It takes two arguments upon initialization: | |
# | |
# * `base`, the app to be called if there is no subdomain. | |
# * `map`, a hash that maps subdomain strings to apps. | |
class SubdomainMap | |
def initialize(base, map = {}) | |
@base = base | |
@map = map | |
end | |
def call(env) | |
# If the `SERVER_NAME` environment variable has a subdomain | |
if env['SERVER_NAME'] =~ /(.*?)\.(?:.*)\..*/ | |
subdomain = $1 | |
end | |
if subdomain | |
# and we can find a corresponding app, | |
if app = @map[subdomain] | |
# call it and return the result. | |
app.call(env) | |
# Otherwise, return 403 Forbidden. | |
else | |
[403, {"Content-Type" => "text/plain"}, []] | |
end | |
# If there is no subdomain, | |
else | |
# call the base app and return the result. | |
@base.call(env) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.dirname(__FILE__) + '/../../spec_helper' | |
describe Useless::SubdomainMap do | |
def map | |
default_app = lambda do |env| | |
[200, {'Content-Type' => 'text/plain'}, ['Default App']] | |
end | |
api_app = lambda do |env| | |
[200, {'Content-Type' => 'text/plain'}, ['API App']] | |
end | |
Useless::SubdomainMap.new default_app, 'subdomain' => api_app | |
end | |
it 'should call the default app if no subdomain is specified' do | |
res = Rack::MockRequest.new(map).get('http://useless.info') | |
res.should be_ok | |
res.body.should == 'Default App' | |
end | |
it 'should call the appropriate API app if a subdomain is specified' do | |
res = Rack::MockRequest.new(map).get('http://subdomain.useless.info') | |
res.should be_ok | |
res.body.should == 'API App' | |
end | |
it 'should return a 403 Forbidden if the specified subdomain is not mapped' do | |
res = Rack::MockRequest.new(map).get('http://nonexistant.useless.info') | |
res.should be_forbidden | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.unshift File.dirname(__FILE__) | |
module Useless | |
def self.call(env) | |
[200, {'Content-Type' => 'text/plain'}, ['useless.io: The Useless Platform']] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.unshift File.dirname(__FILE__) | |
module Useless | |
autoload :Base, 'useless/base' | |
autoload :StreetArt, 'useless/street_art' | |
autoload :SubdomainMap, 'useless/middleware/subdomain_map' | |
def self.call(env) | |
map = SubdomainMap.new Base.new, | |
'street-art' => StreetArt.new | |
map.call(env) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment