This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1953 from SumOfUs/feature/redirect_pronto
Middleware to redirect users to pronto for en petition action pages
- Loading branch information
Showing
6 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Pronto | ||
class PathMatcher | ||
PATTERN = %r{^((?:/a/)(?<slug>[\w-]+)(?:/?)(?:(\?.*)*))$}.freeze | ||
|
||
def self.match(path) | ||
match = PATTERN.match(path) | ||
return match[:slug] if match | ||
end | ||
end | ||
|
||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
@status, @headers, @response = @app.call(env) | ||
req = Rack::Request.new(env) | ||
|
||
path_match = PathMatcher.match(req.path) | ||
|
||
if path_match | ||
page = Page.find_by(slug: path_match) | ||
|
||
if page&.language_code == 'en' && page&.petition_page? | ||
location = "#{Settings.pronto.domain}/#{req.fullpath}" | ||
return [301, { 'Location' => location, 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []] | ||
end | ||
end | ||
|
||
[@status, @headers, @response] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require './lib/middleware/pronto' | ||
require 'spec_helper' | ||
|
||
describe Pronto::PathMatcher do | ||
describe '.match' do | ||
let(:slug) { '/a/foo-bar' } | ||
let(:slug_with_trailing_slash) { '/a/foo-bar/' } | ||
let(:slug_with_query_params) { '/a/foo-bar?akid=123.456.567' } | ||
let(:slug_with_trailing_slash_and_query_params) { '/a/foo-bar/?akid=123.456.789' } | ||
let(:slug_with_follow_up) { '/a/foo-bar/follow-up?foo=bar' } | ||
let(:no_slug) { '/about/staff' } | ||
|
||
context 'returns slug' do | ||
it 'witout trailing slash' do | ||
expect(Pronto::PathMatcher.match(slug)).to eq('foo-bar') | ||
end | ||
|
||
it 'with trailing slash' do | ||
expect(Pronto::PathMatcher.match(slug_with_trailing_slash)).to eq('foo-bar') | ||
end | ||
|
||
it 'with query params' do | ||
expect(Pronto::PathMatcher.match(slug_with_query_params)).to eq('foo-bar') | ||
end | ||
|
||
it 'with trailing slash and query params' do | ||
expect(Pronto::PathMatcher.match(slug_with_trailing_slash_and_query_params)).to eq('foo-bar') | ||
end | ||
end | ||
|
||
context 'returns nil' do | ||
it 'with follow up' do | ||
expect(Pronto::PathMatcher.match(slug_with_follow_up)).to be_nil | ||
end | ||
|
||
it 'when no slug present' do | ||
expect(Pronto::PathMatcher.match(no_slug)).to be_nil | ||
end | ||
end | ||
end | ||
end |