Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/midwire_common/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random(count = 6, ranges = [('a'..'z'), ('A'..'Z'), ('0'..'9')]) ⇒ Object



3
4
5
6
# File 'lib/midwire_common/string.rb', line 3

def random(count = 6, ranges = [('a'..'z'), ('A'..'Z'), ('0'..'9')])
  o = ranges.map(&:to_a).flatten
  (0..(count - 1)).map { o[rand(o.length)] }.join
end

Instance Method Details

#alpha_numeric?Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/midwire_common/string.rb', line 56

def alpha_numeric?
  regex = /^[a-zA-Z0-9]+$/
  (self =~ regex) == 0 ? true : false
end

#email_address?Boolean

rubocop:disable Metrics/LineLength

Returns:

  • (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 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

  (self =~ email_regex) == 0 ? true : false
end

#escape_double_quotesObject



151
152
153
# File 'lib/midwire_common/string.rb', line 151

def escape_double_quotes
  gsub(/["]/, '\\\\\"')
end

#escape_single_quotesObject



147
148
149
# File 'lib/midwire_common/string.rb', line 147

def escape_single_quotes
  gsub(/[']/, '\\\\\'')
end

#format_phoneObject



119
120
121
122
# File 'lib/midwire_common/string.rb', line 119

def format_phone
  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



49
50
51
52
53
54
# File 'lib/midwire_common/string.rb', line 49

def here_with_pipe(delimeter = ' ')
  lines = split("\n")
  lines.map! { |c| c.sub!(/\s*\|/, '') }
  new_string = lines.join(delimeter)
  replace(new_string)
end

#left(count) ⇒ Object



9
10
11
# File 'lib/midwire_common/string.rb', line 9

def left(count)
  slice(0, count)
end

#left_trimObject



17
18
19
20
# File 'lib/midwire_common/string.rb', line 17

def left_trim
  # remove leading whitespace
  gsub(/^[\t\s]+/, '')
end

#left_trim!Object



22
23
24
# File 'lib/midwire_common/string.rb', line 22

def left_trim!
  self.gsub!(/^[\t\s]+/, '') || ''
end

#numeric?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
# File 'lib/midwire_common/string.rb', line 111

def numeric?
  Float(self)
rescue
  false # not numeric
else
  true # numeric
end

#right(count) ⇒ Object



13
14
15
# File 'lib/midwire_common/string.rb', line 13

def right(count)
  slice(-count, count)
end

#right_trimObject



26
27
28
29
# File 'lib/midwire_common/string.rb', line 26

def right_trim
  # remove trailing whitespace
  gsub(/[\t\s]+$/, '')
end

#right_trim!Object



31
32
33
# File 'lib/midwire_common/string.rb', line 31

def right_trim!
  self.gsub!(/[\t\s]+$/, '') || ''
end

#sanitizeObject



124
125
126
# File 'lib/midwire_common/string.rb', line 124

def sanitize
  gsub(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '')
end

#sanitize!Object



128
129
130
# File 'lib/midwire_common/string.rb', line 128

def sanitize!
  self.gsub!(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '')
end

#shorten(maxcount = 30) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/midwire_common/string.rb', line 132

def shorten(maxcount = 30)
  if 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

#trimObject



35
36
37
38
# File 'lib/midwire_common/string.rb', line 35

def trim
  # remove leading and trailing whitespace
  left_trim.right_trim
end

#trim!Object



40
41
42
43
# File 'lib/midwire_common/string.rb', line 40

def trim!
  # remove leading and trailing whitespace
  self.left_trim!.right_trim! || ''
end

#zipcode?Boolean

rubocop:enable Metrics/LineLength

Returns:

  • (Boolean)


107
108
109
# File 'lib/midwire_common/string.rb', line 107

def zipcode?
  self =~ %r{^(\d{5})(-\d{4})?$}x ? true : false
end