Skip to content

Instantly share code, notes, and snippets.

@dogweather
Created April 12, 2024 20:10
Show Gist options
  • Save dogweather/29146f1ee793abfb46e3e4527798c24a to your computer and use it in GitHub Desktop.
Save dogweather/29146f1ee793abfb46e3e4527798c24a to your computer and use it in GitHub Desktop.
Example of redirect code in Ruby, plus tests
elsif ors?
case param_id
when 'or', 'ors', 'ors_', 'ors_about'
return ORS_ROOT
when /^nrs_/
return NRS_ROOT + '/' + param_id
when /^tex\._/
return TEXAS_STATUTES_ROOT + '/' + param_id
when %r{^ors/chapter/([^/]+)$}i
return ORS_ROOT + "/ors_chapter_#{$1}"
when %r{^ors/([^/]+)$}i
return ORS_ROOT + "/ors_#{$1}"
when /^(ors_[0-9a-z.]+)\%/i
return ORS_ROOT + "/#{$1}"
when /^ors_([0-9a-z]+)$/i
return ORS_ROOT + "/ors_chapter_#{$1}"
when %r{^(?!(ors|crs|n\.y\.)_)}
return ORS_ROOT
when %r{^ors_[^/]+(/ors_[^/]+)$}
return ORS_ROOT + $1
end
elsif texas_statutes?
case param_id
when /^tex\._bus\._org\._/
return TEXAS_STATUTES_ROOT + '/' + param_id.sub( '_bus._org._', "_bus._orgs._" )
when /^tex._code_of_crim._proc$/
return TEXAS_STATUTES_ROOT + "/tex._code_of_crim._proc."
when /^tex._code_of_crim._proc_/
return TEXAS_STATUTES_ROOT + '/' + param_id.sub( "_proc_", "_proc._" )
when /^tex\._govt\._/
return TEXAS_STATUTES_ROOT + '/' + param_id.sub( '_govt._', "_gov't._" )
when /^tex\._util\._/
return TEXAS_STATUTES_ROOT + '/' + param_id.sub( "_util._", "_utils._" )
when /_govt_/
return TEXAS_STATUTES_ROOT + '/' + param_id.sub( "_govt_", "_gov't_" )
when /^ors_/
return ORS_ROOT + "/#{param_id}"
describe 'fixes mis-matched URLs' do
it 'fixes nrs under colorado' do
actual = Publication.crs.create_perm_redirect('nrs_692c.390', request_uri: 'https://colorado.public.law/statutes/nrs_692c.390')
expect(actual).to eq('https://nevada.public.law/statutes/nrs_692c.390')
end
it 'fixes ca under colorado' do
actual = Publication.crs.create_perm_redirect('ca_penal_code_section_1203.4', request_uri: 'https://colorado.public.law/codes/ca_penal_code_section_1203.4')
expect(actual).to eq('https://california.public.law/codes/ca_penal_code_section_1203.4')
end
it 'fixes nrs under oregon' do
actual = Publication.ors.create_perm_redirect('nrs_692c.390', request_uri: 'https://oregon.public.law/statutes/nrs_692c.390')
expect(actual).to eq('https://nevada.public.law/statutes/nrs_692c.390')
end
it 'fixes crs under oregon' do
actual = Publication.ors.create_perm_redirect('crs_title_35_article_51', request_uri: 'https://oregon.public.law/statutes/crs_title_35_article_51')
expect(actual).to eq('https://colorado.public.law/statutes/crs_title_35_article_51')
end
it 'fixes ny laws under oregon' do
actual = Publication.ors.create_perm_redirect('n.y._general_municipal_law_section_891-e', request_uri: 'https://oregon.public.law/statutes/n.y._general_municipal_law_section_891-e')
expect(actual).to eq('https://newyork.public.law/laws/n.y._general_municipal_law_section_891-e')
end
it 'fixes tex under oregon' do
actual = Publication.ors.create_perm_redirect('tex._educ._code_section_21.105', request_uri: 'https://oregon.public.law/statutes/tex._educ._code_section_21.105')
expect(actual).to eq('https://texas.public.law/statutes/tex._educ._code_section_21.105')
end
it 'fixes ors under california' do
actual = Publication.california_codes.create_perm_redirect('ors_90.315', request_uri: 'https://california.public.law/codes/ors_90.315')
expect(actual).to eq('https://oregon.public.law/statutes/ors_90.315')
end
it 'fixes crs under california' do
actual = Publication.california_codes.create_perm_redirect('crs_title_35_article_51', request_uri: 'https://california.public.law/codes/crs_title_35_article_51')
expect(actual).to eq('https://colorado.public.law/statutes/crs_title_35_article_51')
end
end
end
describe '#create_temp_redirect' do
it 'redirects previous ORS editions' do
actual = ors.create_temp_redirect('14', request_uri: 'https://oregon.public.law/ors/2009/volume/14')
expect(actual).to eq('https://oregon.public.law/statutes/ors_volume_14')
end
context 'when is Texas Statutes' do
it 'redirects RECORD-NOT-FOUND to statutes' do
actual = texas_statutes.create_temp_redirect('RECORD-NOT-FOUND')
expect(actual).to eq('https://texas.public.law/statutes')
end
it 'redirects anything else to statutes root' do
actual = texas_statutes.create_temp_redirect("tex._gov't_code_section_806.103")
expect(actual).to eq('https://texas.public.law/statutes')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment