Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment