Class: Surveyor::Common
- Inherits:
-
Object
- Object
- Surveyor::Common
- Defined in:
- lib/surveyor/common.rb
Constant Summary collapse
- OPERATORS =
%w(== != < > <= >= =~)
Class Method Summary collapse
-
.csv_impl ⇒ Class
Loads and uses either ‘FasterCSV` (for Ruby 1.8) or the stdlib `CSV` (for Ruby 1.9+).
- .generate_api_id ⇒ Object
- .to_normalized_string(text) ⇒ Object (also: normalize)
Instance Method Summary collapse
Class Method Details
.csv_impl ⇒ Class
Loads and uses either ‘FasterCSV` (for Ruby 1.8) or the stdlib `CSV` (for Ruby 1.9+).
48 49 50 51 52 53 54 55 56 |
# File 'lib/surveyor/common.rb', line 48 def csv_impl @csv_impl ||= if RUBY_VERSION < '1.9' require 'fastercsv' FasterCSV else require 'csv' CSV end end |
.generate_api_id ⇒ Object
37 38 39 |
# File 'lib/surveyor/common.rb', line 37 def generate_api_id UUIDTools::UUID.random_create.to_s end |
.to_normalized_string(text) ⇒ Object Also known as: normalize
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/surveyor/common.rb', line 24 def to_normalized_string(text) words_to_omit = %w(a be but has have in is it of on or the to when) col_text = text.to_s.gsub(/(<[^>]*>)|\n|\t/su, ' ') # Remove html tags col_text.downcase! # Remove capitalization col_text.gsub!(/\"|\'/u, '') # Remove potential problem characters col_text.gsub!(/\(.*?\)/u,'') # Remove text inside parens col_text.gsub!(/\W/u, ' ') # Remove all other non-word characters cols = (col_text.split(' ') - words_to_omit) (cols.size > 5 ? cols[-5..-1] : cols).join("_") end |
Instance Method Details
#make_tiny_code ⇒ Object
10 11 12 13 14 |
# File 'lib/surveyor/common.rb', line 10 def make_tiny_code # 7 random bytes is increased to ~10 characters (sans padding) by # base64 encoding SecureRandom.urlsafe_base64(7) end |