sinatra-subdomain 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +48 -0
 - data/lib/sinatra/subdomain/version.rb +10 -0
 - data/lib/sinatra/subdomain.rb +36 -0
 - data/test/multiple_tlds_test.rb +25 -0
 - data/test/subdomain_test.rb +25 -0
 - data/test/test_helper.rb +41 -0
 - metadata +80 -0
 
    
        data/README.rdoc
    ADDED
    
    | 
         @@ -0,0 +1,48 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            = Sinatra Subdomain
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            == Installation
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
              sudo gem install sinatra-subdomain
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            == Usage
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
              require "sinatra"
         
     | 
| 
      
 10 
     | 
    
         
            +
              require "sinatra/subdomain"
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
              # Specify which subdomain you want
         
     | 
| 
      
 13 
     | 
    
         
            +
              subdomain :foo do
         
     | 
| 
      
 14 
     | 
    
         
            +
                get '/' do
         
     | 
| 
      
 15 
     | 
    
         
            +
                  "render page for FOO"
         
     | 
| 
      
 16 
     | 
    
         
            +
                end
         
     | 
| 
      
 17 
     | 
    
         
            +
              end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
              # If any subdomain is set
         
     | 
| 
      
 20 
     | 
    
         
            +
              subdomain do
         
     | 
| 
      
 21 
     | 
    
         
            +
                get '/' do
         
     | 
| 
      
 22 
     | 
    
         
            +
                  "render page for #{subdomain} subdomain"
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            By default, sinatra-subdomain will consider 1 TLD as in <tt>example.com</tt>.
         
     | 
| 
      
 27 
     | 
    
         
            +
            You can specify your TLD size for domains like <tt>example.com.br</tt> or <tt>example.co.uk</tt>.
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
              require "sinatra"
         
     | 
