Skip to content

Instantly share code, notes, and snippets.

@devyn
Created December 23, 2008 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devyn/39502 to your computer and use it in GitHub Desktop.
Save devyn/39502 to your computer and use it in GitHub Desktop.
require 'uri'
module CrumbleURL; extend self
def crumble_to_url c
c =~ /^([A-Za-z0-9_-]+)\((\d+)\) ([A-Za-z0-9_.-]+)/
uri = URI::Generic.build(:scheme => $1, :port => $2, :host => $3.split('.').reverse.join('.'))
if c =~ /> ([A-Za-z0-9()_.-^]+)/
uri.path = "/" + $1.split('.').collect{|pt|pt.gsub(/\(\d+\)$/){|o|'/'+o[1..-2]}.gsub("^", ".")}.join("/")
end
if c =~ /\{(.*)\}$/
uri.query = $1.gsub(": ", "=").gsub(", ", "&")
end
uri.to_s
end
alias c2u crumble_to_url
def url_to_crumble u
uri = URI.parse(u)
pth = []
uri.path.split("/").each do |pt|
next if pt.empty?
if (pt.to_i.to_s == pt) and pth.last
pth.last << "(#{pt})"
else
pth << pt.gsub(".", "^")
end
end
"#{uri.scheme}(#{uri.port}) #{(uri.host.split(".").reverse - ['www']).join(".")}#{" > #{pth.join('.')}" unless pth.empty?}#{" {#{uri.query.gsub("&", ", ").gsub("=", ": ")}}" if uri.query}"
end
alias u2c url_to_crumble
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment