Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1953 from SumOfUs/feature/redirect_pronto
Browse files Browse the repository at this point in the history
Middleware to redirect users to pronto for en petition action pages
  • Loading branch information
yeseniamolinab committed Jun 2, 2022
2 parents 309180e + 56d9c6d commit 4d7a0ec
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def campaign_action_count
end

def language_code
language&.code || I18n.default_locale
language&.code || I18n.default_locale.to_s
end

def optimization_tags
Expand Down
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require_relative 'boot'

require 'rails/all'
require './lib/middleware/pronto'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand Down Expand Up @@ -30,6 +30,7 @@ class Application < Rails::Application

config.i18n.available_locales = %i[en fr de es pt nl ar]
config.i18n.enforce_available_locales = true
config.middleware.use Pronto
end
end

Expand Down
2 changes: 2 additions & 0 deletions config/settings/development.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# General Champaign settings.
pronto:
domain: 'https://pronto-development.sumofus.org'
google_optimize:
key: "OPT-5XL8RFP"
secret_key_base: secret_key_base
Expand Down
2 changes: 2 additions & 0 deletions config/settings/production.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Production Settings for Champaign
pronto:
domain: <%= ENV['PRONTO_DOMAIN'] || 'https://action.sumofus.org' %>

google_optimize:
key: <%= ENV['GOOGLE_OPTIMIZE'] || 'OPT-PS8FHLC' %>
Expand Down
32 changes: 32 additions & 0 deletions lib/middleware/pronto.rb
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
43 changes: 43 additions & 0 deletions spec/lib/pronto_spec.rb
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

0 comments on commit 4d7a0ec

Please sign in to comment.