Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2eb01ec

Browse files
committed
Rename Matcher -> Rule
1 parent df3647d commit 2eb01ec

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ gem "rack-reverse-proxy", require: "rack/reverse_proxy"
1717
```
1818

1919
## Usage
20-
Matchers can be a regex or a string. If a regex is used, you can use the subcaptures in your forwarding url by denoting them with a `$`.
2120

22-
Right now if more than one matcher matches any given route, it throws an exception for an ambiguous match. This will probably change later. If no match is found, the call is forwarded to your application.
21+
Rules can be a regex or a string. If a regex is used, you can use the subcaptures in your forwarding url by denoting them with a `$`.
22+
23+
Right now if more than one rule matches any given route, it throws an exception for an ambiguous match. This will probably change later. If no match is found, the call is forwarded to your application.
2324

2425
Below is an example for configuring the middleware:
2526

@@ -43,7 +44,8 @@ end
4344
run app
4445
```
4546

46-
reverse_proxy_options sets global options for all reverse proxies. Available options are:
47+
`reverse_proxy_options` sets global options for all reverse proxies. Available options are:
48+
4749
* `:preserve_host` Set to false to omit Host headers
4850
* `:username` username for basic auth
4951
* `:password` password for basic auth

lib/rack_reverse_proxy/matcher.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module RackReverseProxy
33
# rubocop:disable Metrics/MethodLength
44
# rubocop:disable Metrics/AbcSize
55

6-
# Matcher understands if specific url matches encapsulated rule or not
7-
class Matcher
6+
# Rule understands which urls need to be proxied
7+
class Rule
88
def initialize(matcher, url = nil, options = {})
99
@default_url = url
1010
@url = url
@@ -15,13 +15,13 @@ def initialize(matcher, url = nil, options = {})
1515
elsif matcher.respond_to?(:match)
1616
@matcher = matcher
1717
else
18-
fail ArgumentError, "Invalid Matcher for reverse_proxy"
18+
fail ArgumentError, "Invalid Rule for reverse_proxy"
1919
end
2020
end
2121

2222
attr_reader :matcher, :url, :default_url, :options
2323

24-
def match?(path, *args)
24+
def proxy?(path, *args)
2525
match_path(path, *args) ? true : false
2626
end
2727

lib/rack_reverse_proxy/middleware.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Middleware
1616

1717
def initialize(app = nil, &b)
1818
@app = app || lambda { |_| [404, [], []] }
19-
@matchers = []
19+
@rules = []
2020
@global_options = {
2121
:preserve_host => true,
2222
:x_forwarded_host => true,
@@ -28,32 +28,32 @@ def initialize(app = nil, &b)
2828

2929
def call(env)
3030
rackreq = Rack::Request.new(env)
31-
matcher = get_matcher(
31+
rule = get_rule(
3232
rackreq.fullpath,
3333
Rack::Proxy.extract_http_request_headers(rackreq.env),
3434
rackreq
3535
)
36-
return @app.call(env) if matcher.nil?
36+
return @app.call(env) if rule.nil?
3737

3838
if @global_options[:newrelic_instrumentation]
3939
# Rack::ReverseProxy/foo/bar#GET
4040
action_path = rackreq.path.gsub(%r{/\d+}, "/:id").gsub(%r{^/}, "")
4141
action_name = "#{action_path}/#{rackreq.request_method}"
4242
perform_action_with_newrelic_trace(:name => action_name, :request => rackreq) do
43-
proxy(env, rackreq, matcher)
43+
proxy(env, rackreq, rule)
4444
end
4545
else
46-
proxy(env, rackreq, matcher)
46+
proxy(env, rackreq, rule)
4747
end
4848
end
4949

5050
private
5151

52-
def proxy(env, source_request, matcher)
53-
uri = matcher.get_uri(source_request.fullpath, env)
52+
def proxy(env, source_request, rule)
53+
uri = rule.get_uri(source_request.fullpath, env)
5454
return @app.call(env) if uri.nil?
5555

56-
options = @global_options.dup.merge(matcher.options)
56+
options = @global_options.dup.merge(rule.options)
5757

5858
# Initialize request
5959
target_request = Net::HTTP.const_get(
@@ -118,9 +118,9 @@ def proxy(env, source_request, matcher)
118118
[target_response.status, response_headers, target_response.body]
119119
end
120120

121-
def get_matcher(path, headers, rackreq)
122-
matches = @matchers.select do |matcher|
123-
matcher.match?(path, headers, rackreq)
121+
def get_rule(path, headers, rackreq)
122+
matches = @rules.select do |rule|
123+
rule.proxy?(path, headers, rackreq)
124124
end
125125

126126
if matches.length < 1
@@ -136,11 +136,11 @@ def reverse_proxy_options(options)
136136
@global_options = options
137137
end
138138

139-
def reverse_proxy(matcher, url = nil, opts = {})
140-
if matcher.is_a?(String) && url.is_a?(String) && URI(url).class == URI::Generic
139+
def reverse_proxy(rule, url = nil, opts = {})
140+
if rule.is_a?(String) && url.is_a?(String) && URI(url).class == URI::Generic
141141
fail Errors::GenericURI.new, url
142142
end
143-
@matchers << Matcher.new(matcher, url, opts)
143+
@rules << Rule.new(rule, url, opts)
144144
end
145145

146146
def format_headers(headers)

0 commit comments

Comments
 (0)