Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/midwire_common/string.rb
Class Method Summary collapse
Instance Method Summary collapse
- #escape_double_quotes ⇒ Object
- #escape_single_quotes ⇒ Object
- #format_phone ⇒ Object
-
#here_with_pipe(delimeter = ' ') ⇒ Object
html = <<-stop.here_with_pipe(delimeter=“n”) |<!– Begin: comment –> |<script type=“text/javascript”> stop.
- #is_alpha_numeric? ⇒ Boolean
- #is_email_address? ⇒ Boolean
- #is_numeric? ⇒ Boolean
- #is_zipcode? ⇒ Boolean
- #left(count) ⇒ Object
- #left_trim ⇒ Object
- #left_trim! ⇒ Object
- #right(count) ⇒ Object
- #right_trim ⇒ Object
- #right_trim! ⇒ Object
- #sanitize ⇒ Object
- #sanitize! ⇒ Object
- #shorten(maxcount = 30) ⇒ Object
- #trim ⇒ Object
- #trim! ⇒ Object
Class Method Details
.random(count = 6, ranges = [('a'..'z'),('A'..'Z'),('0'..'9')]) ⇒ Object
4 5 6 7 |
# File 'lib/midwire_common/string.rb', line 4 def random(count = 6, ranges = [('a'..'z'),('A'..'Z'),('0'..'9')]) o = ranges.map{|i| i.to_a}.flatten; string = (0..(count-1)).map{ o[rand(o.length)] }.join; end |
Instance Method Details
#escape_double_quotes ⇒ Object
152 153 154 |
# File 'lib/midwire_common/string.rb', line 152 def escape_double_quotes self.gsub(/["]/, '\\\\\"') end |
#escape_single_quotes ⇒ Object
148 149 150 |
# File 'lib/midwire_common/string.rb', line 148 def escape_single_quotes self.gsub(/[']/, '\\\\\'') end |
#format_phone ⇒ Object
120 121 122 123 |
# File 'lib/midwire_common/string.rb', line 120 def format_phone self.gsub!(/[a-z,! \-\(\)\:\;\.\&\$]+/i, '') '(' << slice(0..2) << ')' << slice(3..5) << '-' << slice(-4,4) end |
#here_with_pipe(delimeter = ' ') ⇒ Object
html = <<-stop.here_with_pipe(delimeter=“n”)
|<!-- Begin: comment -->
|<script type="text/javascript">
stop
50 51 52 53 54 55 |
# File 'lib/midwire_common/string.rb', line 50 def here_with_pipe(delimeter = ' ') lines = self.split("\n") lines.map! {|c| c.sub!(/\s*\|/, '')} new_string = lines.join(delimeter) self.replace(new_string) end |
#is_alpha_numeric? ⇒ Boolean
57 58 59 60 |
# File 'lib/midwire_common/string.rb', line 57 def is_alpha_numeric? regex = /^[a-zA-Z0-9]+$/ return (self =~ regex) == 0 ? true : false end |
#is_email_address? ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/midwire_common/string.rb', line 62 def is_email_address? # //Email address # //Use this version to seek out email addresses in random documents and texts. # //Does not match email addresses using an IP address instead of a domain name. # //Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum. # //Including these increases the risk of false positives when applying the regex to random documents. # '\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b' # # //Email address (anchored) # //Use this anchored version to check if a valid email address was entered. # //Does not match email addresses using an IP address instead of a domain name. # //Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum. # //Requires the "case insensitive" option to be ON. # '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$' # # //Email address (anchored; no consecutive dots) # //Use this anchored version to check if a valid email address was entered. # //Improves on the original email address regex by excluding addresses with consecutive dots such as [email protected] # //Does not match email addresses using an IP address instead of a domain name. # //Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum. # //Including these increases the risk of false positives when applying the regex to random documents. # '^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$' # # //Email address (no consecutive dots) # //Use this version to seek out email addresses in random documents and texts. # //Improves on the original email address regex by excluding addresses with consecutive dots such as [email protected] # //Does not match email addresses using an IP address instead of a domain name. # //Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum. # //Including these increases the risk of false positives when applying the regex to random documents. # '\b[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b' # # //Email address (specific TLDs) # //Does not match email addresses using an IP address instead of a domain name. # //Matches all country code top level domains, and specific common top level domains. # '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$' # # //Email address: Replace with HTML link # '\b(?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b' email_regex = %r{^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|jobs|museum|edu|pro)$}xi # Case insensitive return (self =~ email_regex) == 0 ? true : false end |
#is_numeric? ⇒ Boolean
110 111 112 113 114 115 116 117 118 |
# File 'lib/midwire_common/string.rb', line 110 def is_numeric? begin Float(self) rescue false # not numeric else true # numeric end end |
#is_zipcode? ⇒ Boolean
106 107 108 |
# File 'lib/midwire_common/string.rb', line 106 def is_zipcode? self =~ %r{^(\d{5})(-\d{4})?$}x ? true : false end |
#left(count) ⇒ Object
10 11 12 |
# File 'lib/midwire_common/string.rb', line 10 def left(count) self.slice(0,count) end |
#left_trim ⇒ Object
18 19 20 21 |
# File 'lib/midwire_common/string.rb', line 18 def left_trim # remove leading whitespace self.gsub(/^[\t\s]+/, '') end |
#left_trim! ⇒ Object
23 24 25 |
# File 'lib/midwire_common/string.rb', line 23 def left_trim! self.gsub!(/^[\t\s]+/, '') || '' end |
#right(count) ⇒ Object
14 15 16 |
# File 'lib/midwire_common/string.rb', line 14 def right(count) self.slice(-count,count) end |
#right_trim ⇒ Object
27 28 29 30 |
# File 'lib/midwire_common/string.rb', line 27 def right_trim # remove trailing whitespace self.gsub(/[\t\s]+$/, '') end |
#right_trim! ⇒ Object
32 33 34 |
# File 'lib/midwire_common/string.rb', line 32 def right_trim! self.gsub!(/[\t\s]+$/, '') || '' end |
#sanitize ⇒ Object
125 126 127 |
# File 'lib/midwire_common/string.rb', line 125 def sanitize self.gsub(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '') end |
#sanitize! ⇒ Object
129 130 131 |
# File 'lib/midwire_common/string.rb', line 129 def sanitize! self.gsub!(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '') end |
#shorten(maxcount = 30) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/midwire_common/string.rb', line 133 def shorten(maxcount = 30) if self.length >= maxcount shortened = self[0, maxcount] splitted = shortened.split(/\s/) if splitted.length > 1 words = splitted.length splitted[0, words-1].join(" ") + '...' else shortened[0, maxcount - 3] + '...' end else self end end |
#trim ⇒ Object
36 37 38 39 |
# File 'lib/midwire_common/string.rb', line 36 def trim # remove leading and trailing whitespace self.left_trim.right_trim end |
#trim! ⇒ Object
41 42 43 44 |
# File 'lib/midwire_common/string.rb', line 41 def trim! # remove leading and trailing whitespace self.left_trim!.right_trim! || '' end |