Class: String

Inherits:
Object show all
Defined in:
lib/serverside/core_ext.rb

Overview

String extensions.

Constant Summary collapse

LINE_RE =
/^([^\r]*)\r\n/n.freeze
EMPTY_STRING =
''.freeze

Instance Method Summary collapse

Instance Method Details

#/(o) ⇒ Object

Concatenates a path (purely sugar)



21
22
23
# File 'lib/serverside/core_ext.rb', line 21

def /(o)
  File.join(self, o.to_s)
end

#camelizeObject

Converts an underscored name into a camelized name



32
33
34
# File 'lib/serverside/core_ext.rb', line 32

def camelize
  gsub(/(^|_)(.)/) {$2.upcase}
end

#etagObject



59
60
61
# File 'lib/serverside/core_ext.rb', line 59

def etag
  MD5.hexdigest(self)
end

#get_lineObject



39
40
41
# File 'lib/serverside/core_ext.rb', line 39

def get_line
  sub!(LINE_RE, EMPTY_STRING) ? $1 : nil
end

#get_up_to_boundary(boundary) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/serverside/core_ext.rb', line 43

def get_up_to_boundary(boundary)
  if i = index(boundary)
    part = i > 0 ? self[0..(i - 1)] : ''
    slice!(0..(i + boundary.size - 1))
    part
  end
end

#get_up_to_boundary_with_crlf(boundary) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/serverside/core_ext.rb', line 51

def get_up_to_boundary_with_crlf(boundary)
  if i = index(boundary)
    part = i > 0 ? self[0..(i - 1)] : ''
    slice!(0..(i + boundary.size + 1))
    part
  end
end

#html_escapeObject



16
17
18
# File 'lib/serverside/core_ext.rb', line 16

def html_escape
  gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
end

#underscoreObject

Converts camel-cased phrases to underscored phrases.



26
27
28
29
# File 'lib/serverside/core_ext.rb', line 26

def underscore
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").downcase
end

#uri_escapeObject

Encodes a normal string to a URI string.



6
7
8
9
# File 'lib/serverside/core_ext.rb', line 6

def uri_escape
  gsub(/([^ a-zA-Z0-9_.-]+)/n) {'%'+$1.unpack('H2'*$1.size).
    join('%').upcase}.tr(' ', '+')
end

#uri_unescapeObject

Decodes a URI string to a normal string.



12
13
14
# File 'lib/serverside/core_ext.rb', line 12

def uri_unescape
  tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){[$1.delete('%')].pack('H*')}
end