Skip to content

Commit ad20c3a

Browse files
authored
Support ruby 3 (#122)
* Try ruby 3 * Fix ruby-3 proc changes * Version bump 1.0.0
1 parent 93d4d49 commit ad20c3a

File tree

7 files changed

+40
-38
lines changed

7 files changed

+40
-38
lines changed

.github/workflows/test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ jobs:
1111
strategy:
1212
matrix:
1313
activesupport: ['5.2', '6.0', '6.1']
14-
ruby: [2.6, 2.7]
14+
ruby: [2.6, 2.7, 3.0]
1515
steps:
1616
- uses: actions/checkout@v2
17-
- name: Set up Ruby 2.6
17+
- name: Set up Ruby
1818
uses: ruby/setup-ruby@v1
1919
with:
2020
ruby-version: ${{ matrix.ruby }}

.ruby-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.7.2
1+
3.0.1

api_valve.gemspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ $LOAD_PATH.push File.expand_path('lib', __dir__)
33
# Describe your gem and declare its dependencies:
44
Gem::Specification.new do |s|
55
s.name = 'api_valve'
6-
s.version = ENV.fetch 'VERSION', '0.8.0'
6+
s.version = ENV.fetch 'VERSION', '1.0.0'
77
s.authors = ['mkon']
88
s.email = ['[email protected]']
99
s.homepage = 'https://github.com/mkon/api_valve'
1010
s.summary = 'Lightweight ruby/rack API reverse proxy or gateway'
1111
s.license = 'MIT'
12-
s.required_ruby_version = '~> 2.6'
12+
s.required_ruby_version = '>= 2.6', '< 3.1'
1313

1414
s.files = Dir['lib/**/*', 'README.md']
1515

lib/api_valve/proxy.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ def call(env)
3434

3535
def forward(methods, path_regexp = nil, options = {})
3636
options = options.with_indifferent_access
37-
route_set.append(methods, path_regexp, options.except(:request), proc { |request, match_data|
37+
route_set.append(methods, path_regexp, options.except(:request)) do |request, match_data|
3838
forwarder.call request, {'match_data' => match_data}.merge(options[:request] || {}).with_indifferent_access
39-
})
39+
end
4040
end
4141

4242
def forward_all(options = {})
4343
forward(RouteSet::METHODS, nil, options)
4444
end
4545

4646
def deny(methods, path_regexp = nil, with: 'Error::Forbidden')
47-
route_set.append(methods, path_regexp, {}, ->(*_args) { raise ApiValve.const_get(with) })
47+
route_set.append(methods, path_regexp, {}) { raise ApiValve.const_get(with) }
4848
end
4949

5050
protected

lib/api_valve/proxy/builder.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
module ApiValve
22
class Proxy
33
module Builder
4-
# Creates a n instance from a config hash and takes optional block
4+
# Creates an instance from a config hash and takes optional block
55
# which is executed in scope of the proxy
6-
def build(config)
7-
block = Proc.new if block_given? # capture the yield
6+
def build(config, &block)
87
from_hash(config).tap do |proxy|
98
proxy.instance_eval(&block) if block
109
end

lib/api_valve/route_set.rb

+20-20
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,42 @@ def match(env)
3232
[route, match_data]
3333
end
3434

35-
def delete(path = nil, options = {}, prok = nil)
36-
push :delete, path, options, prok || Proc.new
35+
def delete(path = nil, options = {}, &block)
36+
push :delete, path, options, &block
3737
end
3838

39-
def get(path = nil, options = {}, prok = nil)
40-
push :get, path, options, prok || Proc.new
39+
def get(path = nil, options = {}, &block)
40+
push :get, path, options, &block
4141
end
4242

43-
def head(path = nil, options = {}, prok = nil)
44-
push :head, path, options, prok || Proc.new
43+
def head(path = nil, options = {}, &block)
44+
push :head, path, options, &block
4545
end
4646

47-
def patch(path = nil, options = {}, prok = nil)
48-
push :patch, path, options, prok || Proc.new
47+
def patch(path = nil, options = {}, &block)
48+
push :patch, path, options, &block
4949
end
5050

51-
def post(path = nil, options = {}, prok = nil)
52-
push :post, path, options, prok || Proc.new
51+
def post(path = nil, options = {}, &block)
52+
push :post, path, options, &block
5353
end
5454

55-
def put(path = nil, options = {}, prok = nil)
56-
push :put, path, options, prok || Proc.new
55+
def put(path = nil, options = {}, &block)
56+
push :put, path, options, &block
5757
end
5858

59-
def any(path = nil, options = {}, prok = nil)
60-
append METHODS, path, options, prok || Proc.new
59+
def any(path = nil, options = {}, &block)
60+
append METHODS, path, options, &block
6161
end
6262

63-
def push(methods, regexp, options = {}, prok = nil)
64-
add_route :push, methods, regexp, options, prok || Proc.new
63+
def push(methods, regexp, options = {}, &block)
64+
add_route :push, methods, regexp, options, &block
6565
end
6666

6767
alias append push
6868

69-
def unshift(methods, regexp = nil, options = {}, prok = nil)
70-
add_route :unshift, methods, regexp, options, prok || Proc.new
69+
def unshift(methods, regexp = nil, options = {}, &block)
70+
add_route :unshift, methods, regexp, options, &block
7171
end
7272

7373
def reset_routes
@@ -76,10 +76,10 @@ def reset_routes
7676

7777
private
7878

79-
def add_route(how, methods, regexp, options, prok)
79+
def add_route(how, methods, regexp, options, &block)
8080
methods = METHODS if methods.to_s == 'any'
8181
Array.wrap(methods).each do |method|
82-
@routes[method].public_send how, Route.new(regexp, options, prok)
82+
@routes[method].public_send how, Route.new(regexp, options, block)
8383
end
8484
end
8585
end

spec/api_valve/route_set_spec.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
subject(:route_set) { described_class.new }
33

44
let(:rack_response) { [200, {'Content-Type' => 'text/plain'}, ['OK']] }
5-
let(:route_proc) { double(Proc, call: rack_response) } # rubocop:disable RSpec/VerifiedDoubles
5+
let(:route_proc) { proc { rack_response } } # rubocop:disable RSpec/VerifiedDoubles
66

7-
before { define_routes }
7+
before do
8+
allow(route_proc).to receive(:call).and_call_original
9+
define_routes
10+
end
811

912
%w(get head post put patch delete).each do |method|
1013
context "when routing #{method.upcase} requests" do
1114
let(:define_routes) do
12-
route_set.send(method, nil, {}, route_proc)
15+
route_set.send(method, nil, {}, &route_proc)
1316
end
1417
let(:env) do
1518
{
@@ -26,8 +29,8 @@
2629

2730
context 'when calling specific paths' do
2831
let(:define_routes) do
29-
route_set.send(method, %r{/exists/path}, {}, route_proc)
30-
route_set.send(method, %r{/alsoexists/path}, {}, ->(*_args) { [204, {}, []] })
32+
route_set.send(method, %r{/exists/path}, {}, &route_proc)
33+
route_set.send(method, %r{/alsoexists/path}, {}) { [204, {}, []] }
3134
end
3235
let(:env) do
3336
{
@@ -54,7 +57,7 @@
5457

5558
describe '#unshift' do
5659
let(:define_routes) do
57-
route_set.get %r{^/start}, {}, proc1
60+
route_set.get(%r{^/start}, {}, &proc1)
5861
end
5962
let(:proc1) { proc { rack_response } }
6063
let(:proc2) { proc { rack_response } }
@@ -70,7 +73,7 @@
7073
end
7174

7275
it 'adds the route the the front' do
73-
route_set.unshift(:get, %r{^/start}, {}, proc2)
76+
route_set.unshift(:get, %r{^/start}, {}, &proc2)
7477
route, _match_data = route_set.match(env)
7578
route.call
7679
expect(proc1).not_to have_received(:call)

0 commit comments

Comments
 (0)