Class: MagicMountain::FileFormater

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_mountain/file_formater.rb

Class Method Summary collapse

Class Method Details

.break_curse(file_name, spell) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/magic_mountain/file_formater.rb', line 45

def self.break_curse(file_name, spell)
  healed_file_name = File.join(File.dirname(file_name), '..', 'healed/' + File.basename(file_name))
  healed_file = File.open( healed_file_name, 'w')
  healed_file.write("#{spell.join(',')}\n")
  raw_file  = File.open( file_name, "rb:UTF-16LE" )
  while line = raw_file.gets
    #content = line.bytes.to_a.pack('c*').split("\r")
    line = line.encode("UTF-8").gsub("\r","\n").gsub("\xEF\xBB\xBF".force_encoding("UTF-8"), '')
    content = line.split("\n")
    content.each do |line|
      fields = line.split(",")
      full_row = "#{line},#{generate_pass},#{generate_pass}\n"
      healed_file.write(full_row) if fields.size + 2 == spell.size and fields[0].gsub("\"","").to_i > 0
    end
  end
  raw_file.close
end

.curse_name?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/magic_mountain/file_formater.rb', line 24

def self.curse_name?(file_name)
  raw_file  = File.open( file_name, "rb:UTF-16LE" )
  max_num_of_fields = 0
  while line = raw_file.gets
    content = line.bytes.to_a.pack('c*').split("\r")
    content.each do |line|
      fields = line.split(",")
      max_num_of_fields = fields.size if fields.size > max_num_of_fields
    end
  end
  raw_file.close
  case max_num_of_fields
  when 14
    "need_account"
  when 17
    "new_students"
  else
    max_num_of_fields
  end
end

.generate_passObject



6
7
8
# File 'lib/magic_mountain/file_formater.rb', line 6

def self.generate_pass
    KeePass::Password.generate('[A]{8}', :remove_lookalikes => true)
end

.is_cursed?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/magic_mountain/file_formater.rb', line 10

def self.is_cursed?(file_name)
  begin
    CSV.read(file_name)
  rescue ArgumentError => e
    return true if e.message == "invalid byte sequence in UTF-8"
    puts e.inspect
    return false
  rescue Exception => e
    puts e.inspect
    return false
  end
  return false
end