|
| 1 | +require 'faker' |
| 2 | +require 'kraken-mobile/utils/k' |
| 3 | +require 'json' |
| 4 | + |
| 5 | +class KrakenFaker |
| 6 | + #------------------------------- |
| 7 | + # Fields |
| 8 | + #------------------------------- |
| 9 | + attr_accessor :process_id |
| 10 | + |
| 11 | + #------------------------------- |
| 12 | + # Constructors |
| 13 | + #------------------------------- |
| 14 | + def initialize(process_id:) |
| 15 | + raise 'ERROR: Can\'t create faker for null process id' if process_id.nil? |
| 16 | + |
| 17 | + @process_id = process_id |
| 18 | + end |
| 19 | + |
| 20 | + #------------------------------- |
| 21 | + # Helpers |
| 22 | + #------------------------------- |
| 23 | + def generate_value_for_key(key:) |
| 24 | + value = if key.start_with?('$name') |
| 25 | + generate_name |
| 26 | + elsif key.start_with?('$number') |
| 27 | + generate_number |
| 28 | + elsif key.start_with?('$email') |
| 29 | + generate_email |
| 30 | + elsif key.start_with?('$string') |
| 31 | + generate_string |
| 32 | + elsif key.start_with?('$date') |
| 33 | + generate_date |
| 34 | + else |
| 35 | + raise 'ERROR: Faker key not supported' |
| 36 | + end |
| 37 | + save_key_value_in_dictionary(key: key, value: value) |
| 38 | + value |
| 39 | + end |
| 40 | + |
| 41 | + def reuse_value_for_key(key:) |
| 42 | + dictionary = dictionary_json |
| 43 | + key = key.delete_prefix('$') |
| 44 | + |
| 45 | + raise 'ERROR: Key does not exist' if dictionary[process_id.to_s].nil? |
| 46 | + if dictionary[process_id.to_s][key.to_s].nil? |
| 47 | + raise 'ERROR: Key does not exist' |
| 48 | + end |
| 49 | + |
| 50 | + dictionary[process_id.to_s][key.to_s] |
| 51 | + end |
| 52 | + |
| 53 | + def dictionary_json |
| 54 | + create_dictionary_json_file unless dictionary_json_file_exists? |
| 55 | + |
| 56 | + absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH) |
| 57 | + file = open(absolute_dictionary_path) |
| 58 | + content = file.read |
| 59 | + JSON.parse(content) |
| 60 | + end |
| 61 | + |
| 62 | + def create_dictionary_json_file |
| 63 | + absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH) |
| 64 | + File.open(absolute_dictionary_path, 'w') do |f| |
| 65 | + f.puts({}.to_json) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def dictionary_json_file_exists? |
| 70 | + absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH) |
| 71 | + File.file?(absolute_dictionary_path) |
| 72 | + end |
| 73 | + |
| 74 | + def save_key_value_in_dictionary(key:, value:) |
| 75 | + current_json = dictionary_json |
| 76 | + current_json[process_id.to_s] = {} if current_json[process_id.to_s].nil? |
| 77 | + current_json[process_id.to_s][key.to_s] = value |
| 78 | + |
| 79 | + absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH) |
| 80 | + open(absolute_dictionary_path, 'w') do |f| |
| 81 | + f.puts(current_json.to_json) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + #------------------------------- |
| 86 | + # Generators |
| 87 | + #------------------------------- |
| 88 | + def generate_name |
| 89 | + Faker::Name.name |
| 90 | + end |
| 91 | + |
| 92 | + def generate_number |
| 93 | + Faker::Number.number(digits: rand(10)) |
| 94 | + end |
| 95 | + |
| 96 | + def generate_email |
| 97 | + Faker::Internet.email |
| 98 | + end |
| 99 | + |
| 100 | + def generate_string |
| 101 | + Faker::String.random(length: rand(100)) |
| 102 | + end |
| 103 | + |
| 104 | + def generate_date |
| 105 | + Faker::Date.in_date_period.to_s |
| 106 | + end |
| 107 | +end |
0 commit comments