Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra-hat.rb,
lib/gettext/haml_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random_password(length = 9, strength = 0) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sinatra-hat.rb', line 94

def self.random_password(length=9, strength=0)
  vowels      = 'aeuy'
  consonants  = 'bdghjmnpqrstvz'
  consonants += 'BDGHJLMNPQRSTVWXZ' if strength & 1 != 0
  vowels     += 'AEUY'              if strength & 2 != 0
  consonants += '23456789'          if strength & 4 != 0
  consonants += '@#$%'              if strength & 8 != 0
  password = '';
  alt = rand(2)
  length.times do
    password += consonants[rand(consonants.size - 1)].chr if alt != 0
    password += vowels[rand(vowels.size - 1)].chr         if alt == 0
    alt = 1 - alt
  end
  password
end

Instance Method Details

#cdataObject



58
59
60
# File 'lib/sinatra-hat.rb', line 58

def cdata
  "<![CDATA[#{self.gsub(/\]\]>/,']]]]><![CDATA[>')}]]>"
end

#comma_splitObject



110
111
112
# File 'lib/sinatra-hat.rb', line 110

def comma_split
  CSV.parse_line(gsub(/"\s+,/,'",').gsub(/,\s+"/,',"')).collect{|t| t.strip unless t.nil?}.delete_if{|t| t.nil? || t.empty?}
end

#escape_single_quotesObject



9
10
11
# File 'lib/gettext/haml_parser.rb', line 9

def escape_single_quotes
  self.gsub(/'/, "\\\\'")
end

#excerpt(chars = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/sinatra-hat.rb', line 77

def excerpt(chars=nil)
  first = split(/(?:\n\r?){2,}/)[0] || ""
  return first if chars.nil?
  words = first.split(' ')
  pos, count = words.inject([0, 0]) do |c, v|
    c[1] + v.length < chars ? [c[0] + 1, c[1] + v.length] : c
  end
  words[0..pos].join(' ') + ((pos == words.size) ? '' : '…')
end

#mail_utf8_subjectObject



54
55
56
57
# File 'lib/sinatra-hat.rb', line 54

def mail_utf8_subject
  # 4.2 http://tools.ietf.org/html/rfc2047
  '=?UTF-8?Q?'+(self.chomp.gsub(/[^ !-<>-^`-~]/){|m| m.unpack('C*').map{|c| '=%02X' % c}.join}.gsub(/\s/, '_'))+'?='
end

#md5Object



61
62
63
64
65
# File 'lib/sinatra-hat.rb', line 61

def md5
  hash = Digest::MD5.new
  hash << self
  hash.hexdigest
end

#pad(other, extra = 0) ⇒ Object



89
90
91
92
93
# File 'lib/sinatra-hat.rb', line 89

def pad(other, extra=0)
  padding = other.utf8_length - utf8_length
  padding = 0 if padding < 0
  ' ' * (padding+extra) + self
end

#pwdhash(salt = nil) ⇒ Object

This is Pligg style password hash



72
73
74
75
76
# File 'lib/sinatra-hat.rb', line 72

def pwdhash(salt=nil)
  salt = String.random_password.md5 if salt.nil?
  salt = salt[0..8]
  salt+(salt+self).sha1
end

#sha1Object



66
67
68
69
70
# File 'lib/sinatra-hat.rb', line 66

def sha1
  hash = Digest::SHA1.new
  hash << self
  hash.hexdigest
end


114
115
116
# File 'lib/sinatra-hat.rb', line 114

def to_permalink
  Iconv.iconv('ascii//translit//IGNORE', 'utf-8', self).first.gsub("'", "").gsub(/[^\x00-\x7F]+/, '').gsub(/[^a-zA-Z0-9-]+/, '-').gsub(/--+/, '-').gsub(/^-/, '').gsub(/-$/, '').downcase
end

#utf8_lengthObject



86
87
88
# File 'lib/sinatra-hat.rb', line 86

def utf8_length
  unpack('U*').length
end