Last active
March 19, 2021 16:07
-
-
Save phillipoertel/5d99e66d6181b037697cbfce9d0a6d5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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