/lib/wwmd/urlparse.rb

http://github.com/miketracy/wwmd · Ruby · 64 lines · 56 code · 7 blank · 1 comment · 8 complexity · 3f0071f2f09335989f86461b0c673a14 MD5 · raw file

  1. require 'htmlentities'
  2. require 'wwmd/class_extensions'
  3. module WWMD
  4. class URLParse
  5. HANDLERS = [:https,:http,:ftp,:file]
  6. attr_reader :proto,:location,:path,:script,:rpath,:params,:base_url,:fqpath
  7. def initialize(*args)
  8. # nothing to see here, move along
  9. end
  10. def parse(*args)
  11. if args.size == 1
  12. base = ""
  13. actual = args.shift.to_s.strip
  14. else
  15. base = args.shift.to_s.strip
  16. actual = args.shift.to_s.strip
  17. end
  18. if actual.has_proto?
  19. url = actual
  20. else
  21. url = base
  22. url += "/" unless (base =~ /\/\z/ || actual =~ /\A\// || actual.empty?)
  23. url += actual
  24. end
  25. begin
  26. return URI.parse(url).to_s
  27. rescue => e
  28. STDERR.puts "WARN: #{e}"
  29. return nil
  30. end
  31. end
  32. def has_proto?
  33. begin
  34. return true if HANDLERS.include?(@actual.split(":").first.downcase.to_sym)
  35. rescue
  36. return false
  37. end
  38. end
  39. def to_s
  40. return "#{@proto}://#{@location}/#{rpath}"
  41. end
  42. end
  43. end
  44. class String
  45. HANDLERS = [:https,:http,:ftp,:file]
  46. def has_proto?
  47. begin
  48. return true if HANDLERS.include?(self.split(":").first.downcase.to_sym)
  49. rescue
  50. return false
  51. end
  52. end
  53. def has_ext? #:nodoc:
  54. return false if self.basename.split(".",2)[1].empty?
  55. return true
  56. end
  57. end