class OrganicHash
Constants
- ADJ
- ADV
- DATA_DIR
- NOUN
- VERSION
Public Class Methods
hash(str, array = false)
click to toggle source
# File lib/organic_hash.rb, line 38 def self.hash(str, array = false) OrganicHash.new.hash str, array end
new(length = 3, delimiter = "-", hasher = Digest::SHA1, noun = NOUN, adj = ADJ, adv = ADV)
click to toggle source
# File lib/organic_hash.rb, line 12 def initialize(length = 3, delimiter = "-", hasher = Digest::SHA1, noun = NOUN, adj = ADJ, adv = ADV) @length = [1, [5, length].min].max @delimiter = delimiter @hasher = hasher @noun = noun @adj = adj @adv = adv end
rand(array = false)
click to toggle source
# File lib/organic_hash.rb, line 42 def self.rand(array = false) OrganicHash.new.rand array end
Public Instance Methods
hash(str, array = false)
click to toggle source
# File lib/organic_hash.rb, line 21 def hash(str, array = false) indexes = str2indexes str noun_idx = indexes.pop adjs, advs = indexes.partition.with_index { |_, i| i.even? } adjs = adjs.map { |i| @adj[i % @adj.length] } advs = advs.map { |i| @adv[i % @adv.length] } output = adjs.zip(advs).map(&:reverse).flatten.reject(&:nil?) + [@noun[noun_idx % @noun.length]] array ? output : output.join(@delimiter) end
rand(array = false)
click to toggle source
# File lib/organic_hash.rb, line 34 def rand(array = false) hash SecureRandom.hex, array end
Private Instance Methods
str2indexes(hex)
click to toggle source
# File lib/organic_hash.rb, line 47 def str2indexes(hex) sha1 = @hasher.hexdigest hex seg = sha1.length / @length rem = sha1.length % @length 0.upto(@length - 1).map { |i| sha1[seg*i...seg*(i+1)].to_i(16) } end