Skip to content

Commit 0e9cdea

Browse files
committed
Lazily evaluate block
1 parent 0d17b91 commit 0e9cdea

4 files changed

Lines changed: 32 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Lazily evaluate block ([Robert Keresnyei][yenshirak])
6+
37
## 1.3.0 (2024-03-15)
48

59
* Respond to invalid request URL with 400 ([Gareth Jones][G-Rath])
@@ -100,3 +104,4 @@
100104
[olleolleolle]: https://github.com/olleolleolle
101105
[vinnydiehl]: https://github.com/vinnydiehl
102106
[G-Rath]: https://github.com/G-Rath
107+
[yenshirak]: https://github.com/yenshirak

lib/rack/canonical_host.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def call(env)
1717

1818
return request.bad_request_response unless request.valid?
1919

20-
host = evaluate_host(env)
21-
redirect = Redirect.new(env, host, options)
20+
redirect = Redirect.new(env, host, options, &block)
2221

2322
if redirect.canonical?
2423
app.call(env)
@@ -33,11 +32,5 @@ def call(env)
3332
attr_accessor :host
3433
attr_accessor :options
3534
attr_accessor :block
36-
37-
private
38-
39-
def evaluate_host(env)
40-
block and block.call(env) or host
41-
end
4235
end
4336
end

lib/rack/canonical_host/redirect.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ class Redirect
1515
</html>
1616
HTML
1717

18-
def initialize(env, host, options={})
18+
def initialize(env, host=nil, options={}, &block)
1919
self.env = env
20-
self.host = host
20+
self.default_host = host
21+
self.block = block
2122
self.ignore = Array(options[:ignore])
2223
self.conditions = Array(options[:if])
2324
self.cache_control = options[:cache_control]
@@ -35,7 +36,8 @@ def response
3536
protected
3637

3738
attr_accessor :env
38-
attr_accessor :host
39+
attr_accessor :default_host
40+
attr_accessor :block
3941
attr_accessor :ignore
4042
attr_accessor :conditions
4143
attr_accessor :cache_control
@@ -53,6 +55,12 @@ def any_match?(patterns, request_uri)
5355
}
5456
end
5557

58+
def enabled?
59+
return true if conditions.empty?
60+
61+
any_match?(conditions, request_uri)
62+
end
63+
5664
def headers
5765
{
5866
'cache-control' => cache_control,
@@ -61,10 +69,9 @@ def headers
6169
}.reject { |_, value| !value }
6270
end
6371

64-
def enabled?
65-
return true if conditions.empty?
66-
67-
any_match?(conditions, request_uri)
72+
def host
73+
return @host if defined?(@host)
74+
@host = block && block.call(env) || default_host
6875
end
6976

7077
def ignored?

spec/rack/canonical_host_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,18 @@ def call_app
298298
let(:url) { 'http://www.example.com/full/path' }
299299
end
300300
end
301+
302+
context 'with :if option that rejects the request' do
303+
let(:url) { 'http://api.example.com/full/path' }
304+
305+
let(:app) {
306+
build_app(nil, if: ->(uri) { uri.host == 'example.com' }) { raise }
307+
}
308+
309+
it 'does not evaluate the block' do
310+
expect { call_app }.to_not raise_error
311+
end
312+
end
301313
end
302314
end
303315
end

0 commit comments

Comments
 (0)