rack-domain-filter 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c39fe6503eec58cd1f89d6c012c32bec90546d11
4
- data.tar.gz: 890c524df68a3fe57b494ccd3b5b4b1897b7e15e
3
+ metadata.gz: ecad2be7765f37121b0adcce79f408ee0eefc089
4
+ data.tar.gz: 6c46aa80f6a4af20434246db6fbab7e874bcfb42
5
5
  SHA512:
6
- metadata.gz: 7f95b9847a25e7e4e368b3e899a9f05d75ed83dd64b11f8c6ba644d07cac618d7fe555812cfe97f632c300bb83d7f988bfce0e6af398bd96390bd2e52994d87c
7
- data.tar.gz: a1d2521809ebaf33c025e98bfd92889d579380c414401c0d81a2ba4f9da7aeb6c8f489da1b5bd4fe56b9ec025e59010c1022675cb199faaa8767e07960b580ee
6
+ metadata.gz: 915dd5d8a5299c746ce40afe428fe229bc336167330223f3ad71910f5e18041710d1cfde6139900b26cc94d90604e24e52f2d82c2a65704060eeba3f1141f842
7
+ data.tar.gz: e4ab369e3f4c931e6d38c30960def317d19b8853880d151f145bbe920ec06a764a9908aa06aa014306428d4d074f70af142feb2824fba6e9036d8f2ffc35b1b0
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ sudo: false
3
+
4
+ rvm:
5
+ - 2.1.10
6
+ - 2.2.10
7
+ - 2.3.7
8
+ - 2.4.4
9
+ - 2.5.1
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Gitlab Status: A simple command to check the availability of Gitlab Site
1
+ # Rack Domain Filter
2
+
3
+ [![Build Status](https://travis-ci.org/mufid/rack-domain-filter.svg?branch=master)](https://travis-ci.org/mufid/rack-domain-filter)
2
4
 
3
5
  ## Prerequisites
4
6
 
@@ -21,7 +23,10 @@ See Yarddoc for more information.
21
23
  Suppose you have `Company` model. In Rails, you can do
22
24
  like this:
23
25
 
24
- Rack::SubdomainCompany.configure do |config|
26
+ # Put this inside application.rb, or
27
+ # any environment file in config/environments/*.rb
28
+
29
+ Rack::DomainFilter.configure do |config|
25
30
  config.filter_for /(.+).local.dev/ do |slug|
26
31
  Thread.current[:company] = Company.find_by!(slug)
27
32
  end
@@ -47,7 +52,7 @@ like this:
47
52
  end
48
53
  end
49
54
 
50
- config.middleware.use Rack::SubdomainCompany
55
+ config.middleware.use Rack::DomainFilter
51
56
 
52
57
  In your controller, you can get your current company with
53
58
  this syntax:
@@ -63,3 +68,27 @@ this syntax:
63
68
  Thread.current[:company]
64
69
  end
65
70
  end
71
+
72
+ You may want to put this into global filter. This
73
+ is quick but dirty solution.
74
+
75
+ class ApplicationRecord < ActiveRecord::Base
76
+ default_scope do
77
+ if Thread.current[:company]
78
+ where(company_id: Thread.current[:company].id)
79
+ else
80
+ nil
81
+ end
82
+ end
83
+ end
84
+
85
+ The best way to use this is to explictly
86
+ ask Model to search in current company scope
87
+
88
+ class ApplicationRecord < ActiveRecord::Base
89
+ scope :in_current_company, -> { where(company: Thread.current[:company]) }
90
+ end
91
+
92
+ class Manager < ApplicationRecord; end
93
+
94
+ @managers = Manager.in_current_company
data/Rakefile CHANGED
@@ -11,4 +11,4 @@ RuboCop::RakeTask.new
11
11
  require 'reek/rake/task'
12
12
  Reek::Rake::Task.new
13
13
 
14
- task default: %i[spec reek rubocop]
14
+ task default: %i[spec]
@@ -0,0 +1,11 @@
1
+ module Rack
2
+ class DomainFilter
3
+ module AfterRequestHandler
4
+ def run_after_request
5
+ config.after_requests_list.each do |block|
6
+ block.call
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,53 @@
1
+ module Rack
2
+ class DomainFilter
3
+
4
+ module ConfigurationDSL
5
+ def filter_for(pattern, &block)
6
+ @uri_mapping[pattern] = block
7
+ end
8
+
9
+ def after_request(&block)
10
+ @after_requests_list << block
11
+ end
12
+
13
+ def catch(klazz, &block)
14
+ @exception_catcher_mapping[klazz] = block
15
+ end
16
+
17
+ def no_match(&block)
18
+ raise 'Only allowed 1 no_match block!' if !@no_match.nil?
19
+
20
+ @no_match = block
21
+ end
22
+ end
23
+
24
+ # private
25
+ class Configuration
26
+
27
+ attr_accessor :uri_mapping
28
+ attr_accessor :after_requests_list
29
+ attr_accessor :exception_catcher_mapping
30
+
31
+ def initialize
32
+ @uri_mapping = {}
33
+ @after_requests_list = []
34
+ @exception_catcher_mapping = {}
35
+ @no_match = nil
36
+ end
37
+
38
+ include ConfigurationDSL
39
+
40
+ def no_match_block
41
+ @no_match
42
+ end
43
+
44
+ def allow_passthrough
45
+ @allow_passthrough = true
46
+ end
47
+
48
+ def allow_passthrough?
49
+ @allow_passthrough
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,15 @@
1
+ module Rack
2
+ class DomainFilter
3
+ module ExceptionHandler
4
+ def catch_exception(e)
5
+ config.exception_catcher_mapping.each_pair do |klazz, block|
6
+ if e.is_a?(klazz)
7
+ return block.call(e)
8
+ end
9
+ end
10
+
11
+ raise e
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ module Rack
2
+ class DomainFilter
3
+ module Matcher
4
+ # See https://www.youtube.com/watch?v=b77V0rkr5rk
5
+ # Use host_with_port for better performance
6
+ # See: https://github.com/rack/rack/blob/master/lib/rack/request.rb
7
+ def match_uri(env)
8
+ req = Rack::Request.new(env)
9
+ config.uri_mapping.each_pair do |pattern, block|
10
+ if pattern.is_a?(String)
11
+ break if match_string(pattern, req, block)
12
+ elsif pattern.is_a?(Regexp)
13
+ break if match_regex(pattern, req, block)
14
+ else
15
+ raise "Unknown pattern: #{pattern}. It must be a regex or a string!"
16
+ end
17
+ end
18
+ end
19
+
20
+ def can_respond_no_match?
21
+ !config.no_match_block.nil?
22
+ end
23
+
24
+ def trigger_no_match(env)
25
+ config.no_match_block.call(env)
26
+ end
27
+
28
+ def match_regex(regex, req, block)
29
+ matchdata = req.host_with_port.match(regex)
30
+ return if matchdata.nil?
31
+
32
+ block.call(matchdata[1], req.env)
33
+
34
+ @match_found = true
35
+
36
+ true
37
+ end
38
+
39
+ def match_string(string, req, block)
40
+ return if string != req.host_with_port
41
+
42
+ block.call(req.env)
43
+
44
+ @match_found = true
45
+
46
+ true
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class DomainFilter
3
- VERSION = '1.0.0'
3
+ VERSION = '1.0.1'
4
4
  end
5
5
  end
@@ -1,76 +1,13 @@
1
+ Dir["#{File.dirname(__FILE__)}/domain_filter/ext/*.rb"].sort.each do |path|
2
+ require "rack/domain_filter/ext/#{File.basename(path, '.rb')}"
3
+ end
4
+
1
5
  module Rack
2
6
  class DomainFilter
3
-
4
- module ExceptionHandler
5
- def catch_exception(e)
6
- config.exception_catcher_mapping.each_pair do |klazz, block|
7
- if e.is_a?(klazz)
8
- return block.call(e)
9
- end
10
- end
11
-
12
- raise e
13
- end
14
- end
15
-
16
- module AfterRequestHandler
17
- def run_after_request
18
- config.after_requests_list.each do |block|
19
- block.call
20
- end
21
- end
22
- end
23
-
24
- module Matcher
25
- # See https://www.youtube.com/watch?v=b77V0rkr5rk
26
- # Use host_with_port for better performance
27
- # See: https://github.com/rack/rack/blob/master/lib/rack/request.rb
28
- def match_uri(env)
29
- req = Rack::Request.new(env)
30
- config.uri_mapping.each_pair do |pattern, block|
31
- if pattern.is_a?(String)
32
- break if match_string(pattern, req, block)
33
- elsif pattern.is_a?(Regexp)
34
- break if match_regex(pattern, req, block)
35
- else
36
- raise "Unknown pattern: #{pattern}. It must be a regex or a string!"
37
- end
38
- end
39
- end
40
-
41
- def can_respond_no_match?
42
- !config.no_match_block.nil?
43
- end
44
-
45
- def trigger_no_match(env)
46
- config.no_match_block.call(env)
47
- end
48
-
49
- def match_regex(regex, req, block)
50
- matchdata = req.host_with_port.match(regex)
51
- return if matchdata.nil?
52
-
53
- block.call(matchdata[1], req.env)
54
-
55
- @match_found = true
56
-
57
- true
58
- end
59
-
60
- def match_string(string, req, block)
61
- return if string != req.host_with_port
62
-
63
- block.call(req.env)
64
-
65
- @match_found = true
66
-
67
- true
68
- end
69
- end
70
-
71
7
  def initialize(app, options={})
72
8
  @app = app
73
9
  @config = options[:config] || DomainFilter.global_configuration
10
+ @match_found = false
74
11
  end
75
12
 
76
13
  def call(env)
@@ -118,48 +55,5 @@ module Rack
118
55
  def verify_configuration!
119
56
 
120
57
  end
121
-
122
- class Configuration
123
-
124
- attr_accessor :uri_mapping
125
- attr_accessor :after_requests_list
126
- attr_accessor :exception_catcher_mapping
127
-
128
- def initialize
129
- @uri_mapping = {}
130
- @after_requests_list = []
131
- @exception_catcher_mapping = {}
132
- end
133
-
134
- def filter_for(pattern, &block)
135
- @uri_mapping[pattern] = block
136
- end
137
-
138
- def after_request(&block)
139
- @after_requests_list << block
140
- end
141
-
142
- def catch(klazz, &block)
143
- @exception_catcher_mapping[klazz] = block
144
- end
145
-
146
- def no_match(&block)
147
- raise 'Only allowed 1 no_match block!' if !@no_match.nil?
148
-
149
- @no_match = block
150
- end
151
-
152
- def no_match_block
153
- @no_match
154
- end
155
-
156
- def allow_passthrough
157
- @allow_passthrough = true
158
- end
159
-
160
- def allow_passthrough?
161
- @allow_passthrough
162
- end
163
- end
164
58
  end
165
59
  end
@@ -4,7 +4,7 @@ require 'rack/domain_filter/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.authors = ['Muhammad Mufid Afif']
7
- spec.description = 'Check Gitlab Web Status via HTTP Response.'
7
+ spec.description = 'Per-domain filter for Rack.'
8
8
  spec.email = ['mufidafif@icloud.com']
9
9
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.start_with?('spec/') }
10
10
  spec.homepage = 'https://github.com/wazaundtechnik/rack-subdomain-company'
@@ -16,7 +16,6 @@ Gem::Specification.new do |spec|
16
16
  spec.version = Rack::DomainFilter::VERSION
17
17
 
18
18
  spec.add_dependency 'rack'
19
- spec.add_dependency 'activesupport'
20
19
 
21
20
  # Test and build tools
22
21
  # The test shouldn't broken by the incompatible RSpec version.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-domain-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muhammad Mufid Afif
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-17 00:00:00.000000000 Z
11
+ date: 2018-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -192,7 +178,7 @@ dependencies:
192
178
  - - ">="
193
179
  - !ruby/object:Gem::Version
194
180
  version: '0'
195
- description: Check Gitlab Web Status via HTTP Response.
181
+ description: Per-domain filter for Rack.
196
182
  email:
197
183
  - mufidafif@icloud.com
198
184
  executables: []
@@ -205,6 +191,7 @@ files:
205
191
  - ".reek"
206
192
  - ".rspec"
207
193
  - ".rubocop.yml"
194
+ - ".travis.yml"
208
195
  - ".yardopts"
209
196
  - Gemfile
210
197
  - LICENSE.md
@@ -212,6 +199,10 @@ files:
212
199
  - Rakefile
213
200
  - lib/rack-domain-filter.rb
214
201
  - lib/rack/domain_filter.rb
202
+ - lib/rack/domain_filter/ext/after_request_handler.rb
203
+ - lib/rack/domain_filter/ext/configuration.rb
204
+ - lib/rack/domain_filter/ext/exception_handler.rb
205
+ - lib/rack/domain_filter/ext/matcher.rb
215
206
  - lib/rack/domain_filter/version.rb
216
207
  - rack-domain-filter.gemspec
217
208
  homepage: https://github.com/wazaundtechnik/rack-subdomain-company
@@ -237,5 +228,5 @@ rubyforge_project:
237
228
  rubygems_version: 2.6.13
238
229
  signing_key:
239
230
  specification_version: 4
240
- summary: Check Gitlab Web Status via HTTP Response.
231
+ summary: Per-domain filter for Rack.
241
232
  test_files: []