Class: UVaTools::Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/uva-tools/problem.rb

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ Problem

Returns a new instance of Problem.



27
28
29
# File 'lib/uva-tools/problem.rb', line 27

def initialize(array)
  @info = array
end

Instance Method Details

#acceptedObject



56
57
58
# File 'lib/uva-tools/problem.rb', line 56

def accepted
  @info[18]
end

#dacuObject



60
61
62
# File 'lib/uva-tools/problem.rb', line 60

def dacu
  @info[3]
end

#downloadObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/uva-tools/problem.rb', line 72

def download
  unless downloaded?
    FileUtils::mkdir_p dir_name
    Dir.chdir(dir_name) do
      uri = URI("https://uva.onlinejudge.org/external/#{number/100}/#{number}.html")
      source = Net::HTTP.get(uri)
      if source.include? "URL=p#{number}.pdf"
        File.open("#{number.to_s}.pdf", 'w') do |pdf|
          pdf.write open("https://uva.onlinejudge.org/external/#{number/100}/p#{number}.pdf").read
        end
      else
        File.open("#{number.to_s}.html", 'w') { |f| f.write source }

        source.scan(/#{number}img\d{1,3}\.[a-zA-Z]+/).uniq.each do |picture|
          File.open(picture, 'wb') do |pic_file|
            pic_file.write open("https://uva.onlinejudge.org/external/#{number/100}/#{picture}").read
          end
        end
      end
    end
    true
  else
    false
  end
end

#downloaded?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/uva-tools/problem.rb', line 113

def downloaded?
  File.directory?(dir_name) && (File.exists?("#{dir_name}/#{number}.html") || File.exists?("#{dir_name}/#{number}.pdf"))
end

#idObject



31
32
33
# File 'lib/uva-tools/problem.rb', line 31

def id
  @info[0]
end

#infoObject



43
44
45
46
47
48
49
50
# File 'lib/uva-tools/problem.rb', line 43

def info
  {
    id: id,
    number: number,
    title: title,
    run_time: @info[19]
  }
end

#numberObject



35
36
37
# File 'lib/uva-tools/problem.rb', line 35

def number
  @info[1]
end

#openObject



117
118
119
120
121
122
123
124
125
# File 'lib/uva-tools/problem.rb', line 117

def open
  if f_path = path
    `open #{f_path}`
    true
  else
    puts "Problem not downloaded!"
    false
  end
end

#removeObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/uva-tools/problem.rb', line 98

def remove
  if downloaded?
    Dir.chdir(dir_name) do
      Dir.entries('.').each do |file_name|
        if file_name =~ /^#{number}.*/ && !File.directory?(file_name)
          File.delete(file_name)
        end
      end
    end
    true
  else
    false
  end
end

#submission_infoObject



64
65
66
67
68
69
70
# File 'lib/uva-tools/problem.rb', line 64

def submission_info
  {
    submitted: ,
    accepted: accepted,
    dacu: dacu
  }
end

#submittedObject



52
53
54
# File 'lib/uva-tools/problem.rb', line 52

def 
  @info[6..18].inject(:+)
end

#titleObject



39
40
41
# File 'lib/uva-tools/problem.rb', line 39

def title
  @info[2]
end