Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module Cs
module Hanami
class PathResolver
def self.call(env)
new.call(env)
end
def call(env)
path = env['REQUEST_URI'].split('?').first # path without query string
return 'root' if path == '/'
configs = ::Hanami.configuration.mounted.values
app = configs.find { |config| path.start_with?(config.path_prefix) }
if app
# returns Api, Web, ...
app_constant = Module.const_get(app.to_s.split('::').first)
relative_path = path.split(app.path_prefix, 2).last
env_with_path = env.merge('PATH_INFO' => relative_path)
# returns either nil or something of the form "web_controllers/alive#index"
app_constant.routes.recognize(env_with_path).action
end
end
end
end
end
RSpec.describe Cs::Hanami::PathResolver do
subject { described_class.call(env) }
let(:env) do
verb, path = self.class.description.split
{ 'REQUEST_METHOD' => verb, 'REQUEST_URI' => path }
end
context 'GET /surveyors' do
it { is_expected.to eq('web_controllers/surveyors#index') }
end
context 'GET /surveyors.js' do
it { is_expected.to eq('web_controllers/surveyors#index') }
end
context 'GET /claims/new' do
it { is_expected.to eq('web_controllers/claims#new') }
end
context 'GET /claims/new?group=fruit' do
it { is_expected.to eq('web_controllers/claims#new') }
end
context 'GET /alive' do
it { is_expected.to eq('web_controllers/alive#index') }
end
context 'GET /go' do
it { is_expected.to eq('web_controllers/home#index') }
end
context 'PATCH /locales/42' do
it { is_expected.to eq('web_controllers/locales#update') }
end
context 'GET /' do
it { is_expected.to eq('root') }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment