-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Faker::Json #1156
Merged
Merged
Add Faker::Json #1156
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b5870d8
Add Faker::Json with tests and documentation
the-wendell b7b1e68
Merge https://github.com/stympy/faker
the-wendell cde8723
Fix rubo cop erros
the-wendell 4e7b1e2
Fix rubocop erros in test_faker_json.rb
the-wendell 88b83c2
Update json.md
vbrazo 32fbedd
Merge branch 'master' into master
vbrazo 8c6de3e
Update json.rb
vbrazo 8de68dc
Update test_faker_json.rb
vbrazo 299cc1e
Update test_faker_json.rb
vbrazo e667184
Update json.rb
vbrazo 6f8c5f3
Update .rubocop.yml
vbrazo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Faker::Json | ||
|
||
**shallow_json(width_int, options_hash)** -> JSON formated string | ||
|
||
Takes a width_int and options_hash where the number of key value pairs in the | ||
returned JSON string is equal to the width_int. | ||
|
||
`options_hash = {key: Class.method, value: Class.method}` where Class is | ||
any class in the Faker gem. For example if you wanted random names for keys | ||
and random Star Wars quotes for values you would write the options_hash as follows | ||
```ruby | ||
options_hash = { key: 'Name.first_name', value: 'StarWars.quote' } | ||
``` | ||
|
||
It is important to note that you do not need to append your Faker Gem Class calls | ||
with `Faker::` | ||
|
||
```ruby | ||
Faker::Json.shallow_json(3, key: 'RockBand.name', value: 'Seinfeld.quote') | ||
# => | ||
{"Parliament Funkadelic":"They're real, and they're spectacular.", | ||
"Fleetwood Mac":"I'm not a lesbian. I hate men, but I'm not a lesbian.", | ||
"The Roots":"It became very clear to me sitting out there today that every decision | ||
I've made in my entire life has been wrong. My life is the complete opposite of everything | ||
I want it to be. Every instinct I have, in every aspect of life, be it something to wear, | ||
something to eat - it's all been wrong."} | ||
``` | ||
|
||
**add_depth_to_json(JSON, width_int, options_hash)** -> JSON | ||
|
||
Functions exactly as `Json#shallow_json()` except it takes in a JSON as an | ||
additional argument and returns that JSON with new generated nested JSONs in | ||
place of the lowest nested values. It is important to note that the JSON must | ||
be a JSON and not a hash. | ||
|
||
```ruby | ||
json = Faker::Json.shallow_json(3, key: 'Name.first_name', value: 'Name.last_name') | ||
puts json # => | ||
{"Alisha":"Olson","Everardo":"DuBuque","Bridgette":"Turner"} | ||
|
||
json2 = Faker::Json.add_depth_to_json(json, 2, key: 'Name.first_name', value: 'Name.last_name') | ||
puts json2 # => | ||
{"Alisha":{"Daisy":"Trantow","Oda":"Haag"}, | ||
"Everardo":{"Javier":"Marvin","Eliseo":"Schuppe"}, | ||
"Bridgette":{"Jorge":"Kertzmann","Lelah":"MacGyver"}} | ||
|
||
json3 = Faker::Json.add_depth_to_json(json2, 4, key: 'Name.first_name', value: 'Name.last_name') | ||
puts json3 # => | ||
{"Alisha": | ||
{"Daisy": | ||
{"Bulah":"Wunsch","Cristian":"Champlin","Lester":"Bartoletti","Greg":"Jacobson"}, | ||
"Oda": | ||
{"Salvatore":"Kuhlman","Aubree":"Okuneva","Larry":"Schmitt","Velva":"Gibson"}}, | ||
"Everardo": | ||
{"Javier": | ||
{"Eduardo":"Orn","Laila":"Kub","Thad":"Legros","Dion":"Wilderman"}, | ||
"Eliseo": | ||
{"Olin":"Hilpert","Marisa":"Greenfelder","Karlee":"Schmitt","Judd":"Larkin"}}, | ||
"Bridgette": | ||
{"Jorge": | ||
{"Eloy":"Pfeffer","Kody":"Hansen","Paxton":"Lubowitz","Abe":"Lesch"}, | ||
"Lelah": | ||
{"Rick":"Wiza","Bonita":"Bayer","Gardner":"Auer","Felicity":"Abbott"}}} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Faker | ||
class Json < Base | ||
require 'json' | ||
|
||
class << self | ||
|
||
def shallow_json(width = 3, options = {key: 'Name.first_name', value: 'Name.first_name'}) | ||
options[:key] = options[:key].prepend('Faker::') | ||
options[:value] = options[:value].prepend('Faker::') | ||
|
||
hash = build_shallow_hash(width, options) | ||
JSON.generate(hash) | ||
end | ||
|
||
def add_depth_to_json(json = shallow_json(), width = 3, options = {key: 'Name.first_name', value: 'Name.first_name'}) | ||
options[:key] = options[:key].prepend('Faker::') | ||
options[:value] = options[:value].prepend('Faker::') | ||
|
||
hash = JSON.parse(json) | ||
hash.each do |key, value| | ||
add_hash_to_bottom(hash, [key], width, options) | ||
end | ||
JSON.generate(hash) | ||
end | ||
|
||
private | ||
|
||
def build_shallow_hash(width, options) | ||
key = options[:key] | ||
value = options[:value] | ||
|
||
hash = {} | ||
width.times do | ||
hash[eval(key)] = eval(value) | ||
end | ||
hash | ||
end | ||
|
||
def add_hash_to_bottom(hash, key_array, width, options) | ||
key_string = build_keys_from_array(key_array) | ||
if eval("hash#{key_string}").is_a?(::Hash) | ||
eval("hash#{key_string}").each do |key, value| | ||
key_array << key | ||
add_hash_to_bottom(hash, key_array, width, options) | ||
end | ||
else | ||
add_hash(key_array, hash, width, options) | ||
key_array.pop() | ||
end | ||
end | ||
|
||
def add_hash(key_array, hash, width, options) | ||
string_to_eval = 'hash' | ||
key_array.length.times do |index| | ||
string_to_eval << "['#{key_array[index]}']" | ||
end | ||
string_to_eval << " = #{build_shallow_hash(width, options)}" | ||
eval(string_to_eval) | ||
end | ||
|
||
def build_keys_from_array(key_array) | ||
key_string = "" | ||
key_array.each do |value| | ||
key_string << "['#{value}']" | ||
end | ||
key_string | ||
end | ||
|
||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') | ||
|
||
class TestFakerJson < Test::Unit::TestCase | ||
require 'json' | ||
|
||
def setup | ||
@tester = Faker::Json | ||
end | ||
|
||
def test_shallow_json | ||
json = Faker::Json.shallow_json(3, key: 'Name.first_name', value: 'Name.first_name') | ||
assert JSON.parse(json).flatten.length.equal?(6) | ||
end | ||
|
||
def test_add_depth_to_json | ||
json = Faker::Json.shallow_json(3, key: 'Name.first_name', value: 'Name.first_name') | ||
json = Faker::Json.add_depth_to_json(json, 3, key: 'Name.first_name', value: 'Name.first_name') | ||
assert JSON.parse(json).flatten[1].flatten.length.equal?(6) | ||
assert JSON.parse(json).flatten[3].flatten.length.equal?(6) | ||
assert JSON.parse(json).flatten[5].flatten.length.equal?(6) | ||
|
||
json = Faker::Json.add_depth_to_json(json, 3, key: 'Name.first_name', value: 'Name.first_name') | ||
assert JSON.parse(json).flatten[1].flatten[1].flatten.length.equal?(6) | ||
assert JSON.parse(json).flatten[3].flatten[3].flatten.length.equal?(6) | ||
assert JSON.parse(json).flatten[5].flatten[5].flatten.length.equal?(6) | ||
end | ||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be
if you want random names for keys
instead ofwanted
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I will fix it.