Class: URI::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/rackful/uri.rb

Overview

Extension and monkeypatch of Ruby’s StdLib URI::Generic class.

Instance Method Summary (collapse)

Instance Method Details

- (URI::Generic) normalize

Returns a normalized copy of self.

Returns:



20
21
22
23
24
# File 'lib/rackful/uri.rb', line 20

def normalize
  r = self.dup
  r.normalize!
  r
end

- (self?) normalize!

Monkeypatch of Ruby’s StdLib implementation. In addition to the default implementation, this implementation ensures that

  1. no unreserved characters are pct-encoded, and

  2. all non-unreserved characters are pct-encoded.

Check RFC3986 syntax:

abempty = *( "/" *(unreserved / pct-encoded / sub-delims / ":" / "@") )
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

Returns:

  • (self, nil)

    self if the URI was modified, or nil of the uri was already in normal form.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rackful/uri.rb', line 39

def normalize!
  #unless %r{\A(?:/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*\z}i === self.path
  #  raise TypeError, "Can’t convert String #{self.path.inspect} to Rackful::Path"
  #end
  self.uri_generic_normalize!
  path = '/' + self.segments( Encoding::BINARY ).collect do |segment|
    segment.gsub(/([^a-zA-Z0-9\-._~]+)/n) {
      '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
    }
  end.join('/')
  if path == self.path
    nil
  else
    self.path = path
    self
  end
end

- (String) relative_deprecated(base_path)

Deprecated.

Turns a relative URI (starting with /) into a relative path (starting with ./ or ../)

Parameters:

  • base_path (Path)

Returns:

  • (String)

    a relative URI



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rackful/uri.rb', line 113

def relative_deprecated base_path
  case self
  when base_path
    # RFC2396, Section 4.2
    return ''
  when %r{(?:\A|/)\.\.?(?:/|\z)}
    # self has abnormal absolute path,
    # like "/./", "/../", "/x/../", ...
    return self.dup
  end

  src_path = base_path.scan(%r{(?:\A|[^/]+)/})
  dst_path = self.scan(%r{(?:\A|[^/]+)/?})

  # discard same parts
  while !dst_path.empty? && dst_path.first == src_path.first
    src_path.shift
    dst_path.shift
  end

  tmp = dst_path.join

  # calculate
  if src_path.empty?
    if tmp.empty?
      return './'
    elsif dst_path.first.include?(':') # (see RFC2396 Section 5)
      return './' + tmp
    else
      return tmp
    end
  end

  return '../' * src_path.size + tmp
end

- (Array<String>) segments(encoding = Encoding::UTF_8)

Returns Unencoded segments

Returns:

  • (Array<String>)

    Unencoded segments



100
101
102
103
104
105
106
# File 'lib/rackful/uri.rb', line 100

def segments( encoding = Encoding::UTF_8 )
  r = self.path.split(%r{/+}).collect do |s|
    Rack::Utils.unescape( s, encoding )
  end
  r.shift
  r
end

- (Path) slashify

Returns a copy of self, with a trailing slash.

Returns:

  • (Path)

    a copy of self, with a trailing slash.



59
60
61
62
63
# File 'lib/rackful/uri.rb', line 59

def slashify
  r = self.dup
  r.slashify!
  r
end

- (self) slashify!

Adds a trailing slash to self if necessary.

Returns:

  • (self)


68
69
70
71
72
73
74
75
# File 'lib/rackful/uri.rb', line 68

def slashify!
  if '/' != self.path[-1,1]
    self.path += '/'
    self
  else
    nil
  end
end

- (Path) unslashify

Returns a copy of self without trailing slashes.

Returns:

  • (Path)

    a copy of self without trailing slashes.



79
80
81
82
83
# File 'lib/rackful/uri.rb', line 79

def unslashify
  r = self.dup
  r.unslashify!
  r
end

- (self) unslashify!

Removes trailing slashes from self.

Returns:

  • (self)


88
89
90
91
92
93
94
95
96
# File 'lib/rackful/uri.rb', line 88

def unslashify!
  path = self.path.sub( %r{/+\z}, '' )
  if path == self.path
    nil
  else
    self.path = path
    self
  end
end

- (void) uri_generic_normalize

Copy of the original StdLib URI::Generic::normalize! method.



15
# File 'lib/rackful/uri.rb', line 15

alias_method :uri_generic_normalize, :normalize

- (void) uri_generic_normalize!

Copy of the original StdLib URI::Generic::normalize! method.



8
# File 'lib/rackful/uri.rb', line 8

alias_method :uri_generic_normalize!, :normalize!