Skip to content

Instantly share code, notes, and snippets.

@estebanz01
Created December 18, 2021 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estebanz01/0218b61ed263a92ff706de52c5c2ea7f to your computer and use it in GitHub Desktop.
Save estebanz01/0218b61ed263a92ff706de52c5c2ea7f to your computer and use it in GitHub Desktop.
Generate a hash with arbitrary depth and keys with fake information. Because why not.
source 'https://rubygems.org'
gem 'faker'
#!/usr/bin/env ruby
require 'json'
require 'faker'
class Generator
def self.json_object(depth: 1, keys_per_level: 2)
raise StandardError.new("Incorrect params depth: #{depth} keys: #{keys_per_level}") if depth <= 0 || keys_per_level <= 0
# one/last depth
# { key: val, key: val, ... }
if depth == 1
Hash.new.tap do |hash|
keys_per_level.times do
hash[Faker::Barcode.unique.ean(13).freeze] = Faker::Code.nric(min_age: 1, max_age: 100).freeze
end
end
else
# n-depth
# { key: { ... }, key: { ... }, ... }
Hash.new.tap do |hash|
keys_per_level.times do |n|
hash[Faker::Bank.unique.iban] = self.json_object(depth: depth - 1, keys_per_level: keys_per_level)
end
end
end
end
end
simple = Generator.json_object#.to_json
complex = Generator.json_object(depth: 5)#.to_json
pp simple
pp complex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment