Class: String

Inherits:
Object
  • Object
show all
Includes:
Boris
Defined in:
lib/boris/core_ext/string.rb

Constant Summary

Constants included from Boris

Boris::CODE_LOOKUPS, Boris::CONN_FAILURE_AUTH_FAILED, Boris::CONN_FAILURE_CONNECTION_CLOSED, Boris::CONN_FAILURE_HOST_KEY_MISMATCH, Boris::CONN_FAILURE_LOCAL_CREDENTIALS, Boris::CONN_FAILURE_NO_HOST, Boris::CONN_FAILURE_PASSWORD_EXPIRED, Boris::CONN_FAILURE_REFUSED, Boris::CONN_FAILURE_RPC_FILTERED, Boris::CONN_FAILURE_RPC_UNAVAILABLE, Boris::PORT_DEFAULTS, Boris::VALID_CONNECTION_TYPES, Boris::VENDOR_ADOBE, Boris::VENDOR_AMD, Boris::VENDOR_APC, Boris::VENDOR_BROCADE, Boris::VENDOR_CISCO, Boris::VENDOR_CITRIX, Boris::VENDOR_DELL, Boris::VENDOR_EMULEX, Boris::VENDOR_F5, Boris::VENDOR_HP, Boris::VENDOR_IBM, Boris::VENDOR_INTEL, Boris::VENDOR_MICROSOFT, Boris::VENDOR_ORACLE, Boris::VENDOR_QLOGIC, Boris::VENDOR_REDHAT, Boris::VENDOR_SUSE, Boris::VENDOR_VMWARE, Boris::VERSION

Instance Method Summary collapse

Methods included from Boris

available_profilers, log_level=, logger

Instance Method Details

#after_colonNil, String

Returns the string value found after the last colon symbol from self.

'A:B:C'.after_colon  #=> "C"

Returns:

  • (Nil, String)

    string if value found, else returns nil



11
12
13
# File 'lib/boris/core_ext/string.rb', line 11

def after_colon
  value_after_character(':')
end

#after_periodNil, String

Returns the string value found after the last period symbol from self.

'A.B.C'.after_period  #=> "C"

Returns:

  • (Nil, String)

    string if value found, else returns nil



20
21
22
# File 'lib/boris/core_ext/string.rb', line 20

def after_period
  value_after_character('\.')
end

#after_pipeNil, String

Returns the string value found after the last pipe symbol from self.

'A|B|C'.after_pipe  #=> "C"

Returns:

  • (Nil, String)

    string if value found, else returns nil



29
30
31
# File 'lib/boris/core_ext/string.rb', line 29

def after_pipe
  value_after_character('|')
end

#after_slashNil, String

Returns the string value found after the last slash (both forwards and backwards) symbol from self.

'A/B/C'.after_slash  #=> "C"
'A\B\C'.after_slash  #=> "C"

Returns:

  • (Nil, String)

    string if value found, else returns nil



40
41
42
# File 'lib/boris/core_ext/string.rb', line 40

def after_slash
  value_after_character('\\\\|\/')
end

#before_colonNil, String

Returns the string value found before the first colon symbol from self.

'A:B:C'.before_colon  #=> "A"

Returns:

  • (Nil, String)

    string if value found, else returns nil



49
50
51
# File 'lib/boris/core_ext/string.rb', line 49

def before_colon
  value_before_character(':')
end

#before_periodNil, String

Returns the string value found before the first period symbol from self.

'A.B.C'.before_period  #=> "A"

Returns:

  • (Nil, String)

    string if value found, else returns nil



58
59
60
# File 'lib/boris/core_ext/string.rb', line 58

def before_period
  value_before_character('\.')
end

#before_pipeNil, String

Returns the string value found before the first pipe symbol from self.

'A.B.C'.before_period  #=> "A"

Returns:

  • (Nil, String)

    string if value found, else returns nil



67
68
69
# File 'lib/boris/core_ext/string.rb', line 67

def before_pipe
  value_before_character('|')
end

#before_slashNil, String

Returns the string value found before the first slash (both forwards and backwards) symbol from self.

'A/B/C'.before_slash  #=> "A"
'A\B\C'.before_slash  #=> "A"

Returns:

  • (Nil, String)

    string if value found, else returns nil



78
79
80
# File 'lib/boris/core_ext/string.rb', line 78

def before_slash
  value_before_character('\\\\|\/')
end

#between_curliesNil, String

Returns the string value found between a pair of curly brackets from self.

'A{B}C'.between_curlies  #=> "B"

Returns:

  • (Nil, String)

    string if value found, else returns nil



87
88
89
# File 'lib/boris/core_ext/string.rb', line 87

def between_curlies
  self.extract(/\{(.*)\}/)
end

#between_parenthesisNil, String

Returns the string value found between a pair of parenthesis from self.

'A(B)C'.between_parenthesis  #=> "B"

Returns:

  • (Nil, String)

    string if value found, else returns nil



