Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ruby 3 #122

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.2
3.0.1
4 changes: 2 additions & 2 deletions api_valve.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ $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'
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']

Expand Down
6 changes: 3 additions & 3 deletions lib/api_valve/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ 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 = {})
forward(RouteSet::METHODS, nil, 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
Expand Down
5 changes: 2 additions & 3 deletions lib/api_valve/proxy/builder.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 20 additions & 20 deletions lib/api_valve/route_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 10 additions & 7 deletions spec/api_valve/route_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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 } }
Expand All @@ -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)
Expand Down