Module: Olelo::Util

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



37
38
39
# File 'lib/olelo/util.rb', line 37

def self.included(base)
  base.extend(Util)
end

Instance Method Details

#check {|errors| ... } ⇒ Object

Yields:

  • (errors)

Raises:



47
48
49
50
51
# File 'lib/olelo/util.rb', line 47

def check
  errors = []
  yield(errors)
  raise MultiError, errors if !errors.empty?
end

#decode64(s) ⇒ String

Decode base64 encoded string

Parameters:

  • Base64 (String)

    encoded string

Returns:



142
143
144
# File 'lib/olelo/util.rb', line 142

def decode64(s)
  s.unpack('m').first
end

#deep_copy(object) ⇒ Object

Creates deep copy of object by abusing ‘Marshal` This method is slow and not adequate for huge objects. It can only copy objects which are serializable.

Parameters:

  • Serializable (Object)

    object

Returns:

  • (Object)

    Deep copy of object



134
135
136
# File 'lib/olelo/util.rb', line 134

def deep_copy(object)
  Marshal.load(Marshal.dump(object))
end

#encode64(s) ⇒ String

Encode string as base64

Parameters:

Returns:

  • (String)

    Base64 encoded string



150
151
152
# File 'lib/olelo/util.rb', line 150

def encode64(s)
  [s].pack('m').gsub(/\n/, '')
end

#escape(s) ⇒ Object

Like CGI.escape but escapes space not as +



54
55
56
57
58
59
# File 'lib/olelo/util.rb', line 54

def escape(s)
  s = s.to_s
  s.gsub(/([^a-zA-Z0-9_.-]+)/) do
    '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
  end
end

#escape_html(s) ⇒ String

Escape html entities in string

Parameters:

Returns:



81
82
83
# File 'lib/olelo/util.rb', line 81

def escape_html(s)
  CGI.escapeHTML(s.to_s).html_safe
end

#escape_javascript(s) ⇒ String

Escape javascript code for embedding in html

Parameters:

Returns:



102
103
104
# File 'lib/olelo/util.rb', line 102

def escape_javascript(s)
  s.to_s.gsub(/[&><]/) { |x| JAVASCRIPT_ESCAPE[x] }
end

#md5(s) ⇒ String

Compute md5 hash of string

Parameters:

Returns:

  • (String)

    md5 hash of string



111
112
113
114
115
# File 'lib/olelo/util.rb', line 111

def md5(s)
  s = Digest::MD5.hexdigest(s)
  s.force_encoding(Encoding::ASCII)
  s
end

#no_cache?(env = @env) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/olelo/util.rb', line 43

def no_cache?(env = @env)
  env['HTTP_PRAGMA'] == 'no-cache' || env['HTTP_CACHE_CONTROL'].to_s.include?('no-cache')
end

#sha256(s) ⇒ String

Compute sha256 hash of string

Parameters:

Returns:

  • (String)

    sha256 hash of string



122
123
124
125
126
# File 'lib/olelo/util.rb', line 122

def sha256(s)
  s = Digest::SHA256.hexdigest(s)
  s.force_encoding(Encoding::ASCII)
  s
end

#titlecase(s) ⇒ String

Capitalizes all the words to create a nicer looking title

Parameters:

  • lowercase_string_with_underscore (String)

Returns:

  • (String)

    Lowercase String With Underscore



173
174
175
# File 'lib/olelo/util.rb', line 173

def titlecase(s)
  s.to_s.tr('_', ' ').split(/\s+/).map(&:capitalize).join(' ')
end

#truncate(s, max, omission = '...') ⇒ String

Truncate string if it is too long and add omission

Parameters:

  • String (String)

    to truncate

  • Maximum (Integer)

    length

  • Omission (String)

    string, e.g. ellipsis

Returns:

  • (String)

    Truncated string



160
161
162
163
164
165
166
167
# File 'lib/olelo/util.rb', line 160

def truncate(s, max, omission = '...')
  s = s.to_s
  if s.length > max
    s[0...max] + omission
  else
    s
  end
end

#unescape(s) ⇒ Object

Like CGI.unescape but does not unescape +



62
63
64
65
66
67
68
# File 'lib/olelo/util.rb', line 62

def unescape(s)
  s = s.to_s
  enc = s.encoding
  s.gsub(/((?:%[0-9a-fA-F]{2})+)/) do
    [$1.delete('%')].pack('H*').force_encoding(enc)
  end
end

#unescape_backslash(s) ⇒ Object



70
71
72
73
74
75
# File 'lib/olelo/util.rb', line 70

def unescape_backslash(s)
  s = s.to_s
  enc = s.encoding
  s.gsub(/\\([0-7]{3})/) { $1.to_i(8).chr.force_encoding(enc) }.
    gsub(/\\x([\da-f]{2})/i) { $1.to_i(16).chr.force_encoding(enc) }
end

#unescape_html(s) ⇒ String

Unescape html entities in string

Parameters:

  • s (String)

    String with escaped html entities

Returns:

  • (String)

    Unescaped string



89
90
91
# File 'lib/olelo/util.rb', line 89

def unescape_html(s)
  CGI.unescapeHTML(s.to_s)
end

#valid_xml_chars?(s) ⇒ Boolean

Check if string contains only characters which are valid in XML

Parameters:

Returns:

  • (Boolean)

See Also:



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/olelo/util.rb', line 193

def valid_xml_chars?(s)
  s = s.to_s
  if s.encoding == Encoding::UTF_8
    return false if !s.valid_encoding?
  else
    s = s.dup if s.frozen?
    return false if s.try_encoding(Encoding::UTF_8).encoding != Encoding::UTF_8
  end
  s.codepoints do |n|
    return false if !VALID_XML_CHARS.any? {|v| v === n }
  end
  true
end