From e3d82abbd23ebc787001dc17c7bfc330508058be Mon Sep 17 00:00:00 2001 From: Konstantin Munteanu Date: Tue, 6 Jul 2021 20:17:02 +0200 Subject: [PATCH 1/3] Try ruby 3 --- .github/workflows/test.yml | 4 ++-- api_valve.gemspec | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65b5ca4..72e94b6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,10 +11,10 @@ jobs: strategy: matrix: activesupport: ['5.2', '6.0', '6.1'] - ruby: [2.6, 2.7] + ruby: [2.6, 2.7, 3.0] steps: - uses: actions/checkout@v2 - - name: Set up Ruby 2.6 + - name: Set up Ruby uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} diff --git a/api_valve.gemspec b/api_valve.gemspec index 370a5f8..c97050f 100644 --- a/api_valve.gemspec +++ b/api_valve.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/mkon/api_valve' s.summary = 'Lightweight ruby/rack API reverse proxy or gateway' s.license = 'MIT' - s.required_ruby_version = '~> 2.6' + s.required_ruby_version = '>= 2.6', '< 3.1' s.files = Dir['lib/**/*', 'README.md'] From 6bad401250eb672a7a183709e6858febbfe102f2 Mon Sep 17 00:00:00 2001 From: Konstantin Munteanu Date: Wed, 7 Jul 2021 10:20:45 +0200 Subject: [PATCH 2/3] Fix ruby-3 proc changes --- .ruby-version | 2 +- lib/api_valve/proxy.rb | 6 ++--- lib/api_valve/proxy/builder.rb | 5 ++-- lib/api_valve/route_set.rb | 40 ++++++++++++++++---------------- spec/api_valve/route_set_spec.rb | 17 ++++++++------ 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/.ruby-version b/.ruby-version index 37c2961..cb2b00e 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.7.2 +3.0.1 diff --git a/lib/api_valve/proxy.rb b/lib/api_valve/proxy.rb index 57cc317..d0a62e4 100644 --- a/lib/api_valve/proxy.rb +++ b/lib/api_valve/proxy.rb @@ -34,9 +34,9 @@ def call(env) def forward(methods, path_regexp = nil, options = {}) options = options.with_indifferent_access - route_set.append(methods, path_regexp, options.except(:request), proc { |request, match_data| + route_set.append(methods, path_regexp, options.except(:request)) do |request, match_data| forwarder.call request, {'match_data' => match_data}.merge(options[:request] || {}).with_indifferent_access - }) + end end def forward_all(options = {}) @@ -44,7 +44,7 @@ def forward_all(options = {}) end def deny(methods, path_regexp = nil, with: 'Error::Forbidden') - route_set.append(methods, path_regexp, {}, ->(*_args) { raise ApiValve.const_get(with) }) + route_set.append(methods, path_regexp, {}) { raise ApiValve.const_get(with) } end protected diff --git a/lib/api_valve/proxy/builder.rb b/lib/api_valve/proxy/builder.rb index 1736942..b8ac72a 100644 --- a/lib/api_valve/proxy/builder.rb +++ b/lib/api_valve/proxy/builder.rb @@ -1,10 +1,9 @@ module ApiValve class Proxy module Builder - # Creates a n instance from a config hash and takes optional block + # Creates an instance from a config hash and takes optional block # which is executed in scope of the proxy - def build(config) - block = Proc.new if block_given? # capture the yield + def build(config, &block) from_hash(config).tap do |proxy| proxy.instance_eval(&block) if block end diff --git a/lib/api_valve/route_set.rb b/lib/api_valve/route_set.rb index d6361f0..c1b56d5 100644 --- a/lib/api_valve/route_set.rb +++ b/lib/api_valve/route_set.rb @@ -32,42 +32,42 @@ def match(env) [route, match_data] end - def delete(path = nil, options = {}, prok = nil) - push :delete, path, options, prok || Proc.new + def delete(path = nil, options = {}, &block) + push :delete, path, options, &block end - def get(path = nil, options = {}, prok = nil) - push :get, path, options, prok || Proc.new + def get(path = nil, options = {}, &block) + push :get, path, options, &block end - def head(path = nil, options = {}, prok = nil) - push :head, path, options, prok || Proc.new + def head(path = nil, options = {}, &block) + push :head, path, options, &block end - def patch(path = nil, options = {}, prok = nil) - push :patch, path, options, prok || Proc.new + def patch(path = nil, options = {}, &block) + push :patch, path, options, &block end - def post(path = nil, options = {}, prok = nil) - push :post, path, options, prok || Proc.new + def post(path = nil, options = {}, &block) + push :post, path, options, &block end - def put(path = nil, options = {}, prok = nil) - push :put, path, options, prok || Proc.new + def put(path = nil, options = {}, &block) + push :put, path, options, &block end - def any(path = nil, options = {}, prok = nil) - append METHODS, path, options, prok || Proc.new + def any(path = nil, options = {}, &block) + append METHODS, path, options, &block end - def push(methods, regexp, options = {}, prok = nil) - add_route :push, methods, regexp, options, prok || Proc.new + def push(methods, regexp, options = {}, &block) + add_route :push, methods, regexp, options, &block end alias append push - def unshift(methods, regexp = nil, options = {}, prok = nil) - add_route :unshift, methods, regexp, options, prok || Proc.new + def unshift(methods, regexp = nil, options = {}, &block) + add_route :unshift, methods, regexp, options, &block end def reset_routes @@ -76,10 +76,10 @@ def reset_routes private - def add_route(how, methods, regexp, options, prok) + def add_route(how, methods, regexp, options, &block) methods = METHODS if methods.to_s == 'any' Array.wrap(methods).each do |method| - @routes[method].public_send how, Route.new(regexp, options, prok) + @routes[method].public_send how, Route.new(regexp, options, block) end end end diff --git a/spec/api_valve/route_set_spec.rb b/spec/api_valve/route_set_spec.rb index 6a2d96a..23cd78e 100644 --- a/spec/api_valve/route_set_spec.rb +++ b/spec/api_valve/route_set_spec.rb @@ -2,14 +2,17 @@ subject(:route_set) { described_class.new } let(:rack_response) { [200, {'Content-Type' => 'text/plain'}, ['OK']] } - let(:route_proc) { double(Proc, call: rack_response) } # rubocop:disable RSpec/VerifiedDoubles + let(:route_proc) { proc { rack_response } } # rubocop:disable RSpec/VerifiedDoubles - before { define_routes } + before do + allow(route_proc).to receive(:call).and_call_original + define_routes + end %w(get head post put patch delete).each do |method| context "when routing #{method.upcase} requests" do let(:define_routes) do - route_set.send(method, nil, {}, route_proc) + route_set.send(method, nil, {}, &route_proc) end let(:env) do { @@ -26,8 +29,8 @@ context 'when calling specific paths' do let(:define_routes) do - route_set.send(method, %r{/exists/path}, {}, route_proc) - route_set.send(method, %r{/alsoexists/path}, {}, ->(*_args) { [204, {}, []] }) + route_set.send(method, %r{/exists/path}, {}, &route_proc) + route_set.send(method, %r{/alsoexists/path}, {}) { [204, {}, []] } end let(:env) do { @@ -54,7 +57,7 @@ describe '#unshift' do let(:define_routes) do - route_set.get %r{^/start}, {}, proc1 + route_set.get(%r{^/start}, {}, &proc1) end let(:proc1) { proc { rack_response } } let(:proc2) { proc { rack_response } } @@ -70,7 +73,7 @@ end it 'adds the route the the front' do - route_set.unshift(:get, %r{^/start}, {}, proc2) + route_set.unshift(:get, %r{^/start}, {}, &proc2) route, _match_data = route_set.match(env) route.call expect(proc1).not_to have_received(:call) From 91871442721dea4a5040ada9f3184a92521e3cda Mon Sep 17 00:00:00 2001 From: Konstantin Munteanu Date: Wed, 7 Jul 2021 10:31:01 +0200 Subject: [PATCH 3/3] Version bump 1.0.0 --- api_valve.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_valve.gemspec b/api_valve.gemspec index c97050f..f14eea2 100644 --- a/api_valve.gemspec +++ b/api_valve.gemspec @@ -3,7 +3,7 @@ $LOAD_PATH.push File.expand_path('lib', __dir__) # Describe your gem and declare its dependencies: Gem::Specification.new do |s| s.name = 'api_valve' - s.version = ENV.fetch 'VERSION', '0.8.0' + s.version = ENV.fetch 'VERSION', '1.0.0' s.authors = ['mkon'] s.email = ['konstantin@munteanu.de'] s.homepage = 'https://github.com/mkon/api_valve'