Class: String

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

Overview

– – Reopen the core ruby String class and add the below methods to it. – – Case Sensitivity rules for [ALL] the below methods that are – added to the core Ruby string class. – – For case insensitive behaviour make sure you downcase both the – string object and the parameter strings (or strings within – other parameter objects, like arrays and hashes). –

Instance Method Summary collapse

Instance Method Details

#decrypt(encrypt_key) ⇒ Object

– – Decrypt this string with the parameter encryption/decryption key – and return the decrypted text as a new string. – – encrypt_key => the key the input string was encrypted with – –



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/opensecret/additions/string.rb', line 273

def decrypt encrypt_key

## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting
## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting
## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting
## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting

###### ON Linux improve by changing to OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
###### ON Linux improve by changing to Digest::SHA2.hexdigest decrypt_key
###### ON Linux improve by changing to OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
###### ON Linux improve by changing to Digest::SHA2.hexdigest decrypt_key
###### ON Linux improve by changing to OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
###### ON Linux improve by changing to Digest::SHA2.hexdigest decrypt_key

  cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC').decrypt
  cipher.key = Digest::SHA1.hexdigest encrypt_key
  uncrypted = [self].pack("H*").unpack("C*").pack("c*")
  decrypted_text = cipher.update(uncrypted) + cipher.final

  return decrypted_text

end

#encrypt(decrypt_key) ⇒ Object

– – Encrypt this string with the parameter encryption/decryption key – and return the encrypted text as a new string. – – decrypt_key => the key that will decrypt the output string – –



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/opensecret/additions/string.rb', line 240

def encrypt decrypt_key

## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting
## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting
## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting
## ----> Write a RE-CRYPT method that goes through a folder - decrypting and recrypting

###### ON Linux improve by changing to OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
###### ON Linux improve by changing to Digest::SHA2.hexdigest decrypt_key
###### ON Linux improve by changing to OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
###### ON Linux improve by changing to Digest::SHA2.hexdigest decrypt_key
###### ON Linux improve by changing to OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
###### ON Linux improve by changing to Digest::SHA2.hexdigest decrypt_key
###### ON Linux improve by changing to OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
###### ON Linux improve by changing to Digest::SHA2.hexdigest decrypt_key

  cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC').encrypt
  cipher.key = Digest::SHA1.hexdigest decrypt_key
  crypted = cipher.update(self) + cipher.final
  encrypted_text = crypted.unpack('H*')[0].upcase

  return encrypted_text

end

#has_wrapped?(little_str, prefix, postfix) ⇒ Boolean

– – Return true if the [little string] within this – string object is both – – a] topped by the parameter prefix AND – b] tailed by the parameter postfix – – —————————————– – In the below example [true] is returned – —————————————– – – This [String] => “Hey [<-secrets->] are juicy.” – little string => “secrets” – topped string => “[<-” – tailed string => “->]” – – Why true? Because the little string “secret” is – (wrapped) topped by “[<-” and tailed by “->]” – – —————————————– – Assumptions | Constraints | Boundaries – —————————————– – – - all matches are [case sensitive] – - this string must contain little_str – - one strike and its true – (if little string appears more than once) – so => “all secrets, most [<-secrets->] r juicy” – => true as long as (at least) one is wrapped – –

Returns:

  • (Boolean)


123
124
125
126
127
# File 'lib/opensecret/additions/string.rb', line 123

def has_wrapped? little_str, prefix, postfix

  return self.include?( prefix + little_str + postfix )

end

#in_between(this_delimeter, that_delimeter) ⇒ Object

– – Get the text [in between] this and that delimeter [exclusively]. – Exclusively means the returned text [does not] include either of – the matched delimeters (although an unmatched instance of [this] – delimeter may appear in the in-between text). – – ——————– – Multiple Delimiters – ——————– – – When multiple delimiters exist, the text returned is in between the – – [a] - first occurrence of [this] delimeter AND the – [b] - 1st occurrence of [that] delimeter [AFTER] the 1st delimiter – – Instances of [that] delimiter occurring before [this] are ignored. – The text could contain [this] delimeter instances but is guaranteed – not to contain a [that] delimeter. – – ———– – Parameters – ———– – – this_delimiter : begin delimeter (not included in returned string) – that_delimiter : end delimeter (not included in returned string) – – ———– – Exceptions – ———– – – An exception (error) will be thrown if – – => any nil (or empties) exist in the input parameters – => [this] delimeter does not appear in the in_string – => [that] delimeter does not appear after [this] one –



