Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #137 from pascalbetz/issue-136
Make hanami-router Rack 2.0 compatible.
  • Loading branch information
jodosha committed Jan 10, 2017
2 parents c0decb6 + 81ce7c5 commit 08ef380
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions hanami-router.gemspec
Expand Up @@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.3.0'

spec.add_dependency 'rack', '~> 1.6'
spec.add_dependency 'http_router', '~> 0.11'
spec.add_dependency 'rack', '~> 2.0'
spec.add_dependency 'http_router', '0.11.2'
spec.add_dependency 'hanami-utils', '~> 1.0.0.beta1'

spec.add_development_dependency 'bundler', '~> 1.5'
Expand Down
1 change: 1 addition & 0 deletions lib/hanami/routing/http_router.rb
Expand Up @@ -6,6 +6,7 @@
require 'hanami/routing/force_ssl'
require 'hanami/routing/error'
require 'hanami/utils/path_prefix'
require 'hanami/routing/http_router_monkey_patch'

module Hanami
module Routing
Expand Down
35 changes: 35 additions & 0 deletions lib/hanami/routing/http_router_monkey_patch.rb
@@ -0,0 +1,35 @@
# coding: utf-8
#
# This monkey patches http_router to make it Rack 2.0 compatible.
# Details see: https://github.com/hanami/router/issues/136
#
class HttpRouter
class Node
class Path < Node
def to_code
path_ivar = inject_root_ivar(self)
"#{"if !callback && request.path.size == 1 && request.path.first == '' && (request.rack_request.head? || request.rack_request.get?) && request.rack_request.path_info[-1] == ?/
response = ::Rack::Response.new
response.redirect(request.rack_request.path_info[0, request.rack_request.path_info.size - 1], 302)
return response.finish
end" if router.redirect_trailing_slash?}
#{"if request.#{router.ignore_trailing_slash? ? 'path_finished?' : 'path.empty?'}" unless route.match_partially}
if callback
request.called = true
callback.call(Response.new(request, #{path_ivar}))
else
env = request.rack_request.env
env['router.request'] = request
env['router.params'] ||= {}
#{"env['router.params'].merge!(Hash[#{param_names.inspect}.zip(request.params)])" if dynamic?}
@router.rewrite#{"_partial" if route.match_partially}_path_info(env, request)
response = @router.process_destination_path(#{path_ivar}, env)
return response unless router.pass_on_response(response)
end
#{"end" unless route.match_partially}"
end

end
end
end

0 comments on commit 08ef380

Please sign in to comment.