Created
December 18, 2021 02:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'faker' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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