51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/opensecret/additions/string.rb', line 51

def in_between this_delimeter, that_delimeter

  Throw.if_nil_or_empty_strings [ self, this_delimeter, that_delimeter ]

  scanner_1 = StringScanner.new self
  scanner_1.scan_until /#{this_delimeter}/
  scanner_2 = StringScanner.new scanner_1.post_match
  scanner_2.scan_until /#{that_delimeter}/

  in_between_text = scanner_2.pre_match.strip
  log.info(ere){ in_between_text }

  return in_between_text

# --> file_text = File.read "c:/Users/apollo13/mirror.techwiki/documents/collaboration-tech/gollum/gollum-certbot-ssl.md"

# --> this_delimeter = "<!-- facts"
# --> that_delimeter = "-->"

# --> scanner = StringScanner.new file_text
# --> log.info(ere){ "1st Delimiter => #{this_delimeter}" }
# --> log.info(ere){ "2nd Delimiter => #{that_delimeter}" }
# --> before_text = scanner.scan_until /#{this_delimeter}/
# --> post_match_text = scanner.post_match
# --> scanner2 = StringScanner.new post_match_text
# --> scanner2.scan_until /#{that_delimeter}/
# --> best_text = scanner2.pre_match.strip

# --> best_text = Strings.in_between file_text, this_delimeter, that_delimeter

# --> log.info(ere){ "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" }
# --> log.info(ere){ "What We Want" }
# --> log.info(ere){ "======================================================" }
# --> log.info(ere){ best_text }
# --> log.info(ere){ "======================================================" }

# --> exit

end

#includes_all?(word_array) ⇒ Boolean

– – Return [TRUE] if this string includes [every word] harboured – by the parameter array, else return [FALSE]. The “include” – is a case-sensitive search. – – ———– – Parameter – ———– – – word_array : array of string words for the inclusivity test – – ——————————– – Dependencies and Assumptions – ——————————– – – the parameter string is not nil – array can be [empty] but not nil – array contents are neither nil nor empty –

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/opensecret/additions/string.rb', line 181

def includes_all? word_array

  Throw.if_nil self
  Throw.if_nil word_array

  word_array.each do |word|

    Throw.if_nil word
    return false unless self.include? word

  end

  return true

end

#includes_any?(word_array) ⇒ Boolean

– – return true if the string includes [ANY] word within the – parameter array, else return false. The “include” is done – as a case-sensitive search. – – ———– – Parameter – ———– – – word_array : array of string words for the inclusivity test – – ——————————– – Dependencies and Assumptions – ——————————– – – array can be [empty] but not nil – array contents are neither nil nor empty –

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/opensecret/additions/string.rb', line 216

def includes_any? word_array

  Throw.if_nil self
  Throw.if_nil word_array

  word_array.each do |word|

    Throw.if_nil word
    return true if self.include? word

  end

  return false

end

#log_linesObject

– – Log the string which is expected to be delineated. – If the string originated from a file it will be logged – line by line. If no line delineation the string will be – dumped just as a blob. –



303
304
305
306
307
308
309
310
# File 'lib/opensecret/additions/string.rb', line 303

def log_lines

  self.each_line do |line|
    clean_line = line.chomp.gsub("\\n","")
    log.info(ere) { line } if clean_line.length > 0
  end

end

#sandwich_substr(to_wrap_str, prefix, postfix) ⇒ Object

– – Sandwich the first occurrence of a substring in – this string with the specified pre and postfix. – – This string contains the little string and an – IN-PLACE change is performed with the first – occurrence of the little string being prefixed – and postfixed with the 2 parameter strings. – – ———————————- – Example of sandwiching [wrapping] – ———————————- – – [String] => “Hey secrets are juicy.” – [To_Wrap] => “secrets” – [Prefix] => “[<-” – [Postfix] => “->]” – – [String] => “Hey [<-secrets->] are juicy.” – – This string IS changed in place. –



152
153
154
155
156
157
158
159
# File 'lib/opensecret/additions/string.rb', line 152

def sandwich_substr to_wrap_str, prefix, postfix

  occurs_index = self.downcase.index to_wrap_str.downcase
  self.insert occurs_index, prefix
  shifted_index = occurs_index + prefix.length + to_wrap_str.length
  self.insert shifted_index, postfix

end