Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. require 'uri'
  2. module CrumbleURL; extend self
  3. def crumble_to_url c
  4. c =~ /^([A-Za-z0-9_-]+)\((\d+)\) ([A-Za-z0-9_.-]+)/
  5. uri = URI::Generic.build(:scheme => $1, :port => $2, :host => $3.split('.').reverse.join('.'))
  6. if c =~ /> ([A-Za-z0-9()_.-^]+)/
  7. uri.path = "/" + $1.split('.').collect{|pt|pt.gsub(/\(\d+\)$/){|o|'/'+o[1..-2]}.gsub("^", ".")}.join("/")
  8. end
  9. if c =~ /\{(.*)\}$/
  10. uri.query = $1.gsub(": ", "=").gsub(", ", "&")
  11. end
  12. uri.to_s
  13. end
  14. alias c2u crumble_to_url
  15. def url_to_crumble u
  16. uri = URI.parse(u)
  17. pth = []
  18. uri.path.split("/").each do |pt|
  19. next if pt.empty?
  20. if (pt.to_i.to_s == pt) and pth.last
  21. pth.last << "(#{pt})"
  22. else
  23. pth << pt.gsub(".", "^")
  24. end
  25. end
  26. "#{uri.scheme}(#{uri.port}) #{(uri.host.split(".").reverse - ['www']).join(".")}#{" > #{pth.join('.')}" unless pth.empty?}#{" {#{uri.query.gsub("&", ", ").gsub("=", ": ")}}" if uri.query}"
  27. end
  28. alias u2c url_to_crumble
  29. end
Add Comment
Please, Sign In to add comment