Class: MyObfuscate::ConfigApplicator

Inherits:
Object
  • Object
show all
Defined in:
lib/my_obfuscate/config_applicator.rb

Class Method Summary collapse

Class Method Details

.apply_table_config(row, table_config, columns) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/my_obfuscate/config_applicator.rb', line 4

def self.apply_table_config(row, table_config, columns)
  return row unless table_config.is_a?(Hash)
  row_hash = row_as_hash(row, columns)

  table_config.each do |column, definition|
    index = columns.index(column)

    definition = { :type => definition } if definition.is_a?(Symbol)

    if definition.has_key?(:unless)
      unless_check = make_conditional_method(definition[:unless], index, row)

      next if unless_check.call(row_hash)
    end


    if definition.has_key?(:if)
      if_check = make_conditional_method(definition[:if], index, row)

      next unless if_check.call(row_hash)
    end

    if definition[:skip_regexes]
      next if definition[:skip_regexes].any? {|regex| row[index] =~ regex}
    end

    row[index.to_i] = case definition[:type]
      when :email
        md5 = Digest::MD5.hexdigest(rand.to_s)[0...5]
        clean_quotes("#{Faker::Internet.email}.#{md5}.example.com")
      when :string
        random_string(definition[:length] || 30, definition[:chars] || SENSIBLE_CHARS)
      when :lorem
        clean_bad_whitespace(clean_quotes(Faker::Lorem.sentences(definition[:number] || 1).join(".  ")))
      when :like_english
        clean_quotes random_english_sentences(definition[:number] || 1)
      when :name
        clean_quotes(Faker::Name.name)
      when :first_name
        clean_quotes(Faker::Name.first_name)
      when :last_name
        clean_quotes(Faker::Name.last_name)
      when :address
        clean_quotes("#{Faker::AddressUS.street_address}\\n#{Faker::AddressUS.city}, #{Faker::AddressUS.state_abbr} #{Faker::AddressUS.zip_code}")
      when :street_address
        clean_bad_whitespace(clean_quotes(Faker::AddressUS.street_address))
      when :city
        clean_quotes(Faker::AddressUS.city)
      when :state
        clean_quotes Faker::AddressUS.state_abbr
      when :zip_code
        Faker::AddressUS.zip_code
      when :phone
        clean_quotes Faker::PhoneNumber.phone_number
      when :company
        clean_bad_whitespace(clean_quotes(Faker::Company.name))
      when :ipv4
        Faker::Internet.ip_v4_address
      when :ipv6
        # Inlined from Faker because ffaker doesn't have ipv6.
        @@ip_v6_space ||= (0..65535).to_a
        container = (1..8).map{ |_| @@ip_v6_space.sample }
        container.map{ |n| n.to_s(16) }.join(':')
      when :url
        clean_bad_whitespace(Faker::Internet.http_url)
      when :integer
        random_integer(definition[:between] || (0..1000)).to_s
      when :fixed
        if definition[:one_of]
          definition[:one_of][(rand * definition[:one_of].length).to_i]
        else
          definition[:string].is_a?(Proc) ? definition[:string].call(row_hash) : definition[:string]
        end
      when :null
        nil
      when :keep
        row[index]
      else
        $stderr.puts "Keeping a column value by providing an unknown type (#{definition[:type]}) is deprecated.  Use :keep instead."
        row[index]
    end
  end
  row
end

.clean_bad_whitespace(value) ⇒ Object



141
142
143
# File 'lib/my_obfuscate/config_applicator.rb', line 141

def self.clean_bad_whitespace(value)
  value.gsub(/[\n\t\r]/, '')
end

.clean_quotes(value) ⇒ Object



137
138
139
# File 'lib/my_obfuscate/config_applicator.rb', line 137

def self.clean_quotes(value)
  value.gsub(/['"]/, '')
end

.make_conditional_method(conditional_method, index, row) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/my_obfuscate/config_applicator.rb', line 93

def self.make_conditional_method(conditional_method, index, row)
  if conditional_method.is_a?(Symbol)
    if conditional_method == :blank
      conditional_method = lambda { |row_hash| row[index].nil? || row[index] == '' }
    elsif conditional_method == :nil
      conditional_method = lambda { |row_hash| row[index].nil? }
    end
  end
  conditional_method
end

.random_english_sentences(num) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/my_obfuscate/config_applicator.rb', line 116

def self.random_english_sentences(num)
  @@walker_method ||= begin
    words, counts = [], []
    File.read(File.expand_path(File.join(File.dirname(__FILE__), 'data', 'en_50K.txt'))).each_line do |line|
      word, count = line.split(/\s+/)
      words << word
      counts << count.to_i
    end
    WalkerMethod.new(words, counts)
  end

  sentences = []
  num.times do
    words = []
    (3 + rand * 5).to_i.times { words << @@walker_method.random }
    sentences << words.join(" ") + "."
    sentences.last[0] = sentences.last[0].upcase
  end
  sentences.join(" ")
end

.random_integer(between) ⇒ Object



104
105
106
# File 'lib/my_obfuscate/config_applicator.rb', line 104

def self.random_integer(between)
  (between.min + (between.max - between.min) * rand).round
end

.random_string(length_or_range, chars) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/my_obfuscate/config_applicator.rb', line 108

def self.random_string(length_or_range, chars)
  length_or_range = (length_or_range..length_or_range) if length_or_range.is_a?(Fixnum)
  times = random_integer(length_or_range)
  out = ""
  times.times { out << chars[rand * chars.length] }
  out
end

.row_as_hash(row, columns) ⇒ Object



89
90
91
# File 'lib/my_obfuscate/config_applicator.rb', line 89

def self.row_as_hash(row, columns)
  columns.zip(row).inject({}) {|m, (name, value)| m[name] = value; m}
end