Class: Faker::Json

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/json.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.add_depth_to_json(legacy_json = NOT_GIVEN, legacy_width = NOT_GIVEN, legacy_options = NOT_GIVEN, json: shallow_json, width: 3, options: { key: 'Name.first_name', value: 'Name.first_name' }) ⇒ Hash{String => String}

Produces a random nested JSON formatted string that can take JSON as an additional argument.

rubocop:disable Metrics/ParameterLists

Examples:

json = Faker::Json.shallow_json(width: 3, options: { key: 'Name.first_name', value: 'Name.last_name' })
puts json # =>
  {"Alisha":"Olson","Everardo":"DuBuque","Bridgette":"Turner"}

json2 = Faker::Json.add_depth_to_json(json: json, width: 2, options: { 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(json: json2, width: 4, options: { 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"}}}

Parameters:

  • json (Hash{String => String}) (defaults to: shallow_json)

    Specifies a Json.shallow_json and uses its keys as keys of the nested JSON.

  • width (Integer) (defaults to: 3)

    Specifies the number of nested key-value pairs.

  • options (Hash) (defaults to: { key: 'Name.first_name', value: 'Name.first_name' })

    Specifies a Faker gem class to use for nested keys and for values, respectably. options_hash = Class.method, value: Class.method

Returns:

  • (Hash{String => String})

Available since:

  • 1.9.2


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/faker/default/json.rb', line 75

def add_depth_to_json(legacy_json = NOT_GIVEN, legacy_width = NOT_GIVEN, legacy_options = NOT_GIVEN, json: shallow_json, width: 3, options: { key: 'Name.first_name', value: 'Name.first_name' })
  # rubocop:enable Metrics/ParameterLists
  warn_for_deprecated_arguments do |keywords|
    keywords << :json if legacy_json != NOT_GIVEN
  end
  warn_for_deprecated_arguments do |keywords|
    keywords << :width if legacy_width != NOT_GIVEN
  end
  warn_for_deprecated_arguments do |keywords|
    keywords << :options if legacy_options != NOT_GIVEN
  end

  options[:key] = 'Faker::' + options[:key]
  options[:value] = 'Faker::' + options[:value]

  hash = JSON.parse(json)
  hash.each do |key, _|
    add_hash_to_bottom(hash, [key], width, options)
  end
  JSON.generate(hash)
end

.shallow_json(legacy_width = NOT_GIVEN, legacy_options = NOT_GIVEN, width: 3, options: { key: 'Name.first_name', value: 'Name.first_name' }) ⇒ Hash{String => String}

Produces a random simple JSON formatted string.

Examples:

Faker::Json.shallow_json(width: 3, options: { 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."}

Parameters:

  • width (Integer) (defaults to: 3)

    Specifies the number of key-value pairs.

  • options (Hash) (defaults to: { key: 'Name.first_name', value: 'Name.first_name' })

    Specifies a Faker gem class to use for keys and for values, respectably. options_hash = Class.method, value: Class.method

Returns:

  • (Hash{String => String})

Available since:

  • 1.9.2


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/faker/default/json.rb', line 23

def shallow_json(legacy_width = NOT_GIVEN, legacy_options = NOT_GIVEN, width: 3, options: { key: 'Name.first_name', value: 'Name.first_name' })
  warn_for_deprecated_arguments do |keywords|
    keywords << :width if legacy_width != NOT_GIVEN
    keywords << :options if legacy_options != NOT_GIVEN
  end

  options[:key] = 'Faker::' + options[:key]
  options[:value] = 'Faker::' + options[:value]

  hash = build_shallow_hash(width, options)
  JSON.generate(hash)
end