houser 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.md +2 -0
- data/lib/houser/middleware.rb +5 -3
- data/lib/houser/version.rb +1 -1
- data/spec/houser_spec.rb +43 -21
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a37a9a6b52d4e15a8bf6f4be8b69a19386f2272a
|
4
|
+
data.tar.gz: f0f2777b9488166cd7172e8aca074fb876cda626
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4589b6cd35ef614f1ec56e309e7d8f4a4498e867c4544179c9054f90eade5da87eb26e889f0d9c033d5b5160ca79dd21e2274537274ed1440b2127ef37daffa
|
7
|
+
data.tar.gz: 33bf73317fe420f8bcef9768f04c700fa4c71c5fc609a3cdcdf90912b6767b29aedef009b8a2a67d7e5c6387ebb8d0533e28f7b1679139ab09d27c7b29321c6e
|
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
script: 'bundle exec rspec spec'
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Houser
|
2
2
|
|
3
|
+
![Build Status](https://api.travis-ci.org/radar/houser.png?branch=master)
|
4
|
+
|
3
5
|
This is the multitenancy gem that will be used in the Multitenancy with Rails book as an alternative method to PostgreSQL schemas which can have their own set of problems.
|
4
6
|
|
5
7
|
Houser provides you with two Rack environment variables which can then be used in your application to scope resources correctly. That is all it does for the time being, and it will probably do more in the future.
|
data/lib/houser/middleware.rb
CHANGED
@@ -2,9 +2,11 @@ require 'rack'
|
|
2
2
|
|
3
3
|
module Houser
|
4
4
|
class Middleware
|
5
|
+
attr_accessor :options
|
6
|
+
|
5
7
|
def initialize(app, options={})
|
6
8
|
@options = options
|
7
|
-
@options[:
|
9
|
+
@options[:class] = Object.const_get(options[:class_name])
|
8
10
|
@options[:tld_length] ||= 1
|
9
11
|
@app = app
|
10
12
|
end
|
@@ -12,7 +14,7 @@ module Houser
|
|
12
14
|
def call(env)
|
13
15
|
domain_parts = env['HTTP_HOST'].split('.')
|
14
16
|
|
15
|
-
if domain_parts.length > 1 +
|
17
|
+
if domain_parts.length > 1 + options[:tld_length]
|
16
18
|
subdomain = domain_parts[0]
|
17
19
|
find_tenant(env, subdomain)
|
18
20
|
end
|
@@ -23,7 +25,7 @@ module Houser
|
|
23
25
|
private
|
24
26
|
|
25
27
|
def find_tenant(env, subdomain)
|
26
|
-
object =
|
28
|
+
object = options[:class].find_by(subdomain: subdomain)
|
27
29
|
if object
|
28
30
|
env['X-Houser-Subdomain'] = subdomain
|
29
31
|
env['X-Houser-Object'] = object
|
data/lib/houser/version.rb
CHANGED
data/spec/houser_spec.rb
CHANGED
@@ -17,17 +17,57 @@ describe Houser::Middleware do
|
|
17
17
|
|
18
18
|
let(:subdomain) { 'account1' }
|
19
19
|
|
20
|
+
before do
|
21
|
+
stub_const('Account', Class.new)
|
22
|
+
end
|
23
|
+
|
20
24
|
def env_for(url, opts={})
|
21
25
|
opts.merge!('HTTP_HOST' => URI.parse(url).host)
|
22
26
|
Rack::MockRequest.env_for(url, opts)
|
23
27
|
end
|
24
28
|
|
25
29
|
it "does nothing for non-subdomained requests" do
|
26
|
-
stub_const('Account', Class.new)
|
27
30
|
expect(Account).to_not receive(:find_by)
|
28
31
|
code, env = middleware.call(env_for("http://example.com"))
|
29
32
|
expect(env['X-Houser-Subdomain']).to be_nil
|
30
|
-
expect(env['X-Houser-
|
33
|
+
expect(env['X-Houser-Object']).to be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "sets X-HOUSER-ID header for known subdomains" do
|
37
|
+
account = double(id: 1)
|
38
|
+
expect(Account).to receive(:find_by).with(subdomain: subdomain).and_return(account)
|
39
|
+
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
40
|
+
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
41
|
+
expect(env['X-Houser-Object']).to eq(account)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns no headers for unknown subdomains" do
|
45
|
+
expect(Account).to receive(:find_by).with(subdomain: subdomain).and_return(nil)
|
46
|
+
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
47
|
+
expect(env['X-Houser-Subdomain']).to be_nil
|
48
|
+
expect(env['X-Houser-Object']).to be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
context "with a different class name" do
|
52
|
+
let(:options) do
|
53
|
+
{
|
54
|
+
class_name: 'Store'
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
let(:store) { double(id: 2) }
|
59
|
+
|
60
|
+
before do
|
61
|
+
stub_const('Store', Class.new)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "calls the right class" do
|
65
|
+
expect(Account).to_not receive(:find_by)
|
66
|
+
expect(Store).to receive(:find_by).with(subdomain: subdomain).and_return(store)
|
67
|
+
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
68
|
+
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
69
|
+
expect(env['X-Houser-Object']).to eq(store)
|
70
|
+
end
|
31
71
|
end
|
32
72
|
|
33
73
|
context "with more than one-level of TLD" do
|
@@ -38,29 +78,11 @@ describe Houser::Middleware do
|
|
38
78
|
}
|
39
79
|
end
|
40
80
|
|
41
|
-
it "
|
42
|
-
stub_const('Account', Class.new)
|
81
|
+
it "supports more than one-level of TLD" do
|
43
82
|
expect(Account).to_not receive(:find_by)
|
44
83
|
code, env = middleware.call(env_for("http://example.co.uk"))
|
45
84
|
expect(env['X-Houser-Subdomain']).to be_nil
|
46
85
|
expect(env['X-Houser-ID']).to be_nil
|
47
86
|
end
|
48
87
|
end
|
49
|
-
|
50
|
-
it "sets X-HOUSER-ID header for known subdomains" do
|
51
|
-
stub_const('Account', Class.new)
|
52
|
-
account = double(id: 1)
|
53
|
-
expect(Account).to receive(:find_by).with(subdomain: subdomain).and_return(account)
|
54
|
-
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
55
|
-
expect(env['X-Houser-Subdomain']).to eq(subdomain)
|
56
|
-
expect(env['X-Houser-ID']).to eq(1)
|
57
|
-
end
|
58
|
-
|
59
|
-
it "returns no headers for unknown subdomains" do
|
60
|
-
stub_const('Account', Class.new)
|
61
|
-
expect(Account).to receive(:find_by).with(subdomain: subdomain).and_return(nil)
|
62
|
-
code, env = middleware.call(env_for("http://#{subdomain}.example.com"))
|
63
|
-
expect(env['X-Houser-Subdomain']).to be_nil
|
64
|
-
expect(env['X-Houser-ID']).to be_nil
|
65
|
-
end
|
66
88
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: houser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Bigg
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
91
92
|
- CONTRIBUTING.md
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE.txt
|