module ReadableToken

A tiny Ruby library for generating human readable “tokens”.

Constants

Version

Public Class Methods

generate(opts = {}) click to toggle source

Generate a new token.

# File lib/readable-token.rb, line 17
def self.generate(opts = {})
  # set default options and merge with user provided
  options = {
    min: 8,
    max: nil
  }.merge(opts)

  segments = []

  # add number
  segments << rand(1..9).to_s

  # count number of loops so were not spinning our wheels if we can't meet
  # the criteria requested by the user.
  looped = 0
  while current_length_of(segments) < options[:min]
    new_word = ''
    remove_blanks!(segments)

    looped += 1
    break if looped > 20

    current_length = current_length_of(segments)
    if options[:max] && options[:max] > 0
      remaining = options[:max] - current_length
      new_word = filter_by_length(words, remaining - 1).sample
    else
      new_word = words.sample
    end

    new_length = current_length_of(segments + [new_word])
    next if options[:max] && options[:max] > 0 && new_length > options[:max]

    segments << new_word
  end
  remove_blanks!(segments)

  # join it all together to give us a token
  segments.reverse.join('-')
end
words() click to toggle source

Access to current list of words

# File lib/readable-token.rb, line 7
def self.words
  @@words ||= %w{dream season applause music venue talk top spoon fork tea coffee soup eat drink chat social happy doughnut beans bistro cafe lunch java ruby penguin lion leopard cake tiger crab fish moo milk owl roast aroma balance barista blend body decaf espresso flavor mocha latte organic friend}
end
words=(vals) click to toggle source

Set list of words to the given array

# File lib/readable-token.rb, line 12
def self.words=(vals)
  @@words = vals
end

Private Class Methods

current_length_of(segments) click to toggle source

Calculate the current length

# File lib/readable-token.rb, line 66
def self.current_length_of(segments)
  segments.join('-').length
end
filter_by_length(words, max) click to toggle source

Filter array of words by their maximum length

# File lib/readable-token.rb, line 71
def self.filter_by_length(words, max)
  words.select { |w| w.length <= max }
end
remove_blanks!(segments) click to toggle source

Remove blank, empty, nil array elements

# File lib/readable-token.rb, line 61
def self.remove_blanks!(segments)
  segments.delete_if(&:nil?).delete_if(&:empty?)
end