Class: TeamImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/showdown-team-import.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTeamImporter

Returns a new instance of TeamImporter.



17
18
19
# File 'lib/showdown-team-import.rb', line 17

def initialize
  @teams = Array.new
end

Instance Attribute Details

#teamsObject (readonly)

Returns the value of attribute teams.



15
16
17
# File 'lib/showdown-team-import.rb', line 15

def teams
  @teams
end

Instance Method Details

#import(team_import) ⇒ Object



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
# File 'lib/showdown-team-import.rb', line 21

def import team_import
  team = Team.new(String.new, String.new, String.new)
  line_count = 0
  in_team = false

  team_import.each do |line, count|
    next if line.equal? team_import.last # I haven't found any way to make this not an error except adding more shitty checks
    if in_team
      team.content += line unless team.content.empty? && line.strip.empty? # Beginning of file

      if line.strip == ''
        line_count += 1
        if line_count == 2 # 2 empty lines = end of current team
          @teams.push(team) # add team to array and reset everything
          team = Team.new(String.new, String.new, String.new)
          in_team = false
          line_count = 0
        end
      else # we already appended line earlier, so just resetting linecount
        line_count = 0
      end
    elsif line[0..2] == "===" && line.chomp.reverse[0..2] == "===" # beginning of team
      in_team = true
      name_and_tier = line[4..-5]
      bracket_index = name_and_tier.index(']') # 10/10 code I know
      format = name_and_tier[1..bracket_index-1] if !bracket_index.nil? && bracket_index > 2
      format = "others" unless format

      if !bracket_index.nil? && !name_and_tier[bracket_index + 1..-1].strip.empty?
        team_name = name_and_tier[bracket_index + 1..-1]
      elsif !name_and_tier.strip.empty? && bracket_index.nil?
        team_name = name_and_tier
      else
        team_name = "Untitled [#{SecureRandom.hex[0..5]}]"
      end

      team.name = team_name
      team.tier = format
    else
      raise ParserError.new(line), "Error parsing backup file"
    end
  end

  @teams.push(team) if @teams.empty?
end