| 
      
 30 
     | 
    
         
            +
              require "sinatra/subdomain"
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              set :tld_size, 2
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
            This extension was based on http://github.com/akahn/sinatra-subdomain/
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
            = License
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
            (The MIT License)
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            Copyright © 2010:
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
            * Nando Vieira (http://simplesideias.com.br)
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
| 
         @@ -0,0 +1,36 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "sinatra/base"
         
     | 
| 
      
 2 
     | 
    
         
            +
            require "uri"
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            module Sinatra
         
     | 
| 
      
 5 
     | 
    
         
            +
              module Subdomain
         
     | 
| 
      
 6 
     | 
    
         
            +
                module Helpers
         
     | 
| 
      
 7 
     | 
    
         
            +
                  def subdomain
         
     | 
| 
      
 8 
     | 
    
         
            +
                    uri = URI.parse("http://#{request.env["HTTP_HOST"]}")
         
     | 
| 
      
 9 
     | 
    
         
            +
                    parts = uri.host.split(".")
         
     | 
| 
      
 10 
     | 
    
         
            +
                    parts.pop(options.tld_size + 1)
         
     | 
| 
      
 11 
     | 
    
         
            +
                    parts.first
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                def subdomain(expected_subdomain = nil, &block)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  condition do
         
     | 
| 
      
 17 
     | 
    
         
            +
                    if expected_subdomain
         
     | 
| 
      
 18 
     | 
    
         
            +
                      expected_subdomain.to_s == subdomain
         
     | 
| 
      
 19 
     | 
    
         
            +
                    elsif subdomain
         
     | 
| 
      
 20 
     | 
    
         
            +
                      true
         
     | 
| 
      
 21 
     | 
    
         
            +
                    else
         
     | 
| 
      
 22 
     | 
    
         
            +
                      false
         
     | 
| 
      
 23 
     | 
    
         
            +
                    end
         
     | 
| 
      
 24 
     | 
    
         
            +
                  end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                  yield
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                def self.registered(app)
         
     | 
| 
      
 30 
     | 
    
         
            +
                  app.helpers Sinatra::Subdomain::Helpers
         
     | 
| 
      
 31 
     | 
    
         
            +
                  app.set :tld_size, 1
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
              end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
              register Subdomain
         
     | 
| 
      
 36 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,25 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "test_helper"
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            class MultipleTldsTest < Test::Unit::TestCase
         
     | 
| 
      
 4 
     | 
    
         
            +
              def setup
         
     | 
| 
      
 5 
     | 
    
         
            +
                Capybara.app = MultipleTldsApp
         
     | 
| 
      
 6 
     | 
    
         
            +
              end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
              def test_specified_subdomain
         
     | 
| 
      
 9 
     | 
    
         
            +
                visit "http://foo.smackaho.smackaho.st:9887/"
         
     | 
| 
      
 10 
     | 
    
         
            +
                assert page.has_content? "set: foo"
         
     | 
| 
      
 11 
     | 
    
         
            +
              end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              def test_any_subdomain
         
     | 
| 
      
 14 
     | 
    
         
            +
                visit "http://status.smackaho.smackaho.st:9887/"
         
     | 
| 
      
 15 
     | 
    
         
            +
                assert page.has_content? "any: status"
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                visit "http://mail.smackaho.smackaho.st:9887/"
         
     | 
| 
      
 18 
     | 
    
         
            +
                assert page.has_content? "any: mail"
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              def test_root
         
     | 
| 
      
 22 
     | 
    
         
            +
                visit "http://smackaho.smackaho.st:9887/"
         
     | 
| 
      
 23 
     | 
    
         
            +
                assert page.has_content? "root"
         
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,25 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "test_helper"
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            class SubdomainTest < Test::Unit::TestCase
         
     | 
| 
      
 4 
     | 
    
         
            +
              def setup
         
     | 
| 
      
 5 
     | 
    
         
            +
                Capybara.app = SampleApp
         
     | 
| 
      
 6 
     | 
    
         
            +
              end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
              def test_specified_subdomain
         
     | 
| 
      
 9 
     | 
    
         
            +
                visit "http://foo.smackaho.st:9887/"
         
     | 
| 
      
 10 
     | 
    
         
            +
                assert page.has_content? "set: foo"
         
     | 
| 
      
 11 
     | 
    
         
            +
              end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              def test_any_subdomain
         
     | 
| 
      
 14 
     | 
    
         
            +
                visit "http://status.smackaho.st:9887/"
         
     | 
| 
      
 15 
     | 
    
         
            +
                assert page.has_content? "any: status"
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                visit "http://mail.smackaho.st:9887/"
         
     | 
| 
      
 18 
     | 
    
         
            +
                assert page.has_content? "any: mail"
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              def test_root
         
     | 
| 
      
 22 
     | 
    
         
            +
                visit "http://smackaho.st:9887/"
         
     | 
| 
      
 23 
     | 
    
         
            +
                assert page.has_content? "root"
         
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
            end
         
     | 
    
        data/test/test_helper.rb
    ADDED
    
    | 
         @@ -0,0 +1,41 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "test/unit"
         
     | 
| 
      
 2 
     | 
    
         
            +
            require "rack/test"
         
     | 
| 
      
 3 
     | 
    
         
            +
            require "capybara"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "capybara/dsl"
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            require "sinatra/subdomain"
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            Capybara.default_driver = :selenium
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            class Test::Unit::TestCase
         
     | 
| 
      
 11 
     | 
    
         
            +
              include Capybara
         
     | 
| 
      
 12 
     | 
    
         
            +
            end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            class SampleApp < Sinatra::Base
         
     | 
| 
      
 15 
     | 
    
         
            +
              register Sinatra::Subdomain
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
              subdomain :foo do
         
     | 
| 
      
 18 
     | 
    
         
            +
                get("/") { "set: #{subdomain}" }
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              subdomain do
         
     | 
| 
      
 22 
     | 
    
         
            +
                get("/") { "any: #{subdomain}" }
         
     | 
| 
      
 23 
     | 
    
         
            +
              end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
              get("/") { "root" }
         
     | 
| 
      
 26 
     | 
    
         
            +
            end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
            class MultipleTldsApp < Sinatra::Base
         
     | 
| 
      
 29 
     | 
    
         
            +
              register Sinatra::Subdomain
         
     | 
| 
      
 30 
     | 
    
         
            +
              set :tld_size, 2
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              subdomain :foo do
         
     | 
| 
      
 33 
     | 
    
         
            +
                get("/") { "set: #{subdomain}" }
         
     | 
| 
      
 34 
     | 
    
         
            +
              end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
              subdomain do
         
     | 
| 
      
 37 
     | 
    
         
            +
                get("/") { "any: #{subdomain}" }
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
              get("/") { "root" }
         
     | 
| 
      
 41 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,80 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: sinatra-subdomain
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 5 
     | 
    
         
            +
              segments: 
         
     | 
| 
      
 6 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 7 
     | 
    
         
            +
              - 1
         
     | 
| 
      
 8 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 9 
     | 
    
         
            +
              version: 0.1.0
         
     | 
| 
      
 10 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 11 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 12 
     | 
    
         
            +
            - Nando Vieira
         
     | 
| 
      
 13 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 14 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 15 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            date: 2010-05-09 00:00:00 -03:00
         
     | 
| 
      
 18 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 19 
     | 
    
         
            +
            dependencies: 
         
     | 
| 
      
 20 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 21 
     | 
    
         
            +
              name: sinatra
         
     | 
| 
      
 22 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 23 
     | 
    
         
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 24 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 25 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 26 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 27 
     | 
    
         
            +
                    segments: 
         
     | 
| 
      
 28 
     | 
    
         
            +
                    - 0
         
     | 
| 
      
 29 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 30 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 31 
     | 
    
         
            +
              version_requirements: *id001
         
     | 
| 
      
 32 
     | 
    
         
            +
            description: Separate routes for subdomains on Sinatra
         
     | 
| 
      
 33 
     | 
    
         
            +
            email: fnando.vieira@gmail.com
         
     | 
| 
      
 34 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
            extra_rdoc_files: 
         
     | 
| 
      
 39 
     | 
    
         
            +
            - README.rdoc
         
     | 
| 
      
 40 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 41 
     | 
    
         
            +
            - README.rdoc
         
     | 
| 
      
 42 
     | 
    
         
            +
            - lib/sinatra/subdomain.rb
         
     | 
| 
      
 43 
     | 
    
         
            +
            - lib/sinatra/subdomain/version.rb
         
     | 
| 
      
 44 
     | 
    
         
            +
            - test/multiple_tlds_test.rb
         
     | 
| 
      
 45 
     | 
    
         
            +
            - test/subdomain_test.rb
         
     | 
| 
      
 46 
     | 
    
         
            +
            - test/test_helper.rb
         
     | 
| 
      
 47 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 48 
     | 
    
         
            +
            homepage: http://github.com/fnando/sinatra-subdomain
         
     | 
| 
      
 49 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 52 
     | 
    
         
            +
            rdoc_options: 
         
     | 
| 
      
 53 
     | 
    
         
            +
            - --charset=UTF-8
         
     | 
| 
      
 54 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 55 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 56 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 57 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 58 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 59 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 60 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 61 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 62 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 63 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 64 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 65 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 66 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 67 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 68 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 69 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 70 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 73 
     | 
    
         
            +
            rubygems_version: 1.3.6
         
     | 
| 
      
 74 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 75 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 76 
     | 
    
         
            +
            summary: Separate routes for subdomains on Sinatra
         
     | 
| 
      
 77 
     | 
    
         
            +
            test_files: 
         
     | 
| 
      
 78 
     | 
    
         
            +
            - test/multiple_tlds_test.rb
         
     | 
| 
      
 79 
     | 
    
         
            +
            - test/subdomain_test.rb
         
     | 
| 
      
 80 
     | 
    
         
            +
            - test/test_helper.rb
         
     |