Class: Opto::Resolvers::RandomString

Inherits:
Opto::Resolver show all
Defined in:
lib/opto/resolvers/random_string.rb

Overview

Generates a random string.

Requires at least :length. Also accepts :charset which can be one of:

  • numbers (0-9),

  • letters (a-z + A-Z),

  • downcase (a-z),

  • upcase (A-Z),

  • alphanumeric (0-9 + a-z + A-Z),

  • hex (0-9 + a-f),

  • hex_upcase (0-9 + A-F),

  • base64 (base64 charset (length has to be divisible by four when using base64)),

  • ascii_printable (all printable ascii chars)

  • or a set of characters, for example: { length: 8, charset: '01' } Will generate something like: 01001100

Instance Attribute Summary

Attributes inherited from Opto::Resolver

#hint, #option

Instance Method Summary collapse

Methods inherited from Opto::Resolver

for, inherited, #initialize, origin, #origin, resolvers

Constructor Details

This class inherits a constructor from Opto::Resolver

Instance Method Details

#charset(name) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opto/resolvers/random_string.rb', line 25

def charset(name)
  case name.to_s
  when 'numbers'
    (0..9).map(&:to_s)
  when /\A\d+\-\d+\z/, /\A[a-z]\-[a-z]\z/
    from, to = name.split('-')
    (from..to).map(&:to_s)
  when 'letters'
    charset('upcase') + charset('downcase')
  when 'downcase'
    ('a'..'z').to_a
  when 'upcase'
    ('A'..'Z').to_a
  when 'alphanumeric'
    charset('letters') + charset('numbers')
  when 'hex'
    charset('numbers') + ('a'..'f').to_a
  when 'hex_upcase'
    charset('numbers') + ('A'..'F').to_a
  when 'base64'
    charset('alphanumeric') + ['+', '/']
  when 'ascii_printable'
    (33..126).map {|ord| ord.chr}
  else
    name.to_s.split('')
  end
end

#resolveObject


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opto/resolvers/random_string.rb', line 53

def resolve
  if hint.kind_of?(Hash)
    if hint[:length].nil?
      raise ArgumentError, "Invalid settings for random string. Required: length, optional: charset. Charsets : numbers, letters, alphanumeric, hex, base64, ascii_printable and X-Y range."
    end
  elsif (hint.kind_of?(String) && hint.to_i > 0) || hint.kind_of?(0.class)
    self.hint = { length: hint.to_i }
  else
    raise ArgumentError, "Missing settings for random string."
  end

  if hint[:charset].to_s == 'base64' && hint[:length] % 4 != 0
    raise ArgumentError, "Length must be divisible by 4 when using base64"
  end

  chars = charset(hint[:charset] || 'alphanumeric')
  (1..hint[:length].to_i).each_with_object('') do |_, str|
    str << chars.sample
  end
end