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