Module: TinyCode::ClassMethods

Defined in:
lib/tiny_code.rb

Constant Summary collapse

RAND_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvwxyz"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_normalized_string(text) ⇒ Object



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
# File 'lib/tiny_code.rb', line 19

def self.to_normalized_string(text)
  words_to_omit = ["a", "to", "the", "of", "has", "have", "it", "is", "in", "on", "or", "but", "when", "be"]

  # strip all the html tags from the text data
  col_text = text.gsub(/(<[^>]*>)|\n|\t/s, ' ')

  # Removing capitalization
  col_text.downcase!
  # Removing potential problem characters
  col_text.gsub!(/\"|\'/, '')
  # Removing text inside parens
  col_text.gsub!(/\(.*?\)/,'')

  # Removing all other non-word characters
  col_text.gsub!(/\W/, ' ')

  column_words = []
  words = col_text.split(' ')
  words.each do |word|
    if !words_to_omit.include?(word)
      column_words << word
    end
  end

  #reducing the word list to limit column length
  if column_words.length > 5
    column_words.slice!(0, column_words.length - 5)
  end

  #re-assemble the string
  column_words.join("_")
end

Instance Method Details

#make_tiny_codeObject



7
8
9
# File 'lib/tiny_code.rb', line 7

def make_tiny_code
  random_string(10)
end

#random_string(len) ⇒ Object



11
12
13
14
15
16
# File 'lib/tiny_code.rb', line 11

def random_string(len)
  rand_max = RAND_CHARS.size
  ret = "" 
  len.times{ ret << RAND_CHARS[rand(rand_max)] }
  ret 
end