sinatra-subdomain 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/Gemfile.lock +34 -30
- data/README.rdoc +8 -0
- data/Rakefile +6 -8
- data/lib/sinatra/subdomain.rb +33 -9
- data/lib/sinatra/subdomain/version.rb +1 -1
- data/sinatra-subdomain.gemspec +3 -1
- data/spec/multiple_tlds_spec.rb +6 -0
- data/spec/single_tld_spec.rb +6 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/subdomain_shared.rb +73 -0
- data/test/multiple_tlds_test.rb +31 -10
- data/test/subdomain_test.rb +30 -10
- data/test/test_helper.rb +1 -34
- metadata +45 -14
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/Gemfile.lock
CHANGED
@@ -1,49 +1,53 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sinatra-subdomain (0.1.
|
4
|
+
sinatra-subdomain (0.1.2)
|
5
5
|
sinatra
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
nokogiri (1.5.0)
|
24
|
-
rack (1.3.2)
|
10
|
+
awesome_print (1.0.2)
|
11
|
+
coderay (0.9.8)
|
12
|
+
diff-lcs (1.1.3)
|
13
|
+
method_source (0.6.7)
|
14
|
+
ruby_parser (>= 2.3.1)
|
15
|
+
pry (0.9.7.4)
|
16
|
+
coderay (~> 0.9.8)
|
17
|
+
method_source (~> 0.6.7)
|
18
|
+
ruby_parser (>= 2.3.1)
|
19
|
+
slop (~> 2.1.0)
|
20
|
+
rack (1.4.0)
|
21
|
+
rack-protection (1.2.0)
|
22
|
+
rack
|
25
23
|
rack-test (0.6.1)
|
26
24
|
rack (>= 1.0)
|
27
|
-
rake (0.9.2)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
25
|
+
rake (0.9.2.2)
|
26
|
+
rspec (2.8.0)
|
27
|
+
rspec-core (~> 2.8.0)
|
28
|
+
rspec-expectations (~> 2.8.0)
|
29
|
+
rspec-mocks (~> 2.8.0)
|
30
|
+
rspec-core (2.8.0)
|
31
|
+
rspec-expectations (2.8.0)
|
32
|
+
diff-lcs (~> 1.1.2)
|
33
|
+
rspec-mocks (2.8.0)
|
34
|
+
ruby_parser (2.3.1)
|
35
|
+
sexp_processor (~> 3.0)
|
36
|
+
sexp_processor (3.0.10)
|
37
|
+
sinatra (1.3.2)
|
38
|
+
rack (~> 1.3, >= 1.3.6)
|
39
|
+
rack-protection (~> 1.2)
|
40
|
+
tilt (~> 1.3, >= 1.3.3)
|
41
|
+
slop (2.1.0)
|
38
42
|
tilt (1.3.3)
|
39
|
-
xpath (0.1.4)
|
40
|
-
nokogiri (~> 1.3)
|
41
43
|
|
42
44
|
PLATFORMS
|
43
45
|
ruby
|
44
46
|
|
45
47
|
DEPENDENCIES
|
46
|
-
|
48
|
+
awesome_print
|
49
|
+
pry
|
47
50
|
rack-test
|
48
51
|
rake
|
52
|
+
rspec
|
49
53
|
sinatra-subdomain!
|
data/README.rdoc
CHANGED
@@ -23,6 +23,14 @@
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
If you're not building a classic app, make sure to register Sinatra::Subdomain yourself:
|
27
|
+
|
28
|
+
class Application < Sinatra::Base
|
29
|
+
register Sinatra::Subdomain
|
30
|
+
|
31
|
+
...
|
32
|
+
end
|
33
|
+
|
26
34
|
By default, sinatra-subdomain will consider 1 TLD as in <tt>example.com</tt>.
|
27
35
|
You can specify your TLD size for domains like <tt>example.com.br</tt> or <tt>example.co.uk</tt>.
|
28
36
|
|
data/Rakefile
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
require "bundler"
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
end
|
4
|
+
require "rake/testtask"
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << "test" << "lib"
|
7
|
+
t.test_files = FileList["test/*_test.rb"]
|
8
|
+
t.verbose = true
|
9
|
+
end
|
data/lib/sinatra/subdomain.rb
CHANGED
@@ -3,6 +3,10 @@ require "uri"
|
|
3
3
|
|
4
4
|
module Sinatra
|
5
5
|
module Subdomain
|
6
|
+
class << self
|
7
|
+
attr_accessor :app, :subdomain
|
8
|
+
end
|
9
|
+
|
6
10
|
module Helpers
|
7
11
|
def subdomain
|
8
12
|
uri = URI.parse("http://#{request.env["HTTP_HOST"]}")
|
@@ -12,18 +16,38 @@ module Sinatra
|
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
15
|
-
def subdomain(expected_subdomain =
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
elsif subdomain
|
20
|
-
true
|
21
|
-
else
|
22
|
-
false
|
23
|
-
end
|
19
|
+
def subdomain(expected_subdomain = true, &block)
|
20
|
+
::Sinatra::Subdomain.tap do |mod|
|
21
|
+
mod.app = self
|
22
|
+
mod.subdomain = expected_subdomain
|
24
23
|
end
|
25
24
|
|
26
25
|
yield
|
26
|
+
|
27
|
+
::Sinatra::Subdomain.tap do |mod|
|
28
|
+
mod.app = nil
|
29
|
+
mod.subdomain = nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.route_added(verb, path, block)
|
34
|
+
return unless subdomain && app
|
35
|
+
|
36
|
+
routes = app.instance_variable_get("@routes")
|
37
|
+
last_route = routes[verb].last
|
38
|
+
expected = subdomain
|
39
|
+
|
40
|
+
condition = app.instance_eval do
|
41
|
+
generate_method :subdomain do
|
42
|
+
if expected == true
|
43
|
+
subdomain != nil
|
44
|
+
else
|
45
|
+
subdomain.to_s == expected.to_s
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
last_route[2] << condition
|
27
51
|
end
|
28
52
|
|
29
53
|
def self.registered(app)
|
data/sinatra-subdomain.gemspec
CHANGED
@@ -18,7 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
|
20
20
|
s.add_dependency "sinatra"
|
21
|
+
s.add_development_dependency "rspec"
|
21
22
|
s.add_development_dependency "rake"
|
22
23
|
s.add_development_dependency "rack-test"
|
23
|
-
s.add_development_dependency "
|
24
|
+
s.add_development_dependency "pry"
|
25
|
+
s.add_development_dependency "awesome_print"
|
24
26
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
shared_examples_for "subdomain" do
|
2
|
+
def app
|
3
|
+
@app ||= begin
|
4
|
+
_tld = tld
|
5
|
+
|
6
|
+
@app = Class.new(Sinatra::Base) do
|
7
|
+
register Sinatra::Subdomain
|
8
|
+
set :tld_size, _tld.split(".").size - 1
|
9
|
+
|
10
|
+
subdomain :foo do
|
11
|
+
get("/") { "set: #{subdomain}" }
|
12
|
+
get("/about") { "set: about #{subdomain}" }
|
13
|
+
end
|
14
|
+
|
15
|
+
subdomain do
|
16
|
+
get("/") { "any: #{subdomain}" }
|
17
|
+
get("/about") { "any: about #{subdomain}" }
|
18
|
+
end
|
19
|
+
|
20
|
+
get("/") { "root" }
|
21
|
+
get("/about") { "about" }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when specific subdomain is required" do
|
27
|
+
it "renders root page" do
|
28
|
+
header "HOST", "foo.example#{tld}"
|
29
|
+
get "/"
|
30
|
+
|
31
|
+
last_response.body.should eql("set: foo")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "renders about page" do
|
35
|
+
header "HOST", "foo.example#{tld}"
|
36
|
+
get "/about"
|
37
|
+
|
38
|
+
last_response.body.should eql("set: about foo")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when any subdomain is required" do
|
43
|
+
it "renders root page" do
|
44
|
+
header "HOST", "mail.example#{tld}"
|
45
|
+
get "/"
|
46
|
+
|
47
|
+
last_response.body.should eql("any: mail")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "renders about page" do
|
51
|
+
header "HOST", "mail.example#{tld}"
|
52
|
+
get "/about"
|
53
|
+
|
54
|
+
last_response.body.should eql("any: about mail")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when no subdomain is required" do
|
59
|
+
it "renders root page" do
|
60
|
+
header "HOST", "example#{tld}"
|
61
|
+
get "/"
|
62
|
+
|
63
|
+
last_response.body.should eql("root")
|
64
|
+
end
|
65
|
+
|
66
|
+
it "renders about page" do
|
67
|
+
header "HOST", "example#{tld}"
|
68
|
+
get "/about"
|
69
|
+
|
70
|
+
last_response.body.should eql("about")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/test/multiple_tlds_test.rb
CHANGED
@@ -1,25 +1,46 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
3
|
class MultipleTldsTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
App = Class.new(Sinatra::Base) do
|
5
|
+
register Sinatra::Subdomain
|
6
|
+
set :tld_size, 2
|
7
|
+
|
8
|
+
subdomain :foo do
|
9
|
+
get("/") { "set: #{subdomain}" }
|
10
|
+
end
|
11
|
+
|
12
|
+
subdomain do
|
13
|
+
get("/") { "any: #{subdomain}" }
|
14
|
+
end
|
15
|
+
|
16
|
+
get("/") { "root" }
|
17
|
+
end
|
18
|
+
|
19
|
+
def app
|
20
|
+
App
|
6
21
|
end
|
7
22
|
|
8
23
|
def test_specified_subdomain
|
9
|
-
|
10
|
-
|
24
|
+
header "HOST", "foo.example.com.br"
|
25
|
+
get "/"
|
26
|
+
|
27
|
+
assert_equal "set: foo", last_response.body
|
11
28
|
end
|
12
29
|
|
13
30
|
def test_any_subdomain
|
14
|
-
|
15
|
-
|
31
|
+
header "HOST", "status.example.com.br"
|
32
|
+
get "/"
|
33
|
+
assert_equal "any: status", last_response.body
|
16
34
|
|
17
|
-
|
18
|
-
|
35
|
+
header "HOST", "mail.example.com.br"
|
36
|
+
get "/"
|
37
|
+
assert_equal "any: mail", last_response.body
|
19
38
|
end
|
20
39
|
|
21
40
|
def test_root
|
22
|
-
|
23
|
-
|
41
|
+
header "HOST", "example.com.br"
|
42
|
+
get "/"
|
43
|
+
|
44
|
+
assert_equal "root", last_response.body
|
24
45
|
end
|
25
46
|
end
|
data/test/subdomain_test.rb
CHANGED
@@ -1,25 +1,45 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
3
|
class SubdomainTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
App = Class.new(Sinatra::Base) do
|
5
|
+
register Sinatra::Subdomain
|
6
|
+
|
7
|
+
subdomain :foo do
|
8
|
+
get("/") { "set: #{subdomain}" }
|
9
|
+
end
|
10
|
+
|
11
|
+
subdomain do
|
12
|
+
get("/") { "any: #{subdomain}" }
|
13
|
+
end
|
14
|
+
|
15
|
+
get("/") { "root" }
|
16
|
+
end
|
17
|
+
|
18
|
+
def app
|
19
|
+
App
|
6
20
|
end
|
7
21
|
|
8
22
|
def test_specified_subdomain
|
9
|
-
|
10
|
-
|
23
|
+
header "HOST", "foo.example.org"
|
24
|
+
get "/"
|
25
|
+
|
26
|
+
assert_equal "set: foo", last_response.body
|
11
27
|
end
|
12
28
|
|
13
29
|
def test_any_subdomain
|
14
|
-
|
15
|
-
|
30
|
+
header "HOST", "status.example.org"
|
31
|
+
get "/"
|
32
|
+
assert_equal "any: status", last_response.body
|
16
33
|
|
17
|
-
|
18
|
-
|
34
|
+
header "HOST", "mail.example.org"
|
35
|
+
get "/"
|
36
|
+
assert_equal "any: mail", last_response.body
|
19
37
|
end
|
20
38
|
|
21
39
|
def test_root
|
22
|
-
|
23
|
-
|
40
|
+
header "HOST", "example.org"
|
41
|
+
get "/"
|
42
|
+
|
43
|
+
assert_equal "root", last_response.body
|
24
44
|
end
|
25
45
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,41 +1,8 @@
|
|
1
1
|
require "test/unit"
|
2
2
|
require "rack/test"
|
3
|
-
require "capybara"
|
4
|
-
require "capybara/dsl"
|
5
3
|
|
6
4
|
require "sinatra/subdomain"
|
7
5
|
|
8
|
-
Capybara.default_driver = :selenium
|
9
|
-
|
10
6
|
class Test::Unit::TestCase
|
11
|
-
include
|
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" }
|
7
|
+
include Rack::Test::Methods
|
41
8
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-subdomain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
16
|
-
requirement: &
|
16
|
+
requirement: &70095704587020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70095704587020
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70095704586600 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70095704586600
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rake
|
27
|
-
requirement: &
|
38
|
+
requirement: &70095704586180 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,10 +43,21 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70095704586180
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rack-test
|
38
|
-
requirement: &
|
49
|
+
requirement: &70095704585720 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70095704585720
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: pry
|
60
|
+
requirement: &70095704585240 !ruby/object:Gem::Requirement
|
39
61
|
none: false
|
40
62
|
requirements:
|
41
63
|
- - ! '>='
|
@@ -43,10 +65,10 @@ dependencies:
|
|
43
65
|
version: '0'
|
44
66
|
type: :development
|
45
67
|
prerelease: false
|
46
|
-
version_requirements: *
|
68
|
+
version_requirements: *70095704585240
|
47
69
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement: &
|
70
|
+
name: awesome_print
|
71
|
+
requirement: &70095704584780 !ruby/object:Gem::Requirement
|
50
72
|
none: false
|
51
73
|
requirements:
|
52
74
|
- - ! '>='
|
@@ -54,7 +76,7 @@ dependencies:
|
|
54
76
|
version: '0'
|
55
77
|
type: :development
|
56
78
|
prerelease: false
|
57
|
-
version_requirements: *
|
79
|
+
version_requirements: *70095704584780
|
58
80
|
description: Separate routes for subdomains on Sinatra
|
59
81
|
email:
|
60
82
|
- fnando.vieira@gmail.com
|
@@ -63,6 +85,7 @@ extensions: []
|
|
63
85
|
extra_rdoc_files: []
|
64
86
|
files:
|
65
87
|
- .gitignore
|
88
|
+
- .rspec
|
66
89
|
- Gemfile
|
67
90
|
- Gemfile.lock
|
68
91
|
- README.rdoc
|
@@ -70,6 +93,10 @@ files:
|
|
70
93
|
- lib/sinatra/subdomain.rb
|
71
94
|
- lib/sinatra/subdomain/version.rb
|
72
95
|
- sinatra-subdomain.gemspec
|
96
|
+
- spec/multiple_tlds_spec.rb
|
97
|
+
- spec/single_tld_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/support/subdomain_shared.rb
|
73
100
|
- test/multiple_tlds_test.rb
|
74
101
|
- test/subdomain_test.rb
|
75
102
|
- test/test_helper.rb
|
@@ -87,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
114
|
version: '0'
|
88
115
|
segments:
|
89
116
|
- 0
|
90
|
-
hash:
|
117
|
+
hash: 632946571034999382
|
91
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
119
|
none: false
|
93
120
|
requirements:
|
@@ -96,14 +123,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
123
|
version: '0'
|
97
124
|
segments:
|
98
125
|
- 0
|
99
|
-
hash:
|
126
|
+
hash: 632946571034999382
|
100
127
|
requirements: []
|
101
128
|
rubyforge_project:
|
102
|
-
rubygems_version: 1.8.
|
129
|
+
rubygems_version: 1.8.11
|
103
130
|
signing_key:
|
104
131
|
specification_version: 3
|
105
132
|
summary: Separate routes for subdomains on Sinatra
|
106
133
|
test_files:
|
134
|
+
- spec/multiple_tlds_spec.rb
|
135
|
+
- spec/single_tld_spec.rb
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
- spec/support/subdomain_shared.rb
|
107
138
|
- test/multiple_tlds_test.rb
|
108
139
|
- test/subdomain_test.rb
|
109
140
|
- test/test_helper.rb
|