96
97
98
# File 'lib/boris/core_ext/string.rb', line 96

def between_parenthesis
  self.extract(/\((.*)\)/)
end

#between_quotesNil, String

Returns the string value found between a pair of quotes (single or double) from self.

'A"B"C'.between_quotes  #=> "B"

Returns:

  • (Nil, String)

    string if value found, else returns nil



106
107
108
# File 'lib/boris/core_ext/string.rb', line 106

def between_quotes
  self.extract(/["|'](.*?)["|']/)
end

#clean_stringString

Cleans self by stripping leading/trailing spaces, any consecutive spaces, and removing any ASCII characters that are sometimes reported by devices. Also removes registered ® characters.

'Microsoft(R) Windows(R)'.clean_string             #=> "Microsoft Windows"
"string with\u00A0 weird character".clean_string  #=> "string with weird character"

Returns:

  • (String)

    the cleaned up string



118
119
120
121
122
123
124
# File 'lib/boris/core_ext/string.rb', line 118

def clean_string
  # remove registered "(R)" and trademark "(tm)" marks
  string = self.gsub(/\(r\)|\(tm\)/i, '')
  string.gsub!(/\s+/, ' ')

  string.encode(Encoding.find('ASCII'), :undef=>:replace, :replace=>'').strip
end

#extract(regex) ⇒ String, NilClass

Attempts to pull only the first match inside the parenthesis for a given regex. It’s similar to using String#match or String#scan..join to extract the first matching value (that is, the value to match on found within the parenthesis in the regex).

'abcdef'.extract(/ab(cd)ef/)  #=> "cd"
'abcdef'.extract(/abcdef/)  #=> nil

Returns:

  • (String, NilClass)

    the matched string, else returns nil



135
136
137
# File 'lib/boris/core_ext/string.rb', line 135

def extract(regex)
  self[regex, 1]
end

#format_modelString

Attempts to grab the hardware model from self and formats it for consistency to match the marketing model name as specified from the vendor. Particularly on UNIX systems, the hardware model is not reported in a consistent manner across all systems (even those from the same vendor). This method is used when scrubbing a Target’s retrieved data before outputting it to the user. Returns self if the provided model did not match any of the known model formats used within this method.

'sun fire 400'.format_model  #=> "SunFire 400"
't1000'.format_model  #=> "SPARC Enterprise T1000"

Returns:

  • (String)

    the formatted model, else returns self



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/boris/core_ext/string.rb', line 151

def format_model
  return self if self == ''

  model = self.sub(/server/i, '')

  model = if model =~ /^sun.*blade/i
    'SunBlade ' + model.extract(/(\d+)/)
  elsif model =~ /^sun.*fire/i
    'SunFire ' + model.extract(/(\d+)/)
  elsif model =~ /^T\d{4}/
    'SPARC Enterprise ' + model
  elsif model =~ /^big-*ip/i
    model.sub(/^big-*ip/i, 'BIG-IP')
  elsif model =~ /^wsc\d{4}-*.*/i
    model.sub!(/wsc/i, 'Catalyst ')
    model.include?('-') ? model : model.sub(/(\d{4})(.*)/) {$2.empty? ? "#{$1}" : "#{$1}-#{$2}" }
  else
    model
  end

  model.strip
end

#format_serialNil, String

Formats self to fit a consistent serial number format (no special characters, uppercased).

'abcd1234 '.format_serial  #=> "ABCD1234"
'(none)'.format_serial  #=> nil

Returns:

  • (Nil, String)

    the formatted serial, else returns nil if the serial does not seem legit



182
183
184
185
186
# File 'lib/boris/core_ext/string.rb', line 182

def format_serial
  return nil if self =~ /(^$|\(*none\)*)/i

  self.clean_string.upcase
end

#format_vendorString

Attempts to grab the hardware vendor name from self and formats it for consistency to match the vendor’s corproate name as specified from the vendor. This method is used when scrubbing a Target’s retrieved data before outputting it to the user. Returns self if the provided vendor name did not match any of the known vendor formats used within this method.

'hewlett packard'.format_vendor  #=> "Hewlett Packard Inc."
'sun microsystems'.format_vendor  #=> "Oracle Corp."

Returns:

  • (String)

    the formatted vendor



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/boris/core_ext/string.rb', line 198

def format_vendor
  return nil if self == ''
  
  vendor = self

  vendor = if vendor =~ /^(amd|authenticamd)/i;       VENDOR_AMD
  elsif vendor =~ /^brocade/i;                        VENDOR_BROCADE
  elsif vendor =~ /^citrix/i;                         VENDOR_CITRIX
  elsif vendor =~ /^dell/i;                           VENDOR_DELL
  elsif vendor =~ /^emulex/i;                         VENDOR_EMULEX
  elsif vendor =~ /^(compaq|hp|hewlett packard)/i;    VENDOR_HP
  elsif vendor =~ /^ibm/i;                            VENDOR_IBM
  elsif vendor =~ /^(genuineintel|intel )/i;          VENDOR_INTEL
  elsif vendor =~ /^(microsoft)/i;                    VENDOR_MICROSOFT
  elsif vendor =~ /^(oracle|sun[w]*|sun microsys)/i;  VENDOR_ORACLE
  elsif vendor =~ /^qlogic/i;                         VENDOR_QLOGIC
  elsif vendor =~ /^red\s*hat/i;                      VENDOR_REDHAT
  elsif vendor =~ /^suse linux/i;                     VENDOR_SUSE
  elsif vendor =~ /^vmware/i;                         VENDOR_VMWARE
  else vendor
  end

  vendor.strip
end

#hex_to_ip_addressNil, String

Returns the IP address value derived from self in hex format.

'ffffff00'.hex_to_ip_address  #=> "255.255.255.0"

Returns:

  • (Nil, String)

    returns the IP address



228
229
230
# File 'lib/boris/core_ext/string.rb', line 228

def hex_to_ip_address
  self.scan(/../).map {|octet| octet.hex}.join('.')
end

#pad_elapsed_timeObject

Pads self with leading zeros if needed. Useful for properly formatting a String (usually from the ps command in UNIX representing elapsed time) to a more complete, zero-padded string to the format dd-hh:mm:ss. mm::ss is the bare-minimum required String.

'00:01'.pad_elapsed_time  #=> '00-00:00:01'
'01:01'.pad_elapsed_time  #=> '00-00:01:01'
'01:01:01'.pad_elapsed_time  #=> '00-01:01:01'
'1-01:00:01'.pad_elapsed_time  #=> '01-01:01:01'

Returns:

  • String the padded elapsed time



243
244
245
246
247
248
249
250
251
252
# File 'lib/boris/core_ext/string.rb', line 243

def pad_elapsed_time
  return self if self =~ /\d{2}\-\d{2}:\d{2}:\d{2}/

  return "0#{self}" if self =~ /\d{1}-/

  case self.count(':')
  when 2; "00-#{self}"
  when 1; "00-00:#{self}"
  end
end

#pad_mac_address(delimiter = ':') ⇒ String

Pads self with leading zeros if needed. Useful for properly formatting MAC addresses. Takes an optional delimiter used for splitting and returning the provided string in the proper format. The string to be formatted is expected to already be in a six-octet format.

'0:0:0:0:0:AA'.pad_mac_address  #=> "00:00:00:00:00:AA"
'0-0-0-0-AA-12'.pad_mac_address('-')  #=> "00-00-00-00-AA-12"

Parameters:

  • delimiter (defaults to: ':')

    an optional delimiter for the MAC address (default is ‘:’)

Returns:

  • (String)

    the padded MAC address



264
265
266
267
268
# File 'lib/boris/core_ext/string.rb', line 264

def pad_mac_address(delimiter=':')
  self.split(delimiter).inject([]) do |mac, octet|
    octet.length == 1 ? mac << "0#{octet}" : mac << octet
  end.join(delimiter).upcase
end

#remove_archString

Returns a new string with the architecture removed. See #remove_arch!.

"Windows Server 2003 (64-bit)".remove_arch #=> "Windows Server 2003"

Returns:

  • (String)

    returns a new string with architecture rmeoved



275
276
277
# File 'lib/boris/core_ext/string.rb', line 275

def remove_arch
  String.new(self).remove_arch!
end

#remove_arch!String

Removes the architecture in place. See #remove_arch.

Returns:

  • (String)

    returns self with architecture rmeoved



282
283
284
# File 'lib/boris/core_ext/string.rb', line 282

def remove_arch!
  self.replace(self.gsub(/\s+\(*(32|64)(-|\s)*bit\)*/, ''))
end

#value_after_character(delimiter) ⇒ Nil, String

Allows you to specify your own delimiter to grab the string value found after the last delimiter. It’s mainly used internally with the #after_ helper methods.

'A&B&C'.value_after_character('&')  #=> "C"

Parameters:

  • delimiter

Returns:

  • (Nil, String)

    returns the found value, else returns nil



294
295
296
297
# File 'lib/boris/core_ext/string.rb', line 294

def value_after_character(delimiter)
  x = self.extract(/^.*[#{delimiter}](.*)$/)
  x.nil? ? nil : x.strip
end

#value_before_character(delimiter) ⇒ Nil, String

Allows you to specify your own delimiter to grab the string value found before the first delimiter. It’s mainly used internally with the #after_ helper methods.

'A&B&C'.value_before_character('&')  #=> "A"

Parameters:

  • delimiter

Returns:

  • (Nil, String)

    returns the found value, else returns nil



307
308
309
310
# File 'lib/boris/core_ext/string.rb', line 307

def value_before_character(delimiter)
  x = self.extract(/(.*?)[#{delimiter}]/)
  x.nil? ? nil : x.strip
end