houser 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/Appraisals +7 -0
- data/README.md +20 -1
- data/Rakefile +6 -0
- data/gemfiles/rails3.gemfile +7 -0
- data/gemfiles/rails3.gemfile.lock +120 -0
- data/gemfiles/rails4.gemfile +7 -0
- data/gemfiles/rails4.gemfile.lock +115 -0
- data/houser.gemspec +1 -0
- data/lib/houser/middleware.rb +4 -2
- data/lib/houser/version.rb +1 -1
- data/spec/houser_spec.rb +48 -6
- metadata +21 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d50dc68ec3f594a6648bee55ba34edb32125b769
|
4
|
+
data.tar.gz: 6f892bba2adb73499b267b31b33b9c5e166d7fac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aa002ff57efc25dab9b52d5f9ec16ef678477a51c24a6092589529462a7a795c3244e991ececcd07feb895c32c77f6710789a80696cc10b39537471e5b71e24
|
7
|
+
data.tar.gz: 2ef326f2e61d8faab3f3a8927890a86fb36377347303698299a6d2a4bf1587cda249a59c168402df290eaa482946538492725e2d0718c75ec3edd889fa817658
|
data/.travis.yml
CHANGED
data/Appraisals
ADDED
data/README.md
CHANGED
@@ -28,7 +28,26 @@ If you're using a TLD like `.co.uk` instead of `.com`, you will need to specify
|
|
28
28
|
config.middleware.use Houser::Middleware,
|
29
29
|
:class_name => 'Model',
|
30
30
|
:tld_length => 2
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
There are two rack environment variables set by the Houser middleware that you can use throughout your application to scope resources.
|
35
|
+
|
36
|
+
If no object is found for the subdomain the request is received on, both of these variables will be nil.
|
37
|
+
|
38
|
+
### `env['X-Houser-Subdomain']`
|
39
|
+
|
40
|
+
The complete subdomain of the request. The following domains have the following subdomains:
|
41
|
+
|
42
|
+
* `sub1.example.com => sub1`
|
43
|
+
* `sub2.sub1.example.com => sub2.sub1`
|
44
|
+
* `sub1.example.co.uk => sub1`
|
45
|
+
* `sub2.sub1.example.co.uk => sub2.sub1`
|
46
|
+
|
47
|
+
### `env['X-Houser-Object']`
|
48
|
+
|
49
|
+
The instance of the Class that is found based on the subdomain.
|
31
50
|
|
32
51
|
## Contributing
|
33
52
|
|
34
|
-
Please see [CONTRIBUTING.md](https://github.com/radar/houser/CONTRIBUTING.md)
|
53
|
+
Please see [CONTRIBUTING.md](https://github.com/radar/houser/CONTRIBUTING.md)
|
data/Rakefile
CHANGED
@@ -0,0 +1,120 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
houser (1.0.2)
|
5
|
+
rack
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (3.2.17)
|
11
|
+
actionpack (= 3.2.17)
|
12
|
+
mail (~> 2.5.4)
|
13
|
+
actionpack (3.2.17)
|
14
|
+
activemodel (= 3.2.17)
|
15
|
+
activesupport (= 3.2.17)
|
16
|
+
builder (~> 3.0.0)
|
17
|
+
erubis (~> 2.7.0)
|
18
|
+
journey (~> 1.0.4)
|
19
|
+
rack (~> 1.4.5)
|
20
|
+
rack-cache (~> 1.2)
|
21
|
+
rack-test (~> 0.6.1)
|
22
|
+
sprockets (~> 2.2.1)
|
23
|
+
activemodel (3.2.17)
|
24
|
+
activesupport (= 3.2.17)
|
25
|
+
builder (~> 3.0.0)
|
26
|
+
activerecord (3.2.17)
|
27
|
+
activemodel (= 3.2.17)
|
28
|
+
activesupport (= 3.2.17)
|
29
|
+
arel (~> 3.0.2)
|
30
|
+
tzinfo (~> 0.3.29)
|
31
|
+
activeresource (3.2.17)
|
32
|
+
activemodel (= 3.2.17)
|
33
|
+
activesupport (= 3.2.17)
|
34
|
+
activesupport (3.2.17)
|
35
|
+
i18n (~> 0.6, >= 0.6.4)
|
36
|
+
multi_json (~> 1.0)
|
37
|
+
appraisal (0.5.2)
|
38
|
+
bundler
|
39
|
+
rake
|
40
|
+
arel (3.0.3)
|
41
|
+
builder (3.0.4)
|
42
|
+
coderay (1.1.0)
|
43
|
+
diff-lcs (1.2.5)
|
44
|
+
erubis (2.7.0)
|
45
|
+
hike (1.2.3)
|
46
|
+
i18n (0.6.9)
|
47
|
+
journey (1.0.4)
|
48
|
+
json (1.8.1)
|
49
|
+
mail (2.5.4)
|
50
|
+
mime-types (~> 1.16)
|
51
|
+
treetop (~> 1.4.8)
|
52
|
+
method_source (0.8.2)
|
53
|
+
mime-types (1.25.1)
|
54
|
+
multi_json (1.9.2)
|
55
|
+
polyglot (0.3.4)
|
56
|
+
pry (0.9.12.6)
|
57
|
+
coderay (~> 1.0)
|
58
|
+
method_source (~> 0.8)
|
59
|
+
slop (~> 3.4)
|
60
|
+
rack (1.4.5)
|
61
|
+
rack-cache (1.2)
|
62
|
+
rack (>= 0.4)
|
63
|
+
rack-ssl (1.3.4)
|
64
|
+
rack
|
65
|
+
rack-test (0.6.2)
|
66
|
+
rack (>= 1.0)
|
67
|
+
rails (3.2.17)
|
68
|
+
actionmailer (= 3.2.17)
|
69
|
+
actionpack (= 3.2.17)
|
70
|
+
activerecord (= 3.2.17)
|
71
|
+
activeresource (= 3.2.17)
|
72
|
+
activesupport (= 3.2.17)
|
73
|
+
bundler (~> 1.0)
|
74
|
+
railties (= 3.2.17)
|
75
|
+
railties (3.2.17)
|
76
|
+
actionpack (= 3.2.17)
|
77
|
+
activesupport (= 3.2.17)
|
78
|
+
rack-ssl (~> 1.3.2)
|
79
|
+
rake (>= 0.8.7)
|
80
|
+
rdoc (~> 3.4)
|
81
|
+
thor (>= 0.14.6, < 2.0)
|
82
|
+
rake (10.2.2)
|
83
|
+
rdoc (3.12.2)
|
84
|
+
json (~> 1.4)
|
85
|
+
rspec-core (2.99.0.beta1)
|
86
|
+
rspec-expectations (2.99.0.beta1)
|
87
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
88
|
+
rspec-mocks (2.99.0.beta1)
|
89
|
+
rspec-rails (2.99.0.beta1)
|
90
|
+
actionpack (>= 3.0)
|
91
|
+
activemodel (>= 3.0)
|
92
|
+
activesupport (>= 3.0)
|
93
|
+
railties (>= 3.0)
|
94
|
+
rspec-core (= 2.99.0.beta1)
|
95
|
+
rspec-expectations (= 2.99.0.beta1)
|
96
|
+
rspec-mocks (= 2.99.0.beta1)
|
97
|
+
slop (3.5.0)
|
98
|
+
sprockets (2.2.2)
|
99
|
+
hike (~> 1.2)
|
100
|
+
multi_json (~> 1.0)
|
101
|
+
rack (~> 1.0)
|
102
|
+
tilt (~> 1.1, != 1.3.0)
|
103
|
+
thor (0.19.1)
|
104
|
+
tilt (1.4.1)
|
105
|
+
treetop (1.4.15)
|
106
|
+
polyglot
|
107
|
+
polyglot (>= 0.3.1)
|
108
|
+
tzinfo (0.3.39)
|
109
|
+
|
110
|
+
PLATFORMS
|
111
|
+
ruby
|
112
|
+
|
113
|
+
DEPENDENCIES
|
114
|
+
appraisal (~> 0.5.2)
|
115
|
+
bundler (~> 1.5)
|
116
|
+
houser!
|
117
|
+
pry
|
118
|
+
rails (= 3.2.17)
|
119
|
+
rake
|
120
|
+
rspec-rails (= 2.99.0.beta1)
|
@@ -0,0 +1,115 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
houser (1.0.2)
|
5
|
+
rack
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (4.0.4)
|
11
|
+
actionpack (= 4.0.4)
|
12
|
+
mail (~> 2.5.4)
|
13
|
+
actionpack (4.0.4)
|
14
|
+
activesupport (= 4.0.4)
|
15
|
+
builder (~> 3.1.0)
|
16
|
+
erubis (~> 2.7.0)
|
17
|
+
rack (~> 1.5.2)
|
18
|
+
rack-test (~> 0.6.2)
|
19
|
+
activemodel (4.0.4)
|
20
|
+
activesupport (= 4.0.4)
|
21
|
+
builder (~> 3.1.0)
|
22
|
+
activerecord (4.0.4)
|
23
|
+
activemodel (= 4.0.4)
|
24
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
25
|
+
activesupport (= 4.0.4)
|
26
|
+
arel (~> 4.0.0)
|
27
|
+
activerecord-deprecated_finders (1.0.3)
|
28
|
+
activesupport (4.0.4)
|
29
|
+
i18n (~> 0.6, >= 0.6.9)
|
30
|
+
minitest (~> 4.2)
|
31
|
+
multi_json (~> 1.3)
|
32
|
+
thread_safe (~> 0.1)
|
33
|
+
tzinfo (~> 0.3.37)
|
34
|
+
appraisal (0.5.2)
|
35
|
+
bundler
|
36
|
+
rake
|
37
|
+
arel (4.0.2)
|
38
|
+
atomic (1.1.16)
|
39
|
+
builder (3.1.4)
|
40
|
+
coderay (1.1.0)
|
41
|
+
diff-lcs (1.2.5)
|
42
|
+
erubis (2.7.0)
|
43
|
+
hike (1.2.3)
|
44
|
+
i18n (0.6.9)
|
45
|
+
mail (2.5.4)
|
46
|
+
mime-types (~> 1.16)
|
47
|
+
treetop (~> 1.4.8)
|
48
|
+
method_source (0.8.2)
|
49
|
+
mime-types (1.25.1)
|
50
|
+
minitest (4.7.5)
|
51
|
+
multi_json (1.9.2)
|
52
|
+
polyglot (0.3.4)
|
53
|
+
pry (0.9.12.6)
|
54
|
+
coderay (~> 1.0)
|
55
|
+
method_source (~> 0.8)
|
56
|
+
slop (~> 3.4)
|
57
|
+
rack (1.5.2)
|
58
|
+
rack-test (0.6.2)
|
59
|
+
rack (>= 1.0)
|
60
|
+
rails (4.0.4)
|
61
|
+
actionmailer (= 4.0.4)
|
62
|
+
actionpack (= 4.0.4)
|
63
|
+
activerecord (= 4.0.4)
|
64
|
+
activesupport (= 4.0.4)
|
65
|
+
bundler (>= 1.3.0, < 2.0)
|
66
|
+
railties (= 4.0.4)
|
67
|
+
sprockets-rails (~> 2.0.0)
|
68
|
+
railties (4.0.4)
|
69
|
+
actionpack (= 4.0.4)
|
70
|
+
activesupport (= 4.0.4)
|
71
|
+
rake (>= 0.8.7)
|
72
|
+
thor (>= 0.18.1, < 2.0)
|
73
|
+
rake (10.2.2)
|
74
|
+
rspec-core (2.99.0.beta1)
|
75
|
+
rspec-expectations (2.99.0.beta1)
|
76
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
77
|
+
rspec-mocks (2.99.0.beta1)
|
78
|
+
rspec-rails (2.99.0.beta1)
|
79
|
+
actionpack (>= 3.0)
|
80
|
+
activemodel (>= 3.0)
|
81
|
+
activesupport (>= 3.0)
|
82
|
+
railties (>= 3.0)
|
83
|
+
rspec-core (= 2.99.0.beta1)
|
84
|
+
rspec-expectations (= 2.99.0.beta1)
|
85
|
+
rspec-mocks (= 2.99.0.beta1)
|
86
|
+
slop (3.5.0)
|
87
|
+
sprockets (2.12.0)
|
88
|
+
hike (~> 1.2)
|
89
|
+
multi_json (~> 1.0)
|
90
|
+
rack (~> 1.0)
|
91
|
+
tilt (~> 1.1, != 1.3.0)
|
92
|
+
sprockets-rails (2.0.1)
|
93
|
+
actionpack (>= 3.0)
|
94
|
+
activesupport (>= 3.0)
|
95
|
+
sprockets (~> 2.8)
|
96
|
+
thor (0.19.1)
|
97
|
+
thread_safe (0.3.1)
|
98
|
+
atomic (>= 1.1.7, < 2)
|
99
|
+
tilt (1.4.1)
|
100
|
+
treetop (1.4.15)
|
101
|
+
polyglot
|
102
|
+
polyglot (>= 0.3.1)
|
103
|
+
tzinfo (0.3.39)
|
104
|
+
|
105
|
+
PLATFORMS
|
106
|
+
ruby
|
107
|
+
|
108
|
+
DEPENDENCIES
|
109
|
+
appraisal (~> 0.5.2)
|
110
|
+
bundler (~> 1.5)
|
111
|
+
houser!
|
112
|
+
pry
|
113
|
+
rails (= 4.0.4)
|
114
|
+
rake
|
115
|
+
rspec-rails (= 2.99.0.beta1)
|
data/houser.gemspec
CHANGED
data/lib/houser/middleware.rb
CHANGED
@@ -6,6 +6,7 @@ module Houser
|
|
6
6
|
|
7
7
|
def initialize(app, options={})
|
8
8
|
@options = options
|
9
|
+
@options[:subdomain_column] ||= "subdomain"
|
9
10
|
@options[:class] = Object.const_get(options[:class_name])
|
10
11
|
@options[:tld_length] ||= 1
|
11
12
|
@app = app
|
@@ -15,7 +16,8 @@ module Houser
|
|
15
16
|
domain_parts = env['HTTP_HOST'].split('.')
|
16
17
|
|
17
18
|
if domain_parts.length > 1 + options[:tld_length]
|
18
|
-
|
19
|
+
domain_name = domain_parts[(-options[:tld_length] - 1)..-1]
|
20
|
+
subdomain = (domain_parts - domain_name).join('.')
|
19
21
|
find_tenant(env, subdomain)
|
20
22
|
end
|
21
23
|
|
@@ -25,7 +27,7 @@ module Houser
|
|
25
27
|
private
|
26
28
|
|
27
29
|
def find_tenant(env, subdomain)
|
28
|
-
object = options[:class].
|
30
|
+
object = options[:class].where(options[:subdomain_column].to_sym => subdomain).first
|
29
31
|
if object
|
30
32
|
env['X-Houser-Subdomain'] = subdomain
|
31
33
|
env['X-Houser-Object'] = object
|
data/lib/houser/version.rb
CHANGED
data/spec/houser_spec.rb
CHANGED
@@ -27,7 +27,7 @@ describe Houser::Middleware do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it "does nothing for non-subdomained requests" do
|
30
|
-
expect(Account).to_not receive(:
|
30
|
+
expect(Account).to_not receive(:where)
|
31
31
|
code, env = middleware.call(env_for("http://example.com"))
|
32
32
|
expect(env['X-Houser-Subdomain']).to be_nil
|
33
33
|
expect(env['X-Houser-Object']).to be_nil
|
@@ -35,19 +35,32 @@ describe Houser::Middleware do
|
|
35
35
|
|
36
36
|
it "sets X-HOUSER-ID header for known subdomains" do
|
37
37
|
account = double(id: 1)
|
38
|
-
expect(Account).to receive(:
|
38
|
+
expect(Account).to receive(:where).with(subdomain: subdomain).and_return([account])
|
39
39
|
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
40
40
|
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
41
41
|
expect(env['X-Houser-Object']).to eq(account)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "returns no headers for unknown subdomains" do
|
45
|
-
expect(Account).to receive(:
|
45
|
+
expect(Account).to receive(:where).with(subdomain: subdomain).and_return([])
|
46
46
|
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
47
47
|
expect(env['X-Houser-Subdomain']).to be_nil
|
48
48
|
expect(env['X-Houser-Object']).to be_nil
|
49
49
|
end
|
50
50
|
|
51
|
+
|
52
|
+
context "double subdomain" do
|
53
|
+
let(:subdomain) { 'ruby.melbourne' }
|
54
|
+
|
55
|
+
it "sets X-HOUSER-ID header for known subdomains within subdomains" do
|
56
|
+
account = double(id: 1)
|
57
|
+
expect(Account).to receive(:where).with(subdomain: subdomain).and_return([account])
|
58
|
+
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
59
|
+
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
60
|
+
expect(env['X-Houser-Object']).to eq(account)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
51
64
|
context "with a different class name" do
|
52
65
|
let(:options) do
|
53
66
|
{
|
@@ -62,8 +75,8 @@ describe Houser::Middleware do
|
|
62
75
|
end
|
63
76
|
|
64
77
|
it "calls the right class" do
|
65
|
-
expect(Account).to_not receive(:
|
66
|
-
expect(Store).to receive(:
|
78
|
+
expect(Account).to_not receive(:where)
|
79
|
+
expect(Store).to receive(:where).with(subdomain: subdomain).and_return([store])
|
67
80
|
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
68
81
|
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
69
82
|
expect(env['X-Houser-Object']).to eq(store)
|
@@ -79,10 +92,39 @@ describe Houser::Middleware do
|
|
79
92
|
end
|
80
93
|
|
81
94
|
it "supports more than one-level of TLD" do
|
82
|
-
expect(Account).to_not receive(:
|
95
|
+
expect(Account).to_not receive(:where)
|
83
96
|
code, env = middleware.call(env_for("http://example.co.uk"))
|
84
97
|
expect(env['X-Houser-Subdomain']).to be_nil
|
85
98
|
expect(env['X-Houser-ID']).to be_nil
|
86
99
|
end
|
100
|
+
|
101
|
+
context "double subdomain" do
|
102
|
+
let(:subdomain) { 'ruby.melbourne' }
|
103
|
+
|
104
|
+
it "sets X-HOUSER-ID header for known subdomains within subdomains" do
|
105
|
+
account = double(id: 1)
|
106
|
+
expect(Account).to receive(:where).with(subdomain: subdomain).and_return([account])
|
107
|
+
code, env = middleware.call(env_for("http://#{subdomain}.example.co.uk"))
|
108
|
+
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
109
|
+
expect(env['X-Houser-Object']).to eq(account)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "with a different column name" do
|
115
|
+
let(:options) do
|
116
|
+
{
|
117
|
+
subdomain_column: 'foo',
|
118
|
+
class_name: 'Account'
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
it "finds by an alternative column" do
|
123
|
+
account = double(id: 1)
|
124
|
+
expect(Account).to receive(:where).with(:foo => subdomain).and_return([account])
|
125
|
+
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
126
|
+
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
127
|
+
expect(env['X-Houser-Object']).to eq(account)
|
128
|
+
end
|
87
129
|
end
|
88
130
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: houser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Bigg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: appraisal
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.5.2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.5.2
|
83
97
|
description: Lightweight multitenancy gem.
|
84
98
|
email:
|
85
99
|
- radarlistener@gmail.com
|
@@ -89,11 +103,16 @@ extra_rdoc_files: []
|
|
89
103
|
files:
|
90
104
|
- ".gitignore"
|
91
105
|
- ".travis.yml"
|
106
|
+
- Appraisals
|
92
107
|
- CONTRIBUTING.md
|
93
108
|
- Gemfile
|
94
109
|
- LICENSE.txt
|
95
110
|
- README.md
|
96
111
|
- Rakefile
|
112
|
+
- gemfiles/rails3.gemfile
|
113
|
+
- gemfiles/rails3.gemfile.lock
|
114
|
+
- gemfiles/rails4.gemfile
|
115
|
+
- gemfiles/rails4.gemfile.lock
|
97
116
|
- houser.gemspec
|
98
117
|
- lib/houser.rb
|
99
118
|
- lib/houser/middleware.rb
|