Class: RandomHex::RandomNumberTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
random_hex.rb

Overview

Outputs a string of random hexadecimal characters of any length. Defaults to a six-character string.

Examples:

Generate 6 random characters.

{{ random_hex_string }}

Generate 20 random characters.

{{ random_hex_string 10 }}

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, context) ⇒ String?

Called by Jekyll only once to register the module.

Parameters:

  • tag_name (String)

    Describe this parameter's purpose

  • text (String)

    Describe this parameter's purpose

  • context (String)

    Describe this parameter's purpose



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'random_hex.rb', line 18

def initialize(tag_name, text, context)
  super#(tag_name, text, context)
  text.to_s.strip!
  if text.empty?
    @n = 6
  else
    tokens = text.split(' ')
    abort "random_hex_string error - more than one token was provided: '#{text}'" if tokens.length > 1
    not_integer = !Integer(text, exception: false)
    abort "random_hex_string error: '#{text}' is not a valid integer" if not_integer
    @n = text.to_i
  end
end

Instance Method Details

#render(_) ⇒ Object



32
33
34
35
# File 'random_hex.rb', line 32

def render(_)
  require 'securerandom'
  SecureRandom.hex(@n)
end