Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/atech/extensions/string.rb
Direct Known Subclasses
Class Method Summary collapse
-
.generate_token(options = {}) ⇒ Object
Returns a psuedo-random UUID which sepearated by hyphens in standard UUID format.
-
.random(size = 25) ⇒ Object
Returns a random string of the size specified.
Instance Method Summary collapse
-
#first_character ⇒ Object
Returns the first character of a string.
-
#to_md5(length = 32) ⇒ Object
Returns the hex-encoded MD5 hash value of the current string.
-
#to_sha1(length = 40) ⇒ Object
Returns the hex-encoded SHA1 hash value of the current string.
Class Method Details
.generate_token(options = {}) ⇒ Object
Returns a psuedo-random UUID which sepearated by hyphens in standard UUID format. It’s worth noting this doesn’t actually follow the specification for Version 4 UUIDs.
14 15 16 17 |
# File 'lib/atech/extensions/string.rb', line 14 def generate_token( = {}) values = [rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x1000000), rand(0x1000000)] "%04x%04x-%04x-%04x-%04x-%06x%06x" % values end |
.random(size = 25) ⇒ Object
Returns a random string of the size specified. This can be used for salts or generated passwords. The generated string will only include lower case characters and numbers 0 though 9.
22 23 24 25 |
# File 'lib/atech/extensions/string.rb', line 22 def random(size = 25) array = ('a'..'z').to_a + (0..9).to_a (0...size).map{ array[rand(array.size)] }.join end |
Instance Method Details
#first_character ⇒ Object
Returns the first character of a string.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/atech/extensions/string.rb', line 41 def first_character case self[0] when 194..223 return self[0,2] when 224..239 return self[0,3] when 240..244 return self[0,4] else return self[0,1] end end |
#to_md5(length = 32) ⇒ Object
Returns the hex-encoded MD5 hash value of the current string. Optionally, pass a value to limit the length of the returned SHA.
36 37 38 |
# File 'lib/atech/extensions/string.rb', line 36 def to_md5(length = 32) Digest::MD5.hexdigest(self)[0,length] end |
#to_sha1(length = 40) ⇒ Object
Returns the hex-encoded SHA1 hash value of the current string. Optionally, pass a value to limit the length of the returned SHA.
30 31 32 |
# File 'lib/atech/extensions/string.rb', line 30 def to_sha1(length = 40) Digest::SHA1.hexdigest(self)[0,length] end |