Class: WWMD::URLParse

Inherits:
Object
  • Object
show all
Defined in:
lib/wwmd/urlparse.rb

Overview

yay for experiments in re-inventing the wheel

Constant Summary collapse

HANDLERS =
[:https,:http,:ftp,:file]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ URLParse

Returns a new instance of URLParse.



10
11
12
# File 'lib/wwmd/urlparse.rb', line 10

def initialize(*args)
  # nothing to see here, move along
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def base_url
  @base_url
end

#fqpathObject (readonly)

Returns the value of attribute fqpath.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def fqpath
  @fqpath
end

#locationObject (readonly)

Returns the value of attribute location.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def location
  @location
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def path
  @path
end

#protoObject (readonly)

Returns the value of attribute proto.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def proto
  @proto
end

#rpathObject (readonly)

Returns the value of attribute rpath.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def rpath
  @rpath
end

#scriptObject (readonly)

Returns the value of attribute script.



8
9
10
# File 'lib/wwmd/urlparse.rb', line 8

def script
  @script
end

Instance Method Details

#has_proto?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
# File 'lib/wwmd/urlparse.rb', line 97

def has_proto?
  begin
    return true if HANDLERS.include?(@actual.split(":").first.downcase.to_sym)
  rescue
    return false
  end
end

#make_me_pathObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wwmd/urlparse.rb', line 69

def make_me_path
  @proto,tpath = @base.split(":",2)
  tpath ||= ""
  @params = tpath.clop
  tpath = tpath.clip
  if @actual.empty?
    a_path = tpath.split("/").reject { |x| x.empty? }
  else
    a_path = tpath.dirname.split("/").reject { |x| x.empty? }
  end
  @location = a_path.shift
  if @actual.clop
    @params = @actual.clop
    @actual = @actual.clip
  end
  a_path = [] if (@actual =~ (/^\//))
  b_path = @actual.split("/").reject { |x| x.empty? }
  a_path.pop if (a_path[-1] =~ /^\?/).kind_of?(Fixnum) && !b_path.empty?
  c_path = (a_path + @actual.split("/").reject { |x| x.empty? }).flatten
  d_path = []
  c_path.each do |x|
    (d_path.pop;next) if x == ".."
    next if (x == "." || x =~ /^\?/)
    d_path << x
  end
  return d_path
end

#parse(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wwmd/urlparse.rb', line 14

def parse(*args)
  if args.size == 1
    base = ""
    actual = args.shift.to_s.strip
  else
    base = args.shift.to_s.strip
    actual = args.shift.to_s.strip
  end
  if actual.has_proto?
    url = actual
  else
    url = base        
    url += "/" unless (base =~ /\/\z/ || actual =~ /\A\// || actual.empty?)
    url += actual
  end
  return URI.parse(url).to_s

#### yeah... screw this
  @proto = @location = @path = @script = @rpath = nil
  @base = base.to_s
  @actual = actual
  if self.has_proto?
    @base = @actual
    @actual = ""
  end
# does this work for http://location/?  probably not
  @base += "/" if (!@base.has_ext? || @base.split("/").size == 3)
  @rpath = make_me_path.join("/")
  @rpath += "?#{@params}" if @params
  @path = "/" + @rpath
  if @rpath.has_ext?
    @path = "/" + @rpath.dirname
    @script = @rpath.basename.clip
  end
  @script = "" if @script.nil?
  begin
    @base_url = @proto + "://" + @location
  rescue => e
    return false
    raise e
    dbg = WWMD.debug
    WWMD.debug = true
    putd "ERROR in urlparse TRACE:"
    pp *args
    putd @proto
    putd @base
    putd @actual
    putd @location
    WWMD.debug = dbg
    raise e
  end
  @fqpath = @path + @script
  self
end

#to_sObject



105
106
107
# File 'lib/wwmd/urlparse.rb', line 105

def to_s
  return "#{@proto}://#{@location}/#{rpath}"
end