Skip to content

Instantly share code, notes, and snippets.

@rob-mcgrail
Created April 16, 2012 05:11
Show Gist options
  • Save rob-mcgrail/2396423 to your computer and use it in GitHub Desktop.
Save rob-mcgrail/2396423 to your computer and use it in GitHub Desktop.
Url conversion heroku app to help a content manager migration
require 'sinatra'
require 'haml'
class String
def proper
self.split(/\s+/).each{ |word| word.capitalize! }.join(' ')
end
end
helpers do
def parse(url)
url.gsub!(/\/$/, '')
url.gsub!('www.', '')
url.gsub!('legacy.', '')
ncea = {}
if /alignment-standards\.php/i.match(url)
ncea[:alignment_landing_page] = true
ncea
elsif /http:\/\/tki.org.nz\/e\/community\/ncea\/\w+/i.match(url)
match = /community\/ncea\/((\w+-\w+-\w+|\w+-\w+|\w+))+/i.match(url)
if match
ncea[:subject] = match[1].proper
ncea[:subject] = map_names(ncea[:subject])
end
ncea[:aligned] = true unless /community\/ncea\/\\w+-\w+|\w+(-int)/i.match(url)
match = /-lvl(\d)/i.match(url)
ncea[:level] = match[1] if match
if ncea[:subject] == nil
nil
else
ncea
end
elsif /http:\/\/tki.org.nz\/e\/community\/ncea$/i.match(url)
ncea[:root] = true
ncea
else
nil
end
end
def map_names(name)
name = name.gsub(/-lvl\d/, '').gsub('.php', '').gsub('-int', '')
map = {
'Health' => 'Health-education',
'Agriculture' => 'Agriculture-and-horticultural-science',
'Space-science' => 'Earth-and-space-science',
'Space-sci' => 'Earth-and-space-science',
'Mathematics' => 'Mathematics-and-statistics',
'Maths' => 'Mathematics-and-statistics',
'Media' => 'Media-studies',
'Home-ec' => 'Home-economics',
'Physed' => 'Physical-education',
'Graphics' => 'DVC',
'Socstud' => 'Social-studies',
'Te-reo' => 'Te-reo-maori',
'Tech' => 'Technology',
}
map.each do |k,v|
if name == k
name = v
end
end
name
end
def format(ncea)
status_prefix = {
nil => '/Resources-for-non-aligned-standards',
true => '/Resources-for-aligned-standards',
}
category = {
'Art-history' => '/The-arts',
'Dance' => '/The-arts',
'Drama' => '/The-arts',
'Music' => '/The-arts',
'Visual-arts' => '/The-arts',
'Chinese' => '/Learning-languages',
'Cook-islands-maori' => '/Learning-languages',
'French' => '/Learning-languages',
'German' => '/Learning-languages',
'Japanese' => '/Learning-languages',
'Latin' => '/Learning-languages',
'Samoan' => '/Learning-languages',
'Spanish' => '/Learning-languages',
'English' => '',
'Te-reo-maori' => '',
'Te-reo-rangatira' => '',
'Health-education' => '/Health-and-physical-education',
'Home-economics' => '/Health-and-physical-education',
'Home-ec' => '/Health-and-physical-education',
'Physical-education' => '/Health-and-physical-education',
'Mathematics-and-statistics' => '',
'Agriculture-and-horticultural-science' => '/Science',
'Biology' => '/Science',
'Chemistry' => '/Science',
'Earth-and-space-science' => '/Science',
'Physics' => '/Science',
'Science' => '/Science',
'Accounting' => '/Social-sciences',
'Business-studies' => '/Social-sciences',
'Classical-studies' => '/Social-sciences',
'Economics' => '/Social-sciences',
'Education-for-sustainability' => '/Social-sciences',
'Geography' => '/Social-sciences',
'History' => '/Social-sciences',
'Media-studies' => '/Social-sciences',
'Social-studies' => '/Social-sciences',
'Technology' => '',
'DVC' => 'Technology',
}
url = "http://ncea.tki.org.nz"
if ncea[:root]
url
elsif ncea[:alignment_landing_page]
url << '/Resources-for-aligned-standards'
url
else
url << status_prefix[ncea[:aligned]]
url << category[ncea[:subject]]
url << "/#{ncea[:subject]}"
url << "/Level-#{ncea[:level]}-#{ncea[:subject]}" if ncea[:level]
url
end
end
end
get '/' do
haml :index
end
get '/url/?' do
ncea = parse params[:url]
if ncea == nil
@url = "Unable to parse url"
else
@url = format ncea
end
haml :index
end
__END__
@@layout
%html
%head
%link(rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css")
:css
body {padding:30px; padding-left:60px;}
input {height:27px;margin-bottom:0px;}
h1,h2,h3 {padding-bottom:20px;}
.response {font-size:150%;}
%body
.row
= yield
@@index
%h1
Enter a legacy NCEA url:
%form(id='url-form' method='get' action='/url' class='well form-vertical')
%input(name='url' type='text' required='required' class='input-xlarge span6')
%button(type='submit' class='btn')
Submit
- if @url
= haml :response
@@response
%p.response
%strong= @url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment