sinatra-subdomain 0.2.1 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8842478e8af1a0b347a8f1af15e48bfabed5610
4
- data.tar.gz: 3264574516fbd993d2d768463c198e88b18c0771
3
+ metadata.gz: e503ce75701c38cf44dacb5e113445a0c318961e
4
+ data.tar.gz: d693c87a48d2061ef0066da58795764e93244bb3
5
5
  SHA512:
6
- metadata.gz: 1c6c4b4520b1cea1dc1f0b1d03ad3fd941d9c563f9ebf03212f7624779117625b231409e2afc1fa8449f5f5bb770a339ef56dd6bf1c043a0191c2ca917ff9af7
7
- data.tar.gz: a5d04262a271551df22ac3e3b84b9669e6db3c238e400312132b28fcc55a4d939fe2c805e6b4f691f56a0152558af3d9c6d4d24e44263f2b8dfec86b316f88e5
6
+ metadata.gz: ae45d43735b5e6f8e66122ba052cdb832035030b364e69cb7df5109571176bfc60f90e6ee8acede0fd30d218731a9f4c2efae0d481289922e3112952e82ef3f5
7
+ data.tar.gz: fc0cb786be4891dc12cdb80e528af1934311594ce0f708f6307e5ad625e62391f4ffcc524f8b4ada12a4009b3fbe8af05ea0a486d1d92ee84ad7fffa707ff84d
data/README.md CHANGED
@@ -20,14 +20,14 @@ require "sinatra/subdomain"
20
20
 
21
21
  # Specify which subdomain you want
22
22
  subdomain :foo do
23
- get '/' do
23
+ get "/" do
24
24
  "render page for FOO"
25
25
  end
26
26
  end
27
27
 
28
28
  # If any subdomain is set
29
29
  subdomain do
30
- get '/' do
30
+ get "/" do
31
31
  "render page for #{subdomain} subdomain"
32
32
  end
33
33
  end
@@ -44,14 +44,35 @@ class MyApp < Sinatra::Base
44
44
  register Sinatra::Subdomain
45
45
 
46
46
  subdomain :foo do
47
- get '/' do
47
+ get "/" do
48
48
  "render page for FOO"
49
49
  end
50
50
  end
51
51
 
52
52
  # If any subdomain is set
53
53
  subdomain do
54
- get '/' do
54
+ get "/" do
55
+ "render page for #{subdomain} subdomain"
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ You can also pass an array or regular expressions to match subdomains:
62
+
63
+ ```ruby
64
+ class MyApp < Sinatra::Base
65
+ register Sinatra::Subdomain
66
+
67
+ subdomain [:foo, :bar, :zaz] do
68
+ get "/" do
69
+ "render page for #{subdomain}"
70
+ end
71
+ end
72
+
73
+ # Matches www, www1, www2, etc.
74
+ subdomain /\Awww\d*\z/ do
75
+ get "/" do
55
76
  "render page for #{subdomain} subdomain"
56
77
  end
57
78
  end
@@ -68,8 +89,6 @@ require "sinatra/subdomain"
68
89
  set :tld_size, 2
69
90
  ```
70
91
 
71
- This extension was based on http://github.com/akahn/sinatra-subdomain/
72
-
73
92
  # License
74
93
 
75
94
  (The MIT License)
@@ -1,5 +1,4 @@
1
1
  source "https://rubygems.org"
2
-
3
2
  gemspec path: ".."
4
3
 
5
- gem 'sinatra', '~> 1.4', '>= 1.4.7'
4
+ gem "sinatra", "~> 1.4", ">= 1.4.7"
@@ -1,5 +1,4 @@
1
1
  source "https://rubygems.org"
2
-
3
2
  gemspec path: ".."
4
3
 
5
- gem 'sinatra', '~> 2.0.0.beta2'
4
+ gem "sinatra", "~> 2.0.0.beta2"
@@ -2,8 +2,8 @@ module Sinatra
2
2
  module Subdomain
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 2
6
- PATCH = 1
5
+ MINOR = 3
6
+ PATCH = 0
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
8
  end
9
9
  end
@@ -37,20 +37,29 @@ module Sinatra
37
37
  host =~ Resolv::IPv4::Regex || host =~ Resolv::IPv6::Regex
38
38
  end
39
39
 
40
+ def self.match_subdomain?(expected, actual)
41
+ expected.any? do |expected_subdomain|
42
+ case expected_subdomain
43
+ when true
44
+ !actual.nil?
45
+ when Symbol
46
+ actual.to_s == expected_subdomain.to_s
47
+ else
48
+ expected_subdomain === actual
49
+ end
50
+ end
51
+ end
52
+
40
53
  def self.route_added(verb, _path, _block)
41
54
  return unless subdomain && app
42
55
 
43
56
  routes = app.instance_variable_get("@routes")
44
57
  last_route = routes[verb].last
45
- expected = subdomain
58
+ expected = [subdomain].flatten.compact
46
59
 
47
60
  condition = app.instance_eval do
48
61
  generate_method :subdomain do
49
- if expected == true
50
- !subdomain.nil?
51
- else
52
- subdomain.to_s == expected.to_s
53
- end
62
+ ::Sinatra::Subdomain.match_subdomain?(expected, subdomain)
54
63
  end
55
64
  end
56
65
 
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.files = `git ls-files`.split("\n")
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
19
  s.add_dependency "sinatra"
@@ -0,0 +1,35 @@
1
+ require "test_helper"
2
+
3
+ class SubdomainsTest < Minitest::Test
4
+ def app
5
+ App
6
+ end
7
+
8
+ test "renders listed subdomain (a)" do
9
+ header "HOST", "a.example.com"
10
+ get "/"
11
+
12
+ assert_equal "array: a", last_response.body
13
+ end
14
+
15
+ test "renders listed subdomain (b)" do
16
+ header "HOST", "b.example.com"
17
+ get "/"
18
+
19
+ assert_equal "array: b", last_response.body
20
+ end
21
+
22
+ test "renders matched subdomain (c)" do
23
+ header "HOST", "c.example.com"
24
+ get "/"
25
+
26
+ assert_equal "regex: c", last_response.body
27
+ end
28
+
29
+ test "renders matched subdomain (d)" do
30
+ header "HOST", "d.example.com"
31
+ get "/"
32
+
33
+ assert_equal "regex: d", last_response.body
34
+ end
35
+ end
data/test/support/app.rb CHANGED
@@ -11,6 +11,14 @@ class App < Sinatra::Base
11
11
  get("/about") { "multiple: about #{subdomain}" }
12
12
  end
13
13
 
14
+ subdomain [:a, :b] do
15
+ get("/") { "array: #{subdomain}" }
16
+ end
17
+
18
+ subdomain /\A(c|d)\z/ do
19
+ get("/") { "regex: #{subdomain}" }
20
+ end
21
+
14
22
  subdomain do
15
23
  get("/") { "any: #{subdomain}" }
16
24
  get("/about") { "any: about #{subdomain}" }
@@ -1,9 +1,9 @@
1
1
  module SubdomainTests
2
2
  def app
3
- _tld = tld
3
+ current_tld = tld
4
4
 
5
5
  Class.new(App) do
6
- set :tld_size, _tld.split(".").size - 1
6
+ set :tld_size, current_tld.split(".").size - 1
7
7
  end
8
8
  end
9
9
 
@@ -27,42 +27,42 @@ module SubdomainTests
27
27
  header "HOST", "mail.example#{tld}"
28
28
  get "/"
29
29
 
30
- assert_equal "any: mail",last_response.body
30
+ assert_equal "any: mail", last_response.body
31
31
  end
32
32
 
33
33
  test "when any subdomain is required - renders about page" do
34
34
  header "HOST", "mail.example#{tld}"
35
35
  get "/about"
36
36
 
37
- assert_equal "any: about mail",last_response.body
37
+ assert_equal "any: about mail", last_response.body
38
38
  end
39
39
 
40
40
  test "when no subdomain is required - renders root page" do
41
41
  header "HOST", "example#{tld}"
42
42
  get "/"
43
43
 
44
- assert_equal "root",last_response.body
44
+ assert_equal "root", last_response.body
45
45
  end
46
46
 
47
47
  test "when no subdomain is required - renders about page" do
48
48
  header "HOST", "example#{tld}"
49
49
  get "/about"
50
50
 
51
- assert_equal "about",last_response.body
51
+ assert_equal "about", last_response.body
52
52
  end
53
53
 
54
54
  test "when multi-subdomain is required - renders root page" do
55
55
  header "HOST", "foo.bar.example#{tld}"
56
56
  get "/"
57
57
 
58
- assert_equal "multiple: foo.bar",last_response.body
58
+ assert_equal "multiple: foo.bar", last_response.body
59
59
  end
60
60
 
61
61
  test "when multi-subdomain is required - renders about page" do
62
62
  header "HOST", "foo.bar.example#{tld}"
63
63
  get "/about"
64
64
 
65
- assert_equal "multiple: about foo.bar",last_response.body
65
+ assert_equal "multiple: about foo.bar", last_response.body
66
66
  end
67
67
  end
68
68
  end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'simplecov'
1
+ require "simplecov"
2
2
  SimpleCov.start
3
3
 
4
4
  require "bundler/setup"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-subdomain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -115,6 +115,7 @@ files:
115
115
  - test/sinatra-subdomain/multiple_subdomain_test.rb
116
116
  - test/sinatra-subdomain/multiple_tlds_test.rb
117
117
  - test/sinatra-subdomain/single_tld_test.rb
118
+ - test/sinatra-subdomain/subdomains_test.rb
118
119
  - test/support/app.rb
119
120
  - test/support/subdomain_tests.rb
120
121
  - test/test_helper.rb
@@ -147,6 +148,7 @@ test_files:
147
148
  - test/sinatra-subdomain/multiple_subdomain_test.rb
148
149
  - test/sinatra-subdomain/multiple_tlds_test.rb
149
150
  - test/sinatra-subdomain/single_tld_test.rb
151
+ - test/sinatra-subdomain/subdomains_test.rb
150
152
  - test/support/app.rb
151
153
  - test/support/subdomain_tests.rb
152
154
  - test/test_helper